Java Code Examples for org.apache.uima.util.XMLSerializer#setOutputProperty()

The following examples show how to use org.apache.uima.util.XMLSerializer#setOutputProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CpeBuilder.java    From uima-uimafit with Apache License 2.0 6 votes vote down vote up
/**
 * Writes a temporary file containing a XML descriptor of the given resource. Returns the file.
 * 
 * @param resource
 *          A resource specifier that should we materialized.
 * @return The file containing the XML representation of the given resource.
 */
private static File materializeDescriptor(ResourceSpecifier resource) throws IOException,
        SAXException {
  File tempDesc = File.createTempFile("desc", ".xml");
  tempDesc.deleteOnExit();

  // Write the descriptor using XML 1.1 to allow a wider range of characters for parameter values
  try (OutputStream os = Files.newOutputStream(tempDesc.toPath())) {
    XMLSerializer sax2xml = new XMLSerializer(os, true);
    sax2xml.setOutputProperty(OutputKeys.VERSION, "1.1");
    ContentHandler contentHandler = sax2xml.getContentHandler();
    contentHandler.startDocument();
    resource.toXML(sax2xml.getContentHandler(), true);
    contentHandler.endDocument();
  }

  return tempDesc;
}
 
Example 2
Source File: AnalysisEngine_implTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private void manyDelegatesCommon() throws Exception {
  // Test that an aggregate can be copied preserving all comments and ordering of delegates
  XMLParser.ParsingOptions parsingOptions = new XMLParser.ParsingOptions(false);
  parsingOptions.preserveComments = true;
  XMLParser parser = UIMAFramework.getXMLParser();
  File inFile = JUnitExtension.getFile("TextAnalysisEngineImplTest/AggregateWithManyDelegates.xml");
  AnalysisEngineDescription desc = parser.parseAnalysisEngineDescription(new XMLInputSource(inFile), parsingOptions);

  // Write out descriptor
  File cloneFile = new File(inFile.getParentFile(), "CopyOfAggregateWithManyDelegates.xml");
  try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(cloneFile))) {
    XMLSerializer xmlSerializer = new XMLSerializer(false);
    xmlSerializer.setOutputStream(os);
    // set the amount to a value which will show up if used
    // indent should not be used because we're using a parser mode which preserves
    // comments and ignorable white space.
    // NOTE: Saxon appears to force the indent to be 3 - which is what the input file now uses.
    xmlSerializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    ContentHandler contentHandler = xmlSerializer.getContentHandler();
    contentHandler.startDocument();
    desc.toXML(contentHandler, true);
    contentHandler.endDocument();
  }
  
  String inXml = FileCompare.file2String(inFile);
  String cloneXml = FileCompare.file2String(cloneFile);
  XMLAssert.assertXMLEqual(inXml,  cloneXml);
  // When building from a source distribution the descriptor may not have
  // appropriate line-ends so compute the length as if always 1 byte.
  int diff = fileLength(cloneFile) - fileLength(inFile);
  // One platform inserts a blank line and a final newline, so don't insist on perfection
  // NOTE:  This fails with Saxon as it omits the xmlns attribute (why?) and omits the newlines between adjacent comments.
  // It also produces many differences in indentation if the input is not indented by 3
  assertTrue("File size changed by "+diff+" should be no more than 2", diff >= -2 && diff <= 2);

  // Initialize all delegates and check the initialization order (should be declaration order)
  TestAnnotator2.allContexts = "";
  UIMAFramework.produceAnalysisEngine(desc);
  assertEquals("D/C/B/A/F/E/", TestAnnotator2.allContexts);
  
  // Check that copying aggregate preserved the order of the delegates
  desc = parser.parseAnalysisEngineDescription(new XMLInputSource(cloneFile), parsingOptions);
  TestAnnotator2.allContexts = "";
  UIMAFramework.produceAnalysisEngine(desc);
  assertEquals("D/C/B/A/F/E/", TestAnnotator2.allContexts);
  cloneFile.delete();
}
 
Example 3
Source File: XmiCasSerializer.java    From uima-uimaj with Apache License 2.0 3 votes vote down vote up
/**
 * 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.
 * @param useXml_1_1
 *          if true, the output serializer is set with the OutputKeys.VERSION to "1.1".         
 * @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, boolean useXml_1_1) 
        throws SAXException {
  XmiCasSerializer xmiCasSerializer = new XmiCasSerializer(aTargetTypeSystem);
  XMLSerializer sax2xml = new XMLSerializer(aStream, aPrettyPrint);
  if (useXml_1_1) {
    sax2xml.setOutputProperty(OutputKeys.VERSION,"1.1");
  }
  xmiCasSerializer.serialize(aCAS, sax2xml.getContentHandler(), null, aSharedData, aMarker);
}
 
Example 4
Source File: XCASSerializer.java    From uima-uimaj with Apache License 2.0 3 votes vote down vote up
/**
 * Serializes an XCAS to a stream.
 * 
 * @param aCAS
 *          CAS to serialize.
 * @param aStream
 *          output stream to which to write the XCAS XML document
 * @param isFormattedOutput
 *          if true the XCAS will be serialized formatted   * 
 * @param useXml_1_1
 *          if true, the output serializer is set with the OutputKeys.VERSION to "1.1".         
 * @throws SAXException
 *           if a problem occurs during XCAS serialization
 * @throws IOException
 *           if an I/O failure occurs
 */
public static void serialize(CAS aCAS, OutputStream aStream, boolean isFormattedOutput, boolean useXml_1_1)
        throws SAXException, IOException {
  XCASSerializer xcasSerializer = new XCASSerializer(aCAS.getTypeSystem());
  XMLSerializer sax2xml = new XMLSerializer(aStream, isFormattedOutput);
  if (useXml_1_1) {
    sax2xml.setOutputProperty(OutputKeys.VERSION,"1.1");
  }
  xcasSerializer.serialize(aCAS, sax2xml.getContentHandler());
}