Package org.apache.uima.util

Examples of org.apache.uima.util.XMLSerializer$CharacterValidatingContentHandler


      assertTrue(tIndex.size() == 2); // document annot and this one
      assertTrue(t2Index.size() == 2); // ditto

      // serialize
      StringWriter sw = new StringWriter();
      XMLSerializer xmlSer = new XMLSerializer(sw, false);
      XmiCasSerializer xmiSer = new XmiCasSerializer(cas.getTypeSystem());
      xmiSer.serialize(cas, xmlSer.getContentHandler());
      String xml = sw.getBuffer().toString();

      // deserialize into another CAS (repeat twice to check it still works after reset)
      CAS newCas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
              new FsIndexDescription[0]);
View Full Code Here


              .getTypeSystem();

      // reserialize as XMI, filtering out anything that doesn't fit in the
      // partialTypeSystem
      StringWriter sw = new StringWriter();
      XMLSerializer xmlSer = new XMLSerializer(sw, false);
      XmiCasSerializer xmiSer = new XmiCasSerializer(partialTypeSystem);
      xmiSer.serialize(cas, xmlSer.getContentHandler());
      String xml = sw.getBuffer().toString();
      // System.out.println(xml);

      // deserialize into another CAS (which has the whole type system)
      CAS cas2 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(), indexes);
View Full Code Here

    intArrayFS.set(4, 5);
    cas.getIndexRepository().addFS(intArrayFS);

    // serialize the CAS
    StringWriter sw = new StringWriter();
    XMLSerializer xmlSer = new XMLSerializer(sw, false);
    XmiCasSerializer xmiSer = new XmiCasSerializer(cas.getTypeSystem());
    xmiSer.serialize(cas, xmlSer.getContentHandler());
    String xml = sw.getBuffer().toString();

    // deserialize into another CAS
    CAS cas2 = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
            new FsIndexDescription[0]);

    XmiCasDeserializer deser2 = new XmiCasDeserializer(cas2.getTypeSystem());
    ContentHandler deserHandler2 = deser2.getXmiCasHandler(cas2);
    SAXParserFactory fact = SAXParserFactory.newInstance();
    SAXParser parser = fact.newSAXParser();
    XMLReader xmlReader = parser.getXMLReader();
    xmlReader.setContentHandler(deserHandler2);
    xmlReader.parse(new InputSource(new StringReader(xml)));

    //test that index is correctly populated
    Type intArrayType = cas2.getTypeSystem().getType(CAS.TYPE_NAME_INTEGER_ARRAY);
    Iterator<FeatureStructure> iter = cas2.getIndexRepository().getAllIndexedFS(intArrayType);
    assertTrue(iter.hasNext());
    IntArrayFS intArrayFS2 = (IntArrayFS)iter.next();
    assertFalse(iter.hasNext());
    assertEquals(5, intArrayFS2.size());
    assertEquals(1, intArrayFS2.get(0));
    assertEquals(2, intArrayFS2.get(1));
    assertEquals(3, intArrayFS2.get(2));
    assertEquals(4, intArrayFS2.get(3));
    assertEquals(5, intArrayFS2.get(4));

    // test that serializing the new CAS produces the same XML
    sw = new StringWriter();
    xmlSer = new XMLSerializer(sw, false);
    xmiSer = new XmiCasSerializer(cas2.getTypeSystem());
    xmiSer.serialize(cas2, xmlSer.getContentHandler());
    String xml2 = sw.getBuffer().toString();   
    assertTrue(xml2.equals(xml));
  }
View Full Code Here

    assertTrue(CAS.NAME_DEFAULT_SOFA.equals(v1cas.getSofa().getSofaID()));
    assertTrue(v1cas.getDocumentText().equals("some text for the default text sofa."));

    // reserialize as XMI
    StringWriter sw = new StringWriter();
    XMLSerializer xmlSer = new XMLSerializer(sw, false);
    XmiCasSerializer xmiSer = new XmiCasSerializer(v1cas.getTypeSystem());
    xmiSer.serialize(v1cas, xmlSer.getContentHandler());
    String xml = sw.getBuffer().toString();

    cas.reset();

    // deserialize into another CAS
View Full Code Here

  public void testInvalidCharsInDocumentText() throws Exception {
    CAS cas = CasCreationUtils.createCas(this.typeSystemDesc, null, null);
    char badChar = 0x1A;
    cas.setDocumentText("Text with bad char: " + badChar);
    OutputStream out = new FileOutputStream(this.outputFile);
    XMLSerializer xmlSerializer = new XMLSerializer(out);
    XmiCasSerializer xmiCasSerializer = new XmiCasSerializer(cas.getTypeSystem());
    boolean caughtException = false;
    try {
      xmiCasSerializer.serialize(cas, xmlSerializer.getContentHandler());
    } catch (SAXParseException e) {
      caughtException = true;
    }
    out.close();
    assertTrue("XMI serialization of document text with bad XML 1.0 char should throw exception",
        caughtException);
   
    //but when XML 1.1 output is being generated, don't fail on control chracters which are valid in 1.1.
    if (XML1_1_SUPPORTED) {
      out = new FileOutputStream(this.outputFile);
      try {
        XMLSerializer xml11Serializer = new XMLSerializer(out);
        xml11Serializer.setOutputProperty(OutputKeys.VERSION,"1.1");
        xmiCasSerializer.serialize(cas, xml11Serializer.getContentHandler());
      }
      finally {
        out.close();
      }
    }
View Full Code Here

  public void testInvalidCharsInFeatureValue() throws Exception {
    CAS cas = CasCreationUtils.createCas(this.typeSystemDesc, null, null);
    char badChar = 0x1A;
    cas.setDocumentLanguage("a" + badChar);
    OutputStream out = new FileOutputStream(this.outputFile);
    XMLSerializer xmlSerializer = new XMLSerializer(out);
    XmiCasSerializer xmiCasSerializer = new XmiCasSerializer(cas.getTypeSystem());
    boolean caughtException = false;
    try {
      xmiCasSerializer.serialize(cas, xmlSerializer.getContentHandler());
    } catch (SAXParseException e) {
      caughtException = true;
    }
    out.close();
    assertTrue("XMI serialization of document text with bad XML 1.0 char should throw exception",
        caughtException);
   
    //but when XML 1.1 output is being generated, don't fail on control characters which are valid in 1.1.
    if (XML1_1_SUPPORTED) {
      out = new FileOutputStream(this.outputFile);
      try {
        XMLSerializer xml11Serializer = new XMLSerializer(out);
        xml11Serializer.setOutputProperty(OutputKeys.VERSION,"1.1");
        xmiCasSerializer.serialize(cas, xml11Serializer.getContentHandler());
      }
      finally {
        out.close();
      }
    }
View Full Code Here

  public String prettyPrintModel() {
    StringWriter writer = new StringWriter();
    String parsedText = null;
    try {
      XMLSerializer xmlSerializer = new XMLSerializer(true);
      xmlSerializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.valueOf(
            MultiPageEditorContributor.getXMLindent()).toString());
      xmlSerializer.setIndent(true);
      xmlSerializer.setWriter(writer);
      ContentHandler contentHandler = xmlSerializer.getContentHandler();
      contentHandler.startDocument();
      XMLizable trueDescriptor = getTrueDescriptor();
      if (trueDescriptor instanceof AnalysisEngineDescription) {
        AnalysisEngineDescription aed = (AnalysisEngineDescription) trueDescriptor;
        aed.toXML(contentHandler, true, true);
View Full Code Here

    try {
      // write XMI
      out = new FileOutputStream(name);
      XmiCasSerializer ser = new XmiCasSerializer(aCas.getTypeSystem());
      XMLSerializer xmlSer = new XMLSerializer(out, false);
      ser.serialize(aCas, xmlSer.getContentHandler());
    } finally {
      if (out != null) {
        out.close();
      }
    }
View Full Code Here

  public void serializeToXCAS(OutputStream stream, CAS aCAS, String encoding,
          TypeSystem typeSystem, OutOfTypeSystemData otsd) throws IOException, SAXException {

    if (typeSystem == null)
      typeSystem = aCAS.getTypeSystem();
    XMLSerializer xmlSer = new XMLSerializer(stream, false);
    if (encoding != null)
      xmlSer.setOutputProperty(OutputKeys.ENCODING, encoding);
    XCASSerializer ser = new XCASSerializer(typeSystem);
    ser.serialize(aCAS, xmlSer.getContentHandler(), false, otsd);
  }
View Full Code Here

  public void serializeToXMI(OutputStream stream, CAS aCAS, String encoding, TypeSystem typeSystem,
          OutOfTypeSystemData otsd) throws IOException, SAXException {

    if (typeSystem == null)
      typeSystem = aCAS.getTypeSystem();
    XMLSerializer xmlSer = new XMLSerializer(stream, false);
    if (encoding != null)
      xmlSer.setOutputProperty(OutputKeys.ENCODING, encoding);

    XmiCasSerializer ser = new XmiCasSerializer(typeSystem);

    ser.serialize(aCAS, xmlSer.getContentHandler());
  }
View Full Code Here

TOP

Related Classes of org.apache.uima.util.XMLSerializer$CharacterValidatingContentHandler

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.