Author: tfmorris
Date: 2007-06-08 00:50:23-0700
New Revision: 12782
Modified:
trunk/src/model-euml/src/org/argouml/model/euml/EUMLModelImplementation.java
trunk/src/model-euml/src/org/argouml/model/euml/XmiReaderEUMLImpl.java
trunk/src/model-euml/src/org/argouml/model/euml/XmiWriterEUMLImpl.java
Log:
Issue 4777 - Refactor XMI writer
Modified: trunk/src/model-euml/src/org/argouml/model/euml/EUMLModelImplementation.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/model-euml/src/org/argouml/model/euml/EUMLModelImplementation.java?view=diff&rev=12782&p1=trunk/src/model-euml/src/org/argouml/model/euml/EUMLModelImplementation.java&p2=trunk/src/model-euml/src/org/argouml/model/euml/EUMLModelImplementation.java&r1=12781&r2=12782
==============================================================================
--- trunk/src/model-euml/src/org/argouml/model/euml/EUMLModelImplementation.java (original)
+++ trunk/src/model-euml/src/org/argouml/model/euml/EUMLModelImplementation.java 2007-06-08 00:50:23-0700
@@ -26,6 +26,7 @@
package org.argouml.model.euml;
+import java.io.OutputStream;
import java.io.Writer;
import org.apache.log4j.Logger;
@@ -66,26 +67,8 @@
import org.argouml.model.VisibilityKind;
import org.argouml.model.XmiReader;
import org.argouml.model.XmiWriter;
-import org.argouml.model.euml.ActivityGraphsFactoryEUMLlImpl;
-import org.argouml.model.euml.ActivityGraphsHelperEUMLImpl;
-import org.argouml.model.euml.CommonBehaviorFactoryEUMLImpl;
-import org.argouml.model.euml.CommonBehaviorHelperEUMLImpl;
-import org.argouml.model.euml.CoreFactoryEUMLImpl;
-import org.argouml.model.euml.CoreHelperEUMLImpl;
-import org.argouml.model.euml.DataTypesFactoryEUMLImpl;
-import org.argouml.model.euml.DataTypesHelperEUMLImpl;
-import org.argouml.model.euml.ExtensionMechanismsFactoryEUMLImpl;
-import org.argouml.model.euml.ExtensionMechanismsHelperEUMLImpl;
-import org.argouml.model.euml.ModelEventPumpEUMLImpl;
-import org.argouml.model.euml.ModelManagementFactoryEUMLImpl;
-import org.argouml.model.euml.ModelManagementHelperEUMLImpl;
-import org.argouml.model.euml.StateMachinesFactoryEUMLImpl;
-import org.argouml.model.euml.StateMachinesHelperEUMLImpl;
-import org.argouml.model.euml.UseCasesFactoryEUMLImpl;
-import org.argouml.model.euml.UseCasesHelperEUMLImpl;
import org.eclipse.emf.ecore.resource.Resource;
-import org.eclipse.uml2.uml.UMLFactory;
-import org.eclipse.uml2.uml.resource.UMLResource;
+import org.eclipse.uml2.uml.resource.XMI2UMLResource;
/**
* Eclipse UML2 implementation of the ArgoUML Model subsystem. Although built on
@@ -178,21 +161,21 @@
* Constructor.
*/
public EUMLModelImplementation() {
-
- registerResourceFactories();
+ Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
+ XMI2UMLResource.FILE_EXTENSION, XMI2UMLResource.Factory.INSTANCE);
-// model = UMLFactory.eINSTANCE.createModel();
-// model.setName("uml2_simple_model");
+ // This registers the .uml extension too, but that conflicts with
+ // the ArgoUML usage for a concatenated project file.
+// Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
+// UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
-// theCoreFactory = new CoreFactoryEUMLImpl(this);
-//
-//
-//
-// // Create and start event pump first so it's available for all others
+ // Create and start event pump first so it's available for all others
// theModelEventPump = new ModelEventPumpEUMLImpl(this);
// theModelEventPump.startPumpingEvents();
// LOG.debug("EUML Init - event pump started");
-//
+
+ // We may want to initialize some basic packages first,
+ // but we should try to use lazy initialization for most/all.
// // DataTypes is next so it's available for Kinds, ModelManagement,
// // & Extensions
// theDataTypesFactory = new DataTypesFactoryEUMLImpl(this);
@@ -206,43 +189,8 @@
// thePseudostateKind = new PseudostateKindEUMLImpl(this);
// theScopeKind = new ScopeKindEUMLImpl(this);
// theVisibilityKind = new VisibilityKindEUMLImpl(this);
-//
-// theModelManagementFactory = new ModelManagementFactoryEUMLImpl(this);
-// theExtensionMechanismsHelper =
-// new ExtensionMechanismsHelperEUMLImpl(this);
-// theExtensionMechanismsFactory =
-// new ExtensionMechanismsFactoryEUMLImpl(this);
-// LOG.debug("EUML Init - initialized package Extension mechanism");
-//
-// // Initialize remaining factories and helpers
-// // (but defer heavyweight ones until needed)
-// theCopyHelper = new CopyHelperEUMLImpl(this);
-// theActivityGraphsHelper = new ActivityGraphsHelperEUMLImpl(this);
-// theCoreHelper = new CoreHelperEUMLImpl(this);
-// LOG.debug("EUML Init - initialized package Core helper");
-// theModelManagementHelper = new ModelManagementHelperEUMLImpl(this);
-// theStateMachinesHelper = new StateMachinesHelperEUMLImpl(this);
-// LOG.debug("EUML Init - initialized package StateMachines");
-// theUseCasesFactory = new UseCasesFactoryEUMLImpl(this);
-// theUseCasesHelper = new UseCasesHelperEUMLImpl(this);
-// LOG.debug("EUML Init - initialized package Use Cases");
-// theActivityGraphsFactory = new ActivityGraphsFactoryEUMLlImpl(this);
-// LOG.debug("EUML Init - initialized package Collaborations");
-// theCommonBehaviorFactory = new CommonBehaviorFactoryEUMLImpl(this);
-// theCommonBehaviorHelper = new CommonBehaviorHelperEUMLImpl(this);
-// LOG.debug("EUML Init - initialized package CommonBehavior");
-// theStateMachinesFactory = new StateMachinesFactoryEUMLImpl(this);
-// theCoreFactory = new CoreFactoryEUMLImpl(this);
-// LOG.debug("EUML Init - all packages initialized");
}
- private static void registerResourceFactories() {
- Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
- UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE);
- Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(
- "xmi", UMLResource.Factory.INSTANCE);
- }
-
public ActivityGraphsFactory getActivityGraphsFactory() {
if (theActivityGraphsFactory == null) {
theActivityGraphsFactory = new ActivityGraphsFactoryEUMLlImpl(this);
@@ -487,9 +435,16 @@
return new XmiWriterEUMLImpl(this, model, writer, version);
}
+ public XmiWriter getXmiWriter(Object model, OutputStream stream,
+ String version) throws UmlException {
+ return new XmiWriterEUMLImpl(this, model, stream, version);
+ }
+
public DiagramInterchangeModel getDiagramInterchangeModel() {
// TODO Auto-generated method stub
return null;
}
+
+
}
Modified: trunk/src/model-euml/src/org/argouml/model/euml/XmiReaderEUMLImpl.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/model-euml/src/org/argouml/model/euml/XmiReaderEUMLImpl.java?view=diff&rev=12782&p1=trunk/src/model-euml/src/org/argouml/model/euml/XmiReaderEUMLImpl.java&p2=trunk/src/model-euml/src/org/argouml/model/euml/XmiReaderEUMLImpl.java&r1=12781&r2=12782
==============================================================================
--- trunk/src/model-euml/src/org/argouml/model/euml/XmiReaderEUMLImpl.java (original)
+++ trunk/src/model-euml/src/org/argouml/model/euml/XmiReaderEUMLImpl.java 2007-06-08 00:50:23-0700
@@ -1,7 +1,7 @@
// $Id$
// Copyright (c) 2007, The ArgoUML Project
// All rights reserved.
-//
+//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
@@ -12,7 +12,7 @@
// * Neither the name of the ArgoUML Project nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
-//
+//
// THIS SOFTWARE IS PROVIDED BY THE ArgoUML PROJECT ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
@@ -26,11 +26,26 @@
package org.argouml.model.euml;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.Collection;
+import java.util.HashMap;
import java.util.Map;
+import org.apache.log4j.Logger;
import org.argouml.model.UmlException;
import org.argouml.model.XmiReader;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EcorePackage;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.resource.URIConverter;
+import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
+import org.eclipse.emf.ecore.util.EcoreUtil;
+import org.eclipse.uml2.uml.UMLPackage;
+import org.eclipse.uml2.uml.resource.UMLResource;
import org.xml.sax.InputSource;
/**
@@ -38,11 +53,18 @@
*/
class XmiReaderEUMLImpl implements XmiReader {
+ private static Logger LOG = Logger.getLogger(XmiReaderEUMLImpl.class);
+
+ // TODO: This shouldn't really be hardcoded - tfm
+ private final static String ENCODING = "UTF-8";
+
/**
* The model implementation.
*/
private EUMLModelImplementation modelImpl;
+ private ResourceSet resourceSet;
+
/**
* Constructor.
*
@@ -51,6 +73,40 @@
*/
public XmiReaderEUMLImpl(EUMLModelImplementation implementation) {
modelImpl = implementation;
+
+ resourceSet = new ResourceSetImpl();
+
+ // TODO: We need both complete paths and version independence here
+ // We probably need sometype of run-time lookup scheme
+ // This code is from
+ // http://www.eclipse.org/modeling/mdt/uml2/docs/guides/UML2_2.0_Migration_Guide/guide.html
+ URI umlPluginURI = URI.createURI("jar:file:/"
+ + "org.eclipse.uml2.uml_2.1.0.v200703011358.jar"
+ + "!/");
+ URI umlResourcesPluginURI = URI.createURI("jar:file:/"
+ + "org.eclipse.uml2.uml.resources_2.1.0.v200705251139.jar"
+ + "!/");
+
+ // register the necessary pathmaps
+ Map uriMap = URIConverter.URI_MAP;
+ uriMap.put(URI.createURI(UMLResource.LIBRARIES_PATHMAP),
+ umlResourcesPluginURI.appendSegment("libraries").appendSegment(""));
+ uriMap.put(URI.createURI(UMLResource.METAMODELS_PATHMAP),
+ umlResourcesPluginURI.appendSegment("metamodels").appendSegment(""));
+ uriMap.put(URI.createURI(UMLResource.PROFILES_PATHMAP),
+ umlResourcesPluginURI.appendSegment("profiles").appendSegment(""));
+
+
+ // for a stand alone application, it is necessary to map the
+ // platform plugin URI scheme to the workspace location
+ uriMap.put(URI.createURI("platform:/plugin/org.eclipse.uml2.uml/"),
+ umlPluginURI);
+
+ // Add namespace URIs for eCore and eUML2 to package registry
+ Map packageRegistry = resourceSet.getPackageRegistry();
+ packageRegistry.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
+ packageRegistry.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE);
+
}
public int getIgnoredElementCount() {
@@ -60,23 +116,33 @@
public String[] getIgnoredElements() {
// TODO Auto-generated method stub
- return null;
+ return new String[0];
}
public Map getXMIUUIDToObjectMap() {
// TODO Auto-generated method stub
- return null;
+ return new HashMap();
}
- public Collection parse(InputSource pIs) throws UmlException {
- // TODO Auto-generated method stub
- return null;
+ public Collection parse(InputSource inputSource) throws UmlException {
+ return parse(inputSource, false);
}
- public Collection parse(InputSource pIs, boolean profile)
- throws UmlException {
- // TODO Auto-generated method stub
- return null;
+ public Collection parse(InputSource inputSource, boolean profile)
+ throws UmlException {
+
+ // TODO: This is a hack. Look for alternatives, possibly reworking API
+ File inputFile;
+ try {
+ inputFile = copySource(inputSource);
+ } catch (IOException e) {
+ throw new UmlException("Error creating temporary file", e);
+ }
+
+ Resource resource = resourceSet.getResource(URI.createFileURI(inputFile.toString()), true);
+ EcoreUtil.resolveAll(resource);
+
+ return resource.getContents();
}
public boolean setIgnoredElements(String[] elementNames) {
@@ -84,4 +150,30 @@
return false;
}
+ private File copySource(InputSource input) throws IOException {
+ byte[] buf = new byte[2048];
+ int len;
+
+ // Create & set up temporary output file
+ File tmpOutFile = File.createTempFile("zargo_model_", ".xmi");
+ tmpOutFile.deleteOnExit();
+ FileOutputStream out = new FileOutputStream(tmpOutFile);
+
+ // Add our XML declaration back to the front of the file
+// String decl = "<?xml version=\"1.0\" encoding=\""
+// + ENCODING + "\"?>\n";
+// out.write(decl.getBytes(ENCODING), 0, decl.length());
+
+ InputStream in = input.getByteStream();
+ while ((len = in.read(buf)) > 0) {
+ out.write(buf, 0, len);
+ }
+
+ LOG.debug("Wrote copied XMI file to " + tmpOutFile);
+ return tmpOutFile;
+ }
+
+ public String getTagName() {
+ return "uml:Model";
+ }
}
Modified: trunk/src/model-euml/src/org/argouml/model/euml/XmiWriterEUMLImpl.java
Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/model-euml/src/org/argouml/model/euml/XmiWriterEUMLImpl.java?view=diff&rev=12782&p1=trunk/src/model-euml/src/org/argouml/model/euml/XmiWriterEUMLImpl.java&p2=trunk/src/model-euml/src/org/argouml/model/euml/XmiWriterEUMLImpl.java&r1=12781&r2=12782
==============================================================================
--- trunk/src/model-euml/src/org/argouml/model/euml/XmiWriterEUMLImpl.java (original)
+++ trunk/src/model-euml/src/org/argouml/model/euml/XmiWriterEUMLImpl.java 2007-06-08 00:50:23-0700
@@ -26,14 +26,30 @@
package org.argouml.model.euml;
+import java.io.IOException;
+import java.io.OutputStream;
import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+import org.argouml.model.NotImplementedException;
import org.argouml.model.UmlException;
import org.argouml.model.XmiExtensionWriter;
import org.argouml.model.XmiWriter;
+import org.eclipse.emf.common.util.EList;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
+import org.eclipse.emf.ecore.xmi.XMLResource;
/**
* Eclipse UML2 implementation of XmiWriter.
+ *
+ * TODO: We need facilities for writing and reading stable IDs to/from either
+ * xmi.id or xmi.uuid.
+ *
+ * @author Tom Morris
*/
class XmiWriterEUMLImpl implements XmiWriter {
@@ -42,6 +58,18 @@
*/
private EUMLModelImplementation modelImpl;
+ private OutputStream oStream;
+
+ private org.eclipse.uml2.uml.Package model;
+
+ /**
+ * Old style constructor. Unsupported (and shouldn't get called)
+ */
+ public XmiWriterEUMLImpl(EUMLModelImplementation implementation,
+ Object model, Writer writer, String version) {
+ throw new NotImplementedException();
+ }
+
/**
* Constructor.
*
@@ -55,18 +83,56 @@
* The version of ArgoUML.
*/
public XmiWriterEUMLImpl(EUMLModelImplementation implementation,
- Object model, Writer writer, String version) {
+ Object theModel, OutputStream stream, String version) {
modelImpl = implementation;
+ if (stream == null) {
+ throw new IllegalArgumentException("An OutputStream must be provided");
+ }
+ if (!(theModel instanceof org.eclipse.uml2.uml.Package)) {
+ throw new IllegalArgumentException("A model must be provided"
+ + " and it must be a UML 2 Package");
+ }
+ if (implementation == null) {
+ throw new IllegalArgumentException("A parent must be provided");
+ }
+ modelImpl = implementation;
+ model = (org.eclipse.uml2.uml.Package) theModel;
+ oStream = stream;
}
- public void setXmiExtensionWriter(XmiExtensionWriter xmiExtensionWriter) {
- // TODO Auto-generated method stub
+ public void write() throws UmlException {
+ // This URI is a dummy. We're going to write to our OutputStream
+ Resource resource =
+ new ResourceSetImpl().createResource(URI
+ .createFileURI("foo.xmi"));
+ EList<EObject> contents = resource.getContents();
+ contents.add(model);
+
+ // Do we need to get stereotype applications for each element? - tfm
+// for (Iterator allContents = UMLUtil.getAllContents(model, true,
+// false); allContents.hasNext();) {
+// EObject eObject = (EObject) allContents.next();
+// if (eObject instanceof Element) {
+// contents.addAll(((Element) eObject).getStereotypeApplications());
+// }
+// }
+
+ Map options = new HashMap();
+ options.put(XMLResource.OPTION_LINE_WIDTH, 100);
+
+ // TODO: Is there an option we can use to save our ArgoUML version?
+
+ try {
+ resource.save(oStream, options);
+ } catch (IOException ioe) {
+ throw new UmlException(ioe);
+ }
}
- public void write() throws UmlException {
- // TODO Auto-generated method stub
-
+ public void setXmiExtensionWriter(XmiExtensionWriter xmiExtensionWriter) {
+ throw new NotYetImplementedException();
}
+
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.