You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by sc...@apache.org on 2014/08/11 23:13:00 UTC
svn commit: r1617360 -
/uima/uimaj/branches/2.6.0-json/uimaj-core/src/main/java/org/apache/uima/cas/impl/XmiCasSerializer.java
Author: schor
Date: Mon Aug 11 21:13:00 2014
New Revision: 1617360
URL: http://svn.apache.org/r1617360
Log:
[UIMA-3969] a simple reordering - moving the inner class to the bottom, in order to have the outer class's statics, local fields, static and instance methods all in "one place" rather than split.
Modified:
uima/uimaj/branches/2.6.0-json/uimaj-core/src/main/java/org/apache/uima/cas/impl/XmiCasSerializer.java
Modified: uima/uimaj/branches/2.6.0-json/uimaj-core/src/main/java/org/apache/uima/cas/impl/XmiCasSerializer.java
URL: http://svn.apache.org/viewvc/uima/uimaj/branches/2.6.0-json/uimaj-core/src/main/java/org/apache/uima/cas/impl/XmiCasSerializer.java?rev=1617360&r1=1617359&r2=1617360&view=diff
==============================================================================
--- uima/uimaj/branches/2.6.0-json/uimaj-core/src/main/java/org/apache/uima/cas/impl/XmiCasSerializer.java (original)
+++ uima/uimaj/branches/2.6.0-json/uimaj-core/src/main/java/org/apache/uima/cas/impl/XmiCasSerializer.java Mon Aug 11 21:13:00 2014
@@ -99,159 +99,457 @@ public class XmiCasSerializer {
return numChildren;
}
- /**
- * Use an inner class to hold the data for serializing a CAS. Each call to serialize() creates its
- * own instance.
- *
- *
- */
- private class XmiCasDocSerializer {
-
- // Where the output goes.
- private ContentHandler ch;
-
- // optional error handler, mainly so we can send warnings
- private ErrorHandler eh = null;
-
- // The CAS we're serializing.
- private CASImpl cas;
- // Any FS reference we've touched goes in here.
- private IntRedBlackTree visited;
+ public static final String XMLNS_NS_URI = "http://www.w3.org/2000/xmlns/";
- // All FSs that are in an index somewhere.
- private IntVector indexedFSs;
+ public static final String XMI_NS_URI = "http://www.omg.org/XMI";
- // The current queue for FSs to write out.
- private IntStack queue;
+ public static final String XSI_NS_URI = "http://www.w3.org/2001/XMLSchema-instance";
- // SofaFS type
- // private int sofaTypeCode;
+ public static final String XMI_NS_PREFIX = "xmi";
- // Annotation type
- // private int annotationTypeCode;
+ public static final String XMI_TAG_LOCAL_NAME = "XMI";
- private final AttributesImpl emptyAttrs = new AttributesImpl();
+ public static final String XMI_TAG_QNAME = "xmi:XMI";
- private AttributesImpl workAttrs = new AttributesImpl();
+ public static final XmlElementName XMI_TAG = new XmlElementName(XMI_NS_URI, XMI_TAG_LOCAL_NAME,
+ XMI_TAG_QNAME);
- private static final String cdataType = "CDATA";
+ public static final String INDEXED_ATTR_NAME = "_indexed";
- // For debug statistics.
- private int fsCount = 0;
+ public static final String ID_ATTR_NAME = "xmi:id";
- // utilities for dealing with CAS list types
- private ListUtils listUtils;
+ public static final String XMI_VERSION_LOCAL_NAME = "version";
- // holds the addresses of Array and List FSs that we have encountered
- private IntRedBlackTree arrayAndListFSs;
+ public static final String XMI_VERSION_QNAME = "xmi:version";
- private XmiSerializationSharedData sharedData;
+ public static final String XMI_VERSION_VALUE = "2.0";
- private XmlElementName[] xmiTypeNames; // array, indexed by type code, giving XMI names for
+ /** Namespace URI to use for UIMA types that have no namespace (the "default pacakge" in Java) */
+ public static final String DEFAULT_NAMESPACE_URI = "http:///uima/noNamespace.ecore";
- // each type
+ private TypeSystemImpl filterTypeSystem;
- private Map<String, String> nsUriToPrefixMap = new HashMap<String, String>();
-
- // lives just for one serialize call
- private Map<String, String> uniqueStrings = new HashMap<String, String>();
-
- private Set<String> nsPrefixesUsed = new HashSet<String>();
-
- /**
- * Used to tell if a FS was created before or after mark.
- */
- private MarkerImpl marker;
+ // UIMA logger, to which we may write warnings
+ private Logger logger;
- /**
- * Whether the serializer needs to check for filtered-out types/features. Set to true if type
- * system of CAS does not match type system that was passed to constructor of serializer.
- */
- boolean isFiltering;
+ private Map<String, String> nsUriToSchemaLocationMap = null;
- /**
- * Whether the serializer needs to serialize only the deltas, that is, new FSs created after
- * mark represented by Marker object and preexisting FSs and Views that have been
- * modified. Set to true if Marker object is not null and CASImpl object of this serialize
- * matches the CASImpl in Marker object.
- */
- boolean isDelta;
-
- private XmiCasDocSerializer(ContentHandler ch, ErrorHandler eh, CASImpl cas,
- XmiSerializationSharedData sharedData, MarkerImpl marker) {
- super();
- this.ch = ch;
- this.eh = eh;
- this.cas = cas;
- this.visited = new IntRedBlackTree();
- this.queue = new IntStack();
- this.indexedFSs = new IntVector();
- // this.sofaTypeCode = cas.getTypeSystemImpl().getTypeCode(CAS.TYPE_NAME_SOFA);
- // this.annotationTypeCode = cas.getTypeSystemImpl().getTypeCode(CAS.TYPE_NAME_ANNOTATION);
- this.listUtils = new ListUtils(cas, logger, eh);
- this.arrayAndListFSs = new IntRedBlackTree();
- this.sharedData = sharedData;
- this.isFiltering = filterTypeSystem != null && filterTypeSystem != cas.getTypeSystemImpl();
- if (marker != null ) {
- if (marker.isValid())
- this.marker = marker;
- else {
- CASRuntimeException exception = new CASRuntimeException(
- CASRuntimeException.INVALID_MARKER, new String[] { "Invalid Marker." });
- throw exception;
- }
- }
- this.isDelta = false;
- if (this.marker != null) this.isDelta = true;
- }
+ /**
+ * Creates a new XmiCasSerializer.
+ *
+ * @param ts
+ * the TypeSystem of CASes that will be serialized. If any CAS that is later passed to
+ * the <code>serialize</code> method that contains types and features that are not in
+ * this typesystem, the serialization will not contain instances of those types or values
+ * for those features. So this can be used to filter the results of serialization.
+ * @param nsUriToSchemaLocationMap
+ * Map if supplied, this map is used to generate a "schemaLocation" attribute in the XMI
+ * output. This argument must be a map from namespace URIs to the schema location for
+ * that namespace URI.
+ */
+ public XmiCasSerializer(TypeSystem ts, Map<String, String> nsUriToSchemaLocationMap) {
+ super();
+ // System.out.println("Creating serializer for type system.");
+ this.filterTypeSystem = (TypeSystemImpl) ts;
+ this.nsUriToSchemaLocationMap = nsUriToSchemaLocationMap;
+ this.logger = UIMAFramework.getLogger(XmiCasSerializer.class);
+ }
- // TODO: internationalize
- private void reportWarning(String message) throws SAXException {
- logger.log(Level.WARNING, message);
- if (this.eh != null) {
- this.eh.warning(new SAXParseException(message, null));
- }
- }
+ /**
+ * Creates a new XmiCasSerializer.
+ *
+ * @param ts
+ * the TypeSystem of CASes that will be serialized. If any CAS that is later passed to
+ * the <code>serialize</code> method that contains types and features that are not in
+ * this typesystem, the serialization will not contain instances of those types or values
+ * for those features. So this can be used to filter the results of serialization.
+ * A null value indicates that all types and features will be serialized.
+ */
+ public XmiCasSerializer(TypeSystem ts) {
+ this(ts, (Map) null);
+ }
- /**
- * Check if we've seen this address before.
- *
- * @param addr
- * The address.
- * @return <code>true</code> if we've seen the address before.
- */
- private boolean isVisited(int addr) {
- return visited.containsKey(addr);
- }
+ /**
+ * Creates a new XmiCasSerializer.
+ *
+ * @param ts
+ * the TypeSystem of CASes that will be serialized. If any CAS that is later passed to
+ * the <code>serialize</code> method that contains types and features that are not in
+ * this typesystem, the serialization will not contain instances of those types or values
+ * for those features. So this can be used to filter the results of serialization.
+ * @param uimaContext
+ * not used
+ * @param nsUriToSchemaLocationMap
+ * Map if supplied, this map is used to generate a "schemaLocation" attribute in the XMI
+ * output. This argument must be a map from namespace URIs to the schema location for
+ * that namespace URI.
+ *
+ * @deprecated Use {@link #XmiCasSerializer(TypeSystem, Map)} instead. The UimaContext reference
+ * is never used by this implementation.
+ */
+ @Deprecated
+ public XmiCasSerializer(TypeSystem ts, UimaContext uimaContext, Map<String, String> nsUriToSchemaLocationMap) {
+ this(ts, nsUriToSchemaLocationMap);
+ }
- /**
- * Starts serialization
- */
- private void serialize() throws SAXException {
- // populate nsUriToPrefixMap and xmiTypeNames structures based on CAS
- // type system, and out of typesystem data if any
- initTypeAndNamespaceMappings();
+ /**
+ * Creates a new XmiCasSerializer.
+ *
+ * @param ts
+ * the TypeSystem of CASes that will be serialized. If any CAS that is later passed to
+ * the <code>serialize</code> method that contains types and features that are not in
+ * this typesystem, the serialization will not contain instances of those types or values
+ * for those features. So this can be used to filter the results of serialization.
+ * @param uimaContext
+ * not used
+ *
+ * @deprecated Use {@link #XmiCasSerializer(TypeSystem)} instead. The UimaContext reference is
+ * never used by this implementation.
+ */
+ @Deprecated
+ public XmiCasSerializer(TypeSystem ts, UimaContext uimaContext) {
+ this(ts);
+ }
- int iElementCount = 1; // start at 1 to account for special NULL object
+ /**
+ * Write the CAS data to a SAX content handler.
+ *
+ * @param cas
+ * The CAS to be serialized.
+ * @param contentHandler
+ * The SAX content handler the data is written to. should be inserted into the XCAS
+ * output
+ *
+ * @throws SAXException if there was a SAX exception
+ */
+ public void serialize(CAS cas, ContentHandler contentHandler) throws SAXException {
+ this.serialize(cas, contentHandler, null);
+ }
- enqueueIncoming(); //make sure we enqueue every FS that was deserialized into this CAS
- enqueueIndexed();
- enqueueNonsharedMultivaluedFS();
- enqueueFeaturesOfIndexed();
- iElementCount += indexedFSs.size();
- iElementCount += queue.size();
+ /**
+ * Write the CAS data to a SAX content handler.
+ *
+ * @param cas
+ * The CAS to be serialized.
+ * @param contentHandler
+ * The SAX content handler the data is written to. should be inserted into the XCAS
+ * output
+ * @param errorHandler the SAX Error Handler to use
+ *
+ * @throws SAXException if there was a SAX exception
+ */
+ public void serialize(CAS cas, ContentHandler contentHandler, ErrorHandler errorHandler)
+ throws SAXException {
+ contentHandler.startDocument();
+ XmiCasDocSerializer ser = new XmiCasDocSerializer(contentHandler, errorHandler, ((CASImpl) cas)
+ .getBaseCAS(), null, null);
+ ser.serialize();
+ contentHandler.endDocument();
+ }
- FSIndex<FeatureStructure> sofaIndex = cas.getBaseCAS().indexRepository.getIndex(CAS.SOFA_INDEX_NAME);
- if (!isDelta) {
- iElementCount += (sofaIndex.size()); // one View element per sofa
- if (this.sharedData != null) {
- iElementCount += this.sharedData.getOutOfTypeSystemElements().size();
- }
- } else {
- int numViews = cas.getBaseSofaCount();
- for (int sofaNum = 1; sofaNum <= numViews; sofaNum++) {
- FSIndexRepositoryImpl loopIR = (FSIndexRepositoryImpl) cas.getBaseCAS()
+ /**
+ * Write the CAS data to a SAX content handler.
+ *
+ * @param cas
+ * The CAS to be serialized.
+ * @param contentHandler
+ * The SAX content handler the data is written to. should be inserted into the XCAS
+ * output
+ * @param sharedData
+ * data structure used to allow the XmiCasSerializer and XmiCasDeserializer to share
+ * information.
+ * @param errorHandler the SAX Error Handler to use
+ *
+ * @throws SAXException if there was a SAX exception
+ */
+ public void serialize(CAS cas, ContentHandler contentHandler, ErrorHandler errorHandler,
+ XmiSerializationSharedData sharedData) throws SAXException {
+ contentHandler.startDocument();
+ XmiCasDocSerializer ser = new XmiCasDocSerializer(contentHandler, errorHandler, ((CASImpl) cas)
+ .getBaseCAS(), sharedData, (MarkerImpl) null);
+ ser.serialize();
+ contentHandler.endDocument();
+ }
+
+ /**
+ * Write the CAS data to a SAX content handler.
+ *
+ * @param cas
+ * The CAS to be serialized.
+ * @param contentHandler
+ * The SAX content handler the data is written to. should be inserted into the XCAS
+ * output
+ * @param errorHandler the SAX Error Handler to use
+ * @param sharedData
+ * data structure used to allow the XmiCasSerializer and XmiCasDeserializer to share
+ * information.
+ * @param marker
+ * an object used to filter the FSs and Views to determine if these were created after
+ * the mark was set. Used to serialize a Delta CAS consisting of only new FSs and views and
+ * preexisting FSs and Views that have been modified.
+ *
+ * @throws SAXException if there was a SAX exception
+ */
+ public void serialize(CAS cas, ContentHandler contentHandler, ErrorHandler errorHandler,
+ XmiSerializationSharedData sharedData, Marker marker) throws SAXException {
+
+ contentHandler.startDocument();
+ XmiCasDocSerializer ser = new XmiCasDocSerializer(contentHandler, errorHandler, ((CASImpl) cas)
+ .getBaseCAS(), sharedData, (MarkerImpl) marker);
+ ser.serialize();
+ contentHandler.endDocument();
+ }
+
+ /**
+ * Serializes a CAS to an XMI stream.
+ *
+ * @param aCAS
+ * CAS to serialize.
+ * @param aStream
+ * output stream to which to write the XMI document
+ *
+ * @throws SAXException
+ * if a problem occurs during XMI serialization
+ */
+ public static void serialize(CAS aCAS, OutputStream aStream) throws SAXException {
+ serialize(aCAS, null, aStream, false, null);
+ }
+
+ /**
+ * Serializes a CAS to an XMI stream. Allows a TypeSystem to be specified, to which the produced
+ * XMI will conform. Any types or features not in the target type system will not be serialized.
+ *
+ * @param aCAS
+ * CAS to serialize.
+ * @param aTargetTypeSystem
+ * type system to which the produced XMI will conform. Any types or features not in the
+ * target type system will not be serialized. A null value indicates that all types and features
+ * will be serialized.
+ * @param aStream
+ * output stream to which to write the XMI document
+ *
+ * @throws SAXException
+ * if a problem occurs during XMI serialization
+ */
+ public static void serialize(CAS aCAS, TypeSystem aTargetTypeSystem, OutputStream aStream)
+ throws SAXException {
+ serialize(aCAS, aTargetTypeSystem, aStream, false, null);
+ }
+
+ /**
+ * Serializes a CAS to an XMI stream. This version of this method allows many options to be configured.
+ *
+ * @param aCAS
+ * CAS to serialize.
+ * @param aTargetTypeSystem
+ * type system to which the produced XMI will conform. Any types or features not in the
+ * target type system will not be serialized. A null value indicates that all types and features
+ * will be serialized.
+ * @param aStream
+ * output stream to which to write the XMI document
+ * @param aPrettyPrint
+ * if true the XML output will be formatted with newlines and indenting. If false it will be unformatted.
+ * @param aSharedData
+ * an optional container for data that is shared between the {@link XmiCasSerializer} and the {@link XmiCasDeserializer}.
+ * See the JavaDocs for {@link XmiSerializationSharedData} for details.
+ *
+ * @throws SAXException
+ * if a problem occurs during XMI serialization
+ */
+ public static void serialize(CAS aCAS, TypeSystem aTargetTypeSystem, OutputStream aStream, boolean aPrettyPrint,
+ XmiSerializationSharedData aSharedData)
+ throws SAXException {
+ XmiCasSerializer xmiCasSerializer = new XmiCasSerializer(aTargetTypeSystem);
+ XMLSerializer sax2xml = new XMLSerializer(aStream, aPrettyPrint);
+ xmiCasSerializer.serialize(aCAS, sax2xml.getContentHandler(), null, aSharedData, null);
+ }
+
+ /**
+ * Serializes a Delta CAS to an XMI stream. This version of this method allows many options to be configured.
+ *
+ *
+ * @param aCAS
+ * CAS to serialize.
+ * @param aTargetTypeSystem
+ * type system to which the produced XMI will conform. Any types or features not in the
+ * target type system will not be serialized. A null value indicates that all types and features
+ * will be serialized.
+ * @param aStream
+ * output stream to which to write the XMI document
+ * @param aPrettyPrint
+ * if true the XML output will be formatted with newlines and indenting. If false it will be unformatted.
+ * @param aSharedData
+ * an optional container for data that is shared between the {@link XmiCasSerializer} and the {@link XmiCasDeserializer}.
+ * See the JavaDocs for {@link XmiSerializationSharedData} for details.
+ * @param aMarker
+ * an optional object that is used to filter and serialize a Delta CAS containing only
+ * those FSs and Views created after Marker was set and preexisting FSs and views that were modified.
+ * See the JavaDocs for {@link Marker} for details.
+ * @throws SAXException
+ * if a problem occurs during XMI serialization
+ */
+ public static void serialize(CAS aCAS, TypeSystem aTargetTypeSystem, OutputStream aStream, boolean aPrettyPrint,
+ XmiSerializationSharedData aSharedData, Marker aMarker)
+ throws SAXException {
+ XmiCasSerializer xmiCasSerializer = new XmiCasSerializer(aTargetTypeSystem);
+ XMLSerializer sax2xml = new XMLSerializer(aStream, aPrettyPrint);
+ xmiCasSerializer.serialize(aCAS, sax2xml.getContentHandler(), null, aSharedData, aMarker);
+ }
+
+ /**
+ * Use an inner class to hold the data for serializing a CAS. Each call to serialize() creates its
+ * own instance.
+ *
+ *
+ */
+ private class XmiCasDocSerializer {
+
+ // Where the output goes.
+ private ContentHandler ch;
+
+ // optional error handler, mainly so we can send warnings
+ private ErrorHandler eh = null;
+
+ // The CAS we're serializing.
+ private CASImpl cas;
+
+ // Any FS reference we've touched goes in here.
+ private IntRedBlackTree visited;
+
+ // All FSs that are in an index somewhere.
+ private IntVector indexedFSs;
+
+ // The current queue for FSs to write out.
+ private IntStack queue;
+
+ // SofaFS type
+ // private int sofaTypeCode;
+
+ // Annotation type
+ // private int annotationTypeCode;
+
+ private final AttributesImpl emptyAttrs = new AttributesImpl();
+
+ private AttributesImpl workAttrs = new AttributesImpl();
+
+ private static final String cdataType = "CDATA";
+
+ // For debug statistics.
+ private int fsCount = 0;
+
+ // utilities for dealing with CAS list types
+ private ListUtils listUtils;
+
+ // holds the addresses of Array and List FSs that we have encountered
+ private IntRedBlackTree arrayAndListFSs;
+
+ private XmiSerializationSharedData sharedData;
+
+ private XmlElementName[] xmiTypeNames; // array, indexed by type code, giving XMI names for
+
+ // each type
+
+ private Map<String, String> nsUriToPrefixMap = new HashMap<String, String>();
+
+ // lives just for one serialize call
+ private Map<String, String> uniqueStrings = new HashMap<String, String>();
+
+ private Set<String> nsPrefixesUsed = new HashSet<String>();
+
+ /**
+ * Used to tell if a FS was created before or after mark.
+ */
+ private MarkerImpl marker;
+
+ /**
+ * Whether the serializer needs to check for filtered-out types/features. Set to true if type
+ * system of CAS does not match type system that was passed to constructor of serializer.
+ */
+ boolean isFiltering;
+
+ /**
+ * Whether the serializer needs to serialize only the deltas, that is, new FSs created after
+ * mark represented by Marker object and preexisting FSs and Views that have been
+ * modified. Set to true if Marker object is not null and CASImpl object of this serialize
+ * matches the CASImpl in Marker object.
+ */
+ boolean isDelta;
+
+ private XmiCasDocSerializer(ContentHandler ch, ErrorHandler eh, CASImpl cas,
+ XmiSerializationSharedData sharedData, MarkerImpl marker) {
+ super();
+ this.ch = ch;
+ this.eh = eh;
+ this.cas = cas;
+ this.visited = new IntRedBlackTree();
+ this.queue = new IntStack();
+ this.indexedFSs = new IntVector();
+ // this.sofaTypeCode = cas.getTypeSystemImpl().getTypeCode(CAS.TYPE_NAME_SOFA);
+ // this.annotationTypeCode = cas.getTypeSystemImpl().getTypeCode(CAS.TYPE_NAME_ANNOTATION);
+ this.listUtils = new ListUtils(cas, logger, eh);
+ this.arrayAndListFSs = new IntRedBlackTree();
+ this.sharedData = sharedData;
+ this.isFiltering = filterTypeSystem != null && filterTypeSystem != cas.getTypeSystemImpl();
+ if (marker != null ) {
+ if (marker.isValid())
+ this.marker = marker;
+ else {
+ CASRuntimeException exception = new CASRuntimeException(
+ CASRuntimeException.INVALID_MARKER, new String[] { "Invalid Marker." });
+ throw exception;
+ }
+ }
+ this.isDelta = false;
+ if (this.marker != null) this.isDelta = true;
+ }
+
+ // TODO: internationalize
+ private void reportWarning(String message) throws SAXException {
+ logger.log(Level.WARNING, message);
+ if (this.eh != null) {
+ this.eh.warning(new SAXParseException(message, null));
+ }
+ }
+
+ /**
+ * Check if we've seen this address before.
+ *
+ * @param addr
+ * The address.
+ * @return <code>true</code> if we've seen the address before.
+ */
+ private boolean isVisited(int addr) {
+ return visited.containsKey(addr);
+ }
+
+ /**
+ * Starts serialization
+ */
+ private void serialize() throws SAXException {
+ // populate nsUriToPrefixMap and xmiTypeNames structures based on CAS
+ // type system, and out of typesystem data if any
+ initTypeAndNamespaceMappings();
+
+ int iElementCount = 1; // start at 1 to account for special NULL object
+
+ enqueueIncoming(); //make sure we enqueue every FS that was deserialized into this CAS
+ enqueueIndexed();
+ enqueueNonsharedMultivaluedFS();
+ enqueueFeaturesOfIndexed();
+ iElementCount += indexedFSs.size();
+ iElementCount += queue.size();
+
+ FSIndex<FeatureStructure> sofaIndex = cas.getBaseCAS().indexRepository.getIndex(CAS.SOFA_INDEX_NAME);
+ if (!isDelta) {
+ iElementCount += (sofaIndex.size()); // one View element per sofa
+ if (this.sharedData != null) {
+ iElementCount += this.sharedData.getOutOfTypeSystemElements().size();
+ }
+ } else {
+ int numViews = cas.getBaseSofaCount();
+ for (int sofaNum = 1; sofaNum <= numViews; sofaNum++) {
+ FSIndexRepositoryImpl loopIR = (FSIndexRepositoryImpl) cas.getBaseCAS()
.getSofaIndexRepository(sofaNum);
if (loopIR != null && loopIR.isModified()) {
iElementCount++;
@@ -1255,396 +1553,100 @@ public class XmiCasSerializer {
sb[URIPFX.length + i] = ( c == '.') ? '/' : c;
}
System.arraycopy(URISFX, 0, sb, URIPFX.length + i, URISFX.length);
- nsUri = getUniqueString(new String(sb));
-
-// nsUri = "http:///" + namespace.replace('.', '/') + ".ecore";
- }
- // convert short name to shared string, without interning, reduce GCs
- shortName = getUniqueString(shortName);
-
- // determine what namespace prefix to use
- String prefix = (String) nsUriToPrefixMap.get(nsUri);
- if (prefix == null) {
- if (lastDotIndex != -1) { // have namespace
- int secondLastDotIndex = uimaTypeName.lastIndexOf('.', lastDotIndex-1);
- prefix = uimaTypeName.substring(secondLastDotIndex + 1, lastDotIndex);
- } else {
- prefix = "noNamespace";
- }
- // make sure this prefix hasn't already been used for some other namespace
- if (nsPrefixesUsed.contains(prefix)) {
- String basePrefix = prefix;
- int num = 2;
- while (nsPrefixesUsed.contains(basePrefix + num)) {
- num++;
- }
- prefix = basePrefix + num;
- }
- nsUriToPrefixMap.put(nsUri, prefix);
- nsPrefixesUsed.add(prefix);
- }
-
- return new XmlElementName(nsUri, shortName, getUniqueString(prefix + ':' + shortName));
- }
-
- /*
- * convert to shared string, without interning, reduce GCs
- */
- private String getUniqueString(String s) {
- String u = uniqueStrings.get(s);
- if (null == u) {
- u = s;
- uniqueStrings.put(s, s);
- }
- return u;
- }
-
- /**
- * Serializes all of the out-of-typesystem elements that were recorded
- * in the XmiSerializationSharedData during the last deserialization.
- */
- private void serializeOutOfTypeSystemElements() throws SAXException {
- if (this.marker != null)
- return;
- if (this.sharedData == null)
- return;
- Iterator<OotsElementData> it = this.sharedData.getOutOfTypeSystemElements().iterator();
- while (it.hasNext()) {
- OotsElementData oed = it.next();
- workAttrs.clear();
- // Add ID attribute
- addAttribute(workAttrs, ID_ATTR_NAME, oed.xmiId);
-
- // Add other attributes
- Iterator<XmlAttribute> attrIt = oed.attributes.iterator();
- while (attrIt.hasNext()) {
- XmlAttribute attr = (XmlAttribute) attrIt.next();
- addAttribute(workAttrs, attr.name, attr.value);
- }
-
- // serialize element
- startElement(oed.elementName, workAttrs, oed.childElements.size());
-
- //serialize features encoded as child elements
- Iterator<XmlElementNameAndContents> childElemIt = oed.childElements.iterator();
- while (childElemIt.hasNext()) {
- XmlElementNameAndContents child = childElemIt.next();
- workAttrs.clear();
- Iterator attrIter = child.attributes.iterator();
- while (attrIter.hasNext()) {
- XmlAttribute attr =(XmlAttribute)attrIter.next();
- addAttribute(workAttrs, attr.name, attr.value);
- }
-
- if (child.contents != null) {
- startElement(child.name, workAttrs, 1);
- addText(child.contents);
- }
- else {
- startElement(child.name, workAttrs, 0);
- }
- endElement(child.name);
- }
-
- endElement(oed.elementName);
- }
- }
- }
-
- public static final String XMLNS_NS_URI = "http://www.w3.org/2000/xmlns/";
-
- public static final String XMI_NS_URI = "http://www.omg.org/XMI";
-
- public static final String XSI_NS_URI = "http://www.w3.org/2001/XMLSchema-instance";
-
- public static final String XMI_NS_PREFIX = "xmi";
-
- public static final String XMI_TAG_LOCAL_NAME = "XMI";
-
- public static final String XMI_TAG_QNAME = "xmi:XMI";
-
- public static final XmlElementName XMI_TAG = new XmlElementName(XMI_NS_URI, XMI_TAG_LOCAL_NAME,
- XMI_TAG_QNAME);
-
- public static final String INDEXED_ATTR_NAME = "_indexed";
-
- public static final String ID_ATTR_NAME = "xmi:id";
-
- public static final String XMI_VERSION_LOCAL_NAME = "version";
-
- public static final String XMI_VERSION_QNAME = "xmi:version";
-
- public static final String XMI_VERSION_VALUE = "2.0";
-
- /** Namespace URI to use for UIMA types that have no namespace (the "default pacakge" in Java) */
- public static final String DEFAULT_NAMESPACE_URI = "http:///uima/noNamespace.ecore";
-
- private TypeSystemImpl filterTypeSystem;
-
- // UIMA logger, to which we may write warnings
- private Logger logger;
-
- private Map<String, String> nsUriToSchemaLocationMap = null;
-
- /**
- * Creates a new XmiCasSerializer.
- *
- * @param ts
- * the TypeSystem of CASes that will be serialized. If any CAS that is later passed to
- * the <code>serialize</code> method that contains types and features that are not in
- * this typesystem, the serialization will not contain instances of those types or values
- * for those features. So this can be used to filter the results of serialization.
- * @param nsUriToSchemaLocationMap
- * Map if supplied, this map is used to generate a "schemaLocation" attribute in the XMI
- * output. This argument must be a map from namespace URIs to the schema location for
- * that namespace URI.
- */
- public XmiCasSerializer(TypeSystem ts, Map<String, String> nsUriToSchemaLocationMap) {
- super();
- // System.out.println("Creating serializer for type system.");
- this.filterTypeSystem = (TypeSystemImpl) ts;
- this.nsUriToSchemaLocationMap = nsUriToSchemaLocationMap;
- this.logger = UIMAFramework.getLogger(XmiCasSerializer.class);
- }
-
- /**
- * Creates a new XmiCasSerializer.
- *
- * @param ts
- * the TypeSystem of CASes that will be serialized. If any CAS that is later passed to
- * the <code>serialize</code> method that contains types and features that are not in
- * this typesystem, the serialization will not contain instances of those types or values
- * for those features. So this can be used to filter the results of serialization.
- * A null value indicates that all types and features will be serialized.
- */
- public XmiCasSerializer(TypeSystem ts) {
- this(ts, (Map) null);
- }
-
- /**
- * Creates a new XmiCasSerializer.
- *
- * @param ts
- * the TypeSystem of CASes that will be serialized. If any CAS that is later passed to
- * the <code>serialize</code> method that contains types and features that are not in
- * this typesystem, the serialization will not contain instances of those types or values
- * for those features. So this can be used to filter the results of serialization.
- * @param uimaContext
- * not used
- * @param nsUriToSchemaLocationMap
- * Map if supplied, this map is used to generate a "schemaLocation" attribute in the XMI
- * output. This argument must be a map from namespace URIs to the schema location for
- * that namespace URI.
- *
- * @deprecated Use {@link #XmiCasSerializer(TypeSystem, Map)} instead. The UimaContext reference
- * is never used by this implementation.
- */
- @Deprecated
- public XmiCasSerializer(TypeSystem ts, UimaContext uimaContext, Map<String, String> nsUriToSchemaLocationMap) {
- this(ts, nsUriToSchemaLocationMap);
- }
-
- /**
- * Creates a new XmiCasSerializer.
- *
- * @param ts
- * the TypeSystem of CASes that will be serialized. If any CAS that is later passed to
- * the <code>serialize</code> method that contains types and features that are not in
- * this typesystem, the serialization will not contain instances of those types or values
- * for those features. So this can be used to filter the results of serialization.
- * @param uimaContext
- * not used
- *
- * @deprecated Use {@link #XmiCasSerializer(TypeSystem)} instead. The UimaContext reference is
- * never used by this implementation.
- */
- @Deprecated
- public XmiCasSerializer(TypeSystem ts, UimaContext uimaContext) {
- this(ts);
- }
-
- /**
- * Write the CAS data to a SAX content handler.
- *
- * @param cas
- * The CAS to be serialized.
- * @param contentHandler
- * The SAX content handler the data is written to. should be inserted into the XCAS
- * output
- *
- * @throws SAXException if there was a SAX exception
- */
- public void serialize(CAS cas, ContentHandler contentHandler) throws SAXException {
- this.serialize(cas, contentHandler, null);
- }
+ nsUri = getUniqueString(new String(sb));
+
+// nsUri = "http:///" + namespace.replace('.', '/') + ".ecore";
+ }
+ // convert short name to shared string, without interning, reduce GCs
+ shortName = getUniqueString(shortName);
- /**
- * Write the CAS data to a SAX content handler.
- *
- * @param cas
- * The CAS to be serialized.
- * @param contentHandler
- * The SAX content handler the data is written to. should be inserted into the XCAS
- * output
- * @param errorHandler the SAX Error Handler to use
- *
- * @throws SAXException if there was a SAX exception
- */
- public void serialize(CAS cas, ContentHandler contentHandler, ErrorHandler errorHandler)
- throws SAXException {
- contentHandler.startDocument();
- XmiCasDocSerializer ser = new XmiCasDocSerializer(contentHandler, errorHandler, ((CASImpl) cas)
- .getBaseCAS(), null, null);
- ser.serialize();
- contentHandler.endDocument();
- }
+ // determine what namespace prefix to use
+ String prefix = (String) nsUriToPrefixMap.get(nsUri);
+ if (prefix == null) {
+ if (lastDotIndex != -1) { // have namespace
+ int secondLastDotIndex = uimaTypeName.lastIndexOf('.', lastDotIndex-1);
+ prefix = uimaTypeName.substring(secondLastDotIndex + 1, lastDotIndex);
+ } else {
+ prefix = "noNamespace";
+ }
+ // make sure this prefix hasn't already been used for some other namespace
+ if (nsPrefixesUsed.contains(prefix)) {
+ String basePrefix = prefix;
+ int num = 2;
+ while (nsPrefixesUsed.contains(basePrefix + num)) {
+ num++;
+ }
+ prefix = basePrefix + num;
+ }
+ nsUriToPrefixMap.put(nsUri, prefix);
+ nsPrefixesUsed.add(prefix);
+ }
- /**
- * Write the CAS data to a SAX content handler.
- *
- * @param cas
- * The CAS to be serialized.
- * @param contentHandler
- * The SAX content handler the data is written to. should be inserted into the XCAS
- * output
- * @param sharedData
- * data structure used to allow the XmiCasSerializer and XmiCasDeserializer to share
- * information.
- * @param errorHandler the SAX Error Handler to use
- *
- * @throws SAXException if there was a SAX exception
- */
- public void serialize(CAS cas, ContentHandler contentHandler, ErrorHandler errorHandler,
- XmiSerializationSharedData sharedData) throws SAXException {
- contentHandler.startDocument();
- XmiCasDocSerializer ser = new XmiCasDocSerializer(contentHandler, errorHandler, ((CASImpl) cas)
- .getBaseCAS(), sharedData, (MarkerImpl) null);
- ser.serialize();
- contentHandler.endDocument();
- }
-
- /**
- * Write the CAS data to a SAX content handler.
- *
- * @param cas
- * The CAS to be serialized.
- * @param contentHandler
- * The SAX content handler the data is written to. should be inserted into the XCAS
- * output
- * @param errorHandler the SAX Error Handler to use
- * @param sharedData
- * data structure used to allow the XmiCasSerializer and XmiCasDeserializer to share
- * information.
- * @param marker
- * an object used to filter the FSs and Views to determine if these were created after
- * the mark was set. Used to serialize a Delta CAS consisting of only new FSs and views and
- * preexisting FSs and Views that have been modified.
- *
- * @throws SAXException if there was a SAX exception
- */
- public void serialize(CAS cas, ContentHandler contentHandler, ErrorHandler errorHandler,
- XmiSerializationSharedData sharedData, Marker marker) throws SAXException {
-
- contentHandler.startDocument();
- XmiCasDocSerializer ser = new XmiCasDocSerializer(contentHandler, errorHandler, ((CASImpl) cas)
- .getBaseCAS(), sharedData, (MarkerImpl) marker);
- ser.serialize();
- contentHandler.endDocument();
- }
+ return new XmlElementName(nsUri, shortName, getUniqueString(prefix + ':' + shortName));
+ }
- /**
- * Serializes a CAS to an XMI stream.
- *
- * @param aCAS
- * CAS to serialize.
- * @param aStream
- * output stream to which to write the XMI document
- *
- * @throws SAXException
- * if a problem occurs during XMI serialization
- */
- public static void serialize(CAS aCAS, OutputStream aStream) throws SAXException {
- serialize(aCAS, null, aStream, false, null);
- }
+ /*
+ * convert to shared string, without interning, reduce GCs
+ */
+ private String getUniqueString(String s) {
+ String u = uniqueStrings.get(s);
+ if (null == u) {
+ u = s;
+ uniqueStrings.put(s, s);
+ }
+ return u;
+ }
+
+ /**
+ * Serializes all of the out-of-typesystem elements that were recorded
+ * in the XmiSerializationSharedData during the last deserialization.
+ */
+ private void serializeOutOfTypeSystemElements() throws SAXException {
+ if (this.marker != null)
+ return;
+ if (this.sharedData == null)
+ return;
+ Iterator<OotsElementData> it = this.sharedData.getOutOfTypeSystemElements().iterator();
+ while (it.hasNext()) {
+ OotsElementData oed = it.next();
+ workAttrs.clear();
+ // Add ID attribute
+ addAttribute(workAttrs, ID_ATTR_NAME, oed.xmiId);
- /**
- * Serializes a CAS to an XMI stream. Allows a TypeSystem to be specified, to which the produced
- * XMI will conform. Any types or features not in the target type system will not be serialized.
- *
- * @param aCAS
- * CAS to serialize.
- * @param aTargetTypeSystem
- * type system to which the produced XMI will conform. Any types or features not in the
- * target type system will not be serialized. A null value indicates that all types and features
- * will be serialized.
- * @param aStream
- * output stream to which to write the XMI document
- *
- * @throws SAXException
- * if a problem occurs during XMI serialization
- */
- public static void serialize(CAS aCAS, TypeSystem aTargetTypeSystem, OutputStream aStream)
- throws SAXException {
- serialize(aCAS, aTargetTypeSystem, aStream, false, null);
+ // Add other attributes
+ Iterator<XmlAttribute> attrIt = oed.attributes.iterator();
+ while (attrIt.hasNext()) {
+ XmlAttribute attr = (XmlAttribute) attrIt.next();
+ addAttribute(workAttrs, attr.name, attr.value);
+ }
+
+ // serialize element
+ startElement(oed.elementName, workAttrs, oed.childElements.size());
+
+ //serialize features encoded as child elements
+ Iterator<XmlElementNameAndContents> childElemIt = oed.childElements.iterator();
+ while (childElemIt.hasNext()) {
+ XmlElementNameAndContents child = childElemIt.next();
+ workAttrs.clear();
+ Iterator attrIter = child.attributes.iterator();
+ while (attrIter.hasNext()) {
+ XmlAttribute attr =(XmlAttribute)attrIter.next();
+ addAttribute(workAttrs, attr.name, attr.value);
+ }
+
+ if (child.contents != null) {
+ startElement(child.name, workAttrs, 1);
+ addText(child.contents);
+ }
+ else {
+ startElement(child.name, workAttrs, 0);
+ }
+ endElement(child.name);
+ }
+
+ endElement(oed.elementName);
+ }
+ }
}
-
- /**
- * Serializes a CAS to an XMI stream. This version of this method allows many options to be configured.
- *
- * @param aCAS
- * CAS to serialize.
- * @param aTargetTypeSystem
- * type system to which the produced XMI will conform. Any types or features not in the
- * target type system will not be serialized. A null value indicates that all types and features
- * will be serialized.
- * @param aStream
- * output stream to which to write the XMI document
- * @param aPrettyPrint
- * if true the XML output will be formatted with newlines and indenting. If false it will be unformatted.
- * @param aSharedData
- * an optional container for data that is shared between the {@link XmiCasSerializer} and the {@link XmiCasDeserializer}.
- * See the JavaDocs for {@link XmiSerializationSharedData} for details.
- *
- * @throws SAXException
- * if a problem occurs during XMI serialization
- */
- public static void serialize(CAS aCAS, TypeSystem aTargetTypeSystem, OutputStream aStream, boolean aPrettyPrint,
- XmiSerializationSharedData aSharedData)
- throws SAXException {
- XmiCasSerializer xmiCasSerializer = new XmiCasSerializer(aTargetTypeSystem);
- XMLSerializer sax2xml = new XMLSerializer(aStream, aPrettyPrint);
- xmiCasSerializer.serialize(aCAS, sax2xml.getContentHandler(), null, aSharedData, null);
- }
-
- /**
- * Serializes a Delta CAS to an XMI stream. This version of this method allows many options to be configured.
- *
- *
- * @param aCAS
- * CAS to serialize.
- * @param aTargetTypeSystem
- * type system to which the produced XMI will conform. Any types or features not in the
- * target type system will not be serialized. A null value indicates that all types and features
- * will be serialized.
- * @param aStream
- * output stream to which to write the XMI document
- * @param aPrettyPrint
- * if true the XML output will be formatted with newlines and indenting. If false it will be unformatted.
- * @param aSharedData
- * an optional container for data that is shared between the {@link XmiCasSerializer} and the {@link XmiCasDeserializer}.
- * See the JavaDocs for {@link XmiSerializationSharedData} for details.
- * @param aMarker
- * an optional object that is used to filter and serialize a Delta CAS containing only
- * those FSs and Views created after Marker was set and preexisting FSs and views that were modified.
- * See the JavaDocs for {@link Marker} for details.
- * @throws SAXException
- * if a problem occurs during XMI serialization
- */
- public static void serialize(CAS aCAS, TypeSystem aTargetTypeSystem, OutputStream aStream, boolean aPrettyPrint,
- XmiSerializationSharedData aSharedData, Marker aMarker)
- throws SAXException {
- XmiCasSerializer xmiCasSerializer = new XmiCasSerializer(aTargetTypeSystem);
- XMLSerializer sax2xml = new XMLSerializer(aStream, aPrettyPrint);
- xmiCasSerializer.serialize(aCAS, sax2xml.getContentHandler(), null, aSharedData, aMarker);
- }
+
}