Java Code Examples for org.xml.sax.XMLReader#parse()

The following examples show how to use org.xml.sax.XMLReader#parse() . 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: XSSFUnmarshaller.java    From poiji with MIT License 6 votes vote down vote up
private <T> void processSheet(StylesTable styles,
                              XMLReader reader,
                              ReadOnlySharedStringsTable readOnlySharedStringsTable,
                              Class<T> type,
                              InputStream sheetInputStream,
                              Consumer<? super T> consumer) {

    DataFormatter formatter = new DataFormatter();
    InputSource sheetSource = new InputSource(sheetInputStream);
    try {
        PoijiHandler<T> poijiHandler = new PoijiHandler<>(type, options, consumer);
        ContentHandler contentHandler
                = new XSSFSheetXMLPoijiHandler(styles,
                null,
                readOnlySharedStringsTable,
                poijiHandler,
                formatter,
                false,
                options);
        reader.setContentHandler(contentHandler);
        reader.parse(sheetSource);
    } catch (SAXException | IOException e) {
        IOUtils.closeQuietly(sheetInputStream);
        throw new PoijiException("Problem occurred while reading data", e);
    }
}
 
Example 2
Source File: TypeInfoProviderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test() throws SAXException, ParserConfigurationException, IOException {

    SchemaFactory sf = SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File(XML_DIR + "shiporder11.xsd"));
    validatorHandler = schema.newValidatorHandler();
    MyDefaultHandler myDefaultHandler = new MyDefaultHandler();
    validatorHandler.setContentHandler(myDefaultHandler);

    InputSource is = new InputSource(filenameToURL(XML_DIR + "shiporder11.xml"));

    SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    parserFactory.setNamespaceAware(true);
    XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader();
    xmlReader.setContentHandler(validatorHandler);
    xmlReader.parse(is);

}
 
Example 3
Source File: ATOMContentHandlerTests.java    From OpenESPI-DataCustodian-java with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore
public void processEnty() throws Exception {
	JAXBContext context = marshaller.getJaxbContext();

	SAXParserFactory factory = SAXParserFactory.newInstance();
	factory.setNamespaceAware(true);
	XMLReader reader = factory.newSAXParser().getXMLReader();
	// EntryProcessorServiceImpl procssor =
	// mock(EntryProcessorServiceImpl.class);
	ATOMContentHandler atomContentHandler = new ATOMContentHandler(context,
			entryProcessorService);

	reader.setContentHandler(atomContentHandler);

	reader.parse(new InputSource(FixtureFactory
			.newUsagePointInputStream(UUID.randomUUID())));

	// verify(procssor).process(any(EntryType.class));
}
 
Example 4
Source File: SAXBufferCreator.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public MutableXMLStreamBuffer create(XMLReader reader, InputStream in, String systemId) throws IOException, SAXException {
    if (_buffer == null) {
        createBuffer();
    }
    _buffer.setSystemId(systemId);
    reader.setContentHandler(this);
    reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, this);

    try {
        setHasInternedStrings(reader.getFeature(Features.STRING_INTERNING_FEATURE));
    } catch (SAXException e) {
    }


    if (systemId != null) {
        InputSource s = new InputSource(systemId);
        s.setByteStream(in);
        reader.parse(s);
    } else {
        reader.parse(new InputSource(in));
    }

    return getXMLStreamBuffer();
}
 
Example 5
Source File: JPZIO.java    From shortyz with GNU General Public License v3.0 6 votes vote down vote up
public static Puzzle readPuzzle(InputStream is) {
		Puzzle puz = new Puzzle();
		SAXParserFactory factory = SAXParserFactory.newInstance();

		try {
			SAXParser parser = factory.newSAXParser();

//			 parser.setProperty("http://xml.org/sax/features/validation",
//			 false);
			XMLReader xr = parser.getXMLReader();
			xr.setContentHandler(new JPZXMLParser(puz));
			xr.parse(new InputSource(unzipOrPassthrough(is)));

			puz.setVersion(IO.VERSION_STRING);

		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("Unable to parse XML file: " + e.getMessage());
			throw new RuntimeException(e);
		}
		return puz;
	}
 
Example 6
Source File: ExportServlet.java    From blog-codes with Apache License 2.0 5 votes vote down vote up
/**
 * Renders the XML to the given canvas.
 */
protected void renderXml(String xml, mxICanvas2D canvas) throws SAXException, ParserConfigurationException, IOException
{
	XMLReader reader = parserFactory.newSAXParser().getXMLReader();
	reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
	reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
	reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
	reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
	reader.setContentHandler(new mxSaxOutputHandler(canvas));
	reader.parse(new InputSource(new StringReader(xml)));
}
 
Example 7
Source File: SonosXMLParser.java    From openhab1-addons with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param xml
 * @return a list of Entrys from the given xml string.
 * @throws IOException
 * @throws SAXException
 */
public static List<SonosEntry> getEntriesFromString(String xml) throws SAXException {
    XMLReader reader = XMLReaderFactory.createXMLReader();
    EntryHandler handler = new EntryHandler();
    reader.setContentHandler(handler);
    try {
        reader.parse(new InputSource(new StringReader(xml)));
    } catch (IOException e) {
        logger.error("Could not parse Entries from String {}", xml);
    }
    return handler.getArtists();
}
 
Example 8
Source File: Parser.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public GraphDocument parse(XMLReader reader, InputSource source, XMLParser.ParseMonitor monitor) throws SAXException {
    reader.setContentHandler(new XMLParser(xmlDocument, monitor));
    try {
        reader.parse(source);
    } catch (IOException ex) {
        throw new SAXException(ex);
    }

    return topHandler.getObject();
}
 
Example 9
Source File: HftpFileSystem.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Connect to the name node and get content summary.
 * @param path The path
 * @return The content summary for the path.
 * @throws IOException
 */
private ContentSummary getContentSummary(String path) throws IOException {
  final HttpURLConnection connection = openConnection(
      "/contentSummary" + ServletUtil.encodePath(path),
      "ugi=" + getEncodedUgiParameter());
  InputStream in = null;
  try {
    in = connection.getInputStream();

    final XMLReader xr = XMLReaderFactory.createXMLReader();
    xr.setContentHandler(this);
    xr.parse(new InputSource(in));
  } catch(FileNotFoundException fnfe) {
    //the server may not support getContentSummary
    return null;
  } catch(SAXException saxe) {
    final Exception embedded = saxe.getException();
    if (embedded != null && embedded instanceof IOException) {
      throw (IOException)embedded;
    }
    throw new IOException("Invalid xml format", saxe);
  } finally {
    if (in != null) {
      in.close();
    }
    connection.disconnect();
  }
  return contentsummary;
}
 
Example 10
Source File: SchemaLessProperties.java    From exificient with MIT License 5 votes vote down vote up
private void encodeSchemaLessToEXI(OutputStream osEXI) throws Exception {
	// start encoding process
	XMLReader xmlReader = XMLReaderFactory.createXMLReader();

	EXIResult exiResult = new EXIResult(factory);
	exiResult.setOutputStream(osEXI);
	xmlReader.setContentHandler(exiResult.getHandler());

	xmlReader.parse(new InputSource(new StringReader(xml)));
}
 
Example 11
Source File: ExcelReader.java    From excelReader with MIT License 5 votes vote down vote up
/**
 * Parses the content of one sheet using the specified styles and shared-strings tables.
 * 
 * @param styles a {@link StylesTable} object
 * @param sharedStringsTable a {@link ReadOnlySharedStringsTable} object
 * @param sheetInputStream a {@link InputStream} object
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 */
private void readSheet(StylesTable styles, ReadOnlySharedStringsTable sharedStringsTable,
    InputStream sheetInputStream) throws IOException, ParserConfigurationException, SAXException {

  SAXParserFactory saxFactory = SAXParserFactory.newInstance();
  XMLReader sheetParser = saxFactory.newSAXParser().getXMLReader();

  ContentHandler handler =
      new XSSFSheetXMLHandler(styles, sharedStringsTable, sheetContentsHandler, true);

  sheetParser.setContentHandler(handler);
  sheetParser.parse(new InputSource(sheetInputStream));
}
 
Example 12
Source File: Xxe_sax.java    From openrasp-testcases with MIT License 5 votes vote down vote up
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String data = req.getParameter("data");
    if (data != null) {
        try {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            XMLReader reader = parser.getXMLReader();
            reader.setContentHandler(new MyHandler());
            reader.parse(new InputSource(new StringReader(data)));
        } catch (Exception e) {
            resp.getWriter().println(e);
        }
    }
}
 
Example 13
Source File: XmlToExiConverter.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
protected void encode(String fileName) {
    try (InputStream exiIS = FileUtils.openInputStream(getFile(fileName, XML_EXTENSION));
            OutputStream exiOS = FileUtils.openOutputStream(getFile(fileName, EXI_EXTENSION))) {
        EXIResult exiResult = new EXIResult();
        exiResult.setOutputStream(exiOS);
        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setContentHandler(exiResult.getHandler());
        xmlReader.parse(new InputSource(exiIS));
    } catch (Exception e) {
        // TODO: handle exception
    }
}
 
Example 14
Source File: PXSVGLoader.java    From pixate-freestyle-android with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the stream and returns a {@link PXShape} out of it.
 * 
 * @return A {@link PXShapeGroup}
 * @throws PXSVGParseException
 */
protected PXShapeGroup parse(InputStream inputStream) throws PXSVGParseException {
    try {
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        XMLReader reader = parser.getXMLReader();
        reader.setContentHandler(this);
        reader.parse(new InputSource(new InputStreamReader(inputStream, "UTF-8")));
        inputStream.close();
    } catch (Throwable t) {
        throw new PXSVGParseException("Error processing the shape", t);
    }
    return result;
}
 
Example 15
Source File: XCasToCasDataSaxHandlerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
private void _testConversions(CAS aCAS) throws IOException,
        ParserConfigurationException, SAXException, ResourceInitializationException,
        CASRuntimeException {
  // generate XCAS events and pipe them to XCasToCasDataSaxHandler
  CasData casData = new CasDataImpl();
  XCasToCasDataSaxHandler handler = new XCasToCasDataSaxHandler(casData);
  XCASSerializer xcasSer = new XCASSerializer(aCAS.getTypeSystem());
  xcasSer.serialize(aCAS, handler);

  Assert.assertNotNull(casData);
  assertValidCasData(casData, aCAS.getTypeSystem());
  // System.out.println(casData);

  // now generate XCAS from the CasData
  CasDataToXCas generator = new CasDataToXCas();

  StringWriter sw = new StringWriter();
  XMLSerializer xmlSer = new XMLSerializer(sw, false);
  generator.setContentHandler(xmlSer.getContentHandler());

  generator.generateXCas(casData);
  String xml = sw.getBuffer().toString();
  
  //workaround for XML serializatioj problem on Sun Java 1.4
  if (!builtInXmlSerializationSupportsCRs()) {
    xml = xml.replaceAll("&#10;", "&#13;&#10;");  
  }
  
  UIMAFramework.getLogger(XCasToCasDataSaxHandlerTest.class).log(Level.FINE, xml);

  // deserialize back into CAS for comparison
  // CASMgr tcasMgr = CASFactory.createCAS(aCAS.getTypeSystem());
  // tcasMgr.initCASIndexes();
  // tcasMgr.getIndexRepositoryMgr().commit();

  CAS cas2 = CasCreationUtils.createCas(null, aCAS.getTypeSystem(), null);
  XCASDeserializer deser = new XCASDeserializer(cas2.getTypeSystem());
  ContentHandler deserHandler = deser.getXCASHandler(cas2);

  SAXParserFactory fact = SAXParserFactory.newInstance();
  SAXParser parser = fact.newSAXParser();
  XMLReader xmlReader = parser.getXMLReader();
  xmlReader.setContentHandler(deserHandler);
  xmlReader.parse(new InputSource(new StringReader(xml)));

  // CASes should be identical
  CasComparer.assertEquals(aCAS, cas2);
}
 
Example 16
Source File: TypeEmptyTest.java    From exificient with MIT License 4 votes vote down vote up
public void testComplexType04Code() throws Exception {
	// acceptance.xsd
	String schema = "<xsd:schema targetNamespace=\"urn:foo\"\r\n           xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\r\n           xmlns:foo=\"urn:foo\" xmlns:hoo=\"urn:hoo\">\r\n\r\n<!--<xsd:import namespace=\"urn:goo\" schemaLocation=\"acceptance_imported_goo.xsd\"/>\r\n<xsd:import namespace=\"urn:hoo\" schemaLocation=\"acceptance_imported_hoo.xsd\"/>\r\n<xsd:import namespace=\"urn:ioo\" schemaLocation=\"acceptance_imported_ioo.xsd\"/>\r\n<xsd:import schemaLocation=\"acceptance_imported_default.xsd\"/>-->\r\n\r\n<xsd:simpleType name=\"finalString\" final=\"#all\">\r\n  <xsd:restriction base=\"xsd:string\" />\r\n</xsd:simpleType>\r\n\r\n<xsd:complexType name=\"extendedDate\">\r\n  <xsd:simpleContent>\r\n    <xsd:extension base=\"xsd:date\">\r\n      <xsd:attribute ref=\"foo:aA\" />\r\n    </xsd:extension>\r\n  </xsd:simpleContent>\r\n</xsd:complexType>\r\n\r\n<xsd:simpleType name=\"listOfBytes\">\r\n  <xsd:list>\r\n    <xsd:simpleType>\r\n      <xsd:restriction base=\"xsd:byte\"/>\r\n    </xsd:simpleType>\r\n  </xsd:list>\r\n</xsd:simpleType>\r\n\r\n<xsd:element name=\"ANY\" type=\"xsd:anyType\"/>\r\n\r\n<xsd:element name=\"nillable_ANY\" type=\"xsd:anyType\" nillable=\"true\" />\r\n\r\n<xsd:complexType name=\"anyType\">\r\n  <xsd:complexContent mixed=\"true\">\r\n    <xsd:restriction base=\"xsd:anyType\">\r\n      <xsd:sequence>\r\n        <xsd:any namespace=\"##any\" processContents=\"lax\" minOccurs=\"0\" maxOccurs=\"unbounded\" />\r\n      </xsd:sequence>\r\n      <xsd:anyAttribute namespace=\"##any\" processContents=\"lax\" />\r\n    </xsd:restriction>\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"A\">\r\n  <xsd:complexType>\r\n    <xsd:sequence>\r\n      <xsd:sequence>\r\n        <xsd:element ref=\"foo:AB\"/>\r\n        <xsd:element ref=\"foo:AC\" minOccurs=\"0\" maxOccurs=\"2\"/>\r\n      </xsd:sequence>\r\n      <xsd:sequence minOccurs=\"1\"/>\r\n      <xsd:element ref=\"foo:AD\"/>\r\n      <xsd:element ref=\"foo:AE\" minOccurs=\"0\"/>\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:complexType name=\"B\">\r\n  <xsd:sequence>\r\n    <xsd:element ref=\"foo:AB\"/>\r\n    <xsd:element ref=\"foo:AC\" minOccurs=\"0\" maxOccurs=\"2\"/>\r\n    <xsd:element ref=\"foo:AD\" minOccurs=\"0\"/>\r\n  </xsd:sequence>\r\n</xsd:complexType>\r\n\r\n<xsd:complexType name=\"restricted_B\">\r\n  <xsd:complexContent>\r\n    <xsd:restriction base=\"foo:B\">\r\n      <xsd:sequence>\r\n        <xsd:element ref=\"foo:AB\"/>\r\n        <xsd:element ref=\"foo:AC\" minOccurs=\"0\"/>\r\n        <xsd:element ref=\"foo:AD\" minOccurs=\"0\"/>\r\n      </xsd:sequence>\r\n    </xsd:restriction>\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n<xsd:complexType name=\"extended_B\">\r\n  <xsd:complexContent>\r\n    <xsd:extension base=\"foo:B\">\r\n      <xsd:attribute ref=\"foo:aA\" use=\"required\"/>\r\n    </xsd:extension>\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"B\" type=\"foo:B\" nillable=\"false\"/>\r\n\r\n<xsd:element name=\"nillable_B\" type=\"foo:B\" nillable=\"true\" />\r\n\r\n<xsd:complexType name=\"C\">\r\n  <xsd:all>\r\n    <xsd:element ref=\"foo:AB\" minOccurs=\"0\" />\r\n    <xsd:element ref=\"foo:AC\" />\r\n  </xsd:all>\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"C\" type=\"foo:C\"/>\r\n\r\n<xsd:element name=\"AB\" type=\"xsd:anySimpleType\" nillable=\"false\" />\r\n<xsd:element name=\"nillable_AB\" type=\"xsd:anySimpleType\" nillable=\"true\" />\r\n<xsd:element name=\"AC\" type=\"xsd:anySimpleType\"/>\r\n<xsd:element name=\"AD\" type=\"xsd:anySimpleType\"/>\r\n<xsd:element name=\"AE\" type=\"xsd:anySimpleType\"/>\r\n<xsd:element name=\"AF\" type=\"xsd:date\"/>\r\n\r\n<xsd:element name=\"D\">\r\n  <xsd:complexType>\r\n    <xsd:sequence>\r\n      <xsd:sequence>\r\n        <xsd:element name=\"A\" minOccurs=\"0\" maxOccurs=\"2\" />\r\n        <xsd:sequence maxOccurs=\"2\">\r\n          <xsd:element name=\"B\" />\r\n          <xsd:element name=\"C\" minOccurs=\"0\" />\r\n          <xsd:element name=\"D\" minOccurs=\"0\" />\r\n        </xsd:sequence>\r\n      </xsd:sequence>\r\n      <xsd:element name=\"E\" minOccurs=\"0\" />\r\n      <xsd:element name=\"F\" minOccurs=\"0\" />\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"E\">\r\n  <xsd:complexType>\r\n    <xsd:sequence>\r\n      <xsd:choice>\r\n        <xsd:sequence maxOccurs=\"2\">\r\n          <xsd:element name=\"A\" minOccurs=\"0\" maxOccurs=\"2\" />\r\n          <xsd:element name=\"B\" />\r\n          <xsd:element name=\"C\" minOccurs=\"0\" />\r\n        </xsd:sequence>\r\n        <xsd:sequence minOccurs=\"0\">\r\n          <xsd:element name=\"D\" />\r\n          <xsd:element name=\"E\" />\r\n          <xsd:element name=\"F\" />\r\n        </xsd:sequence>\r\n      </xsd:choice>\r\n      <xsd:element name=\"G\" minOccurs=\"0\" />\r\n      <xsd:choice minOccurs=\"1\"/>\r\n      <xsd:element name=\"H\" minOccurs=\"0\" />\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:attribute name=\"aA\" />\r\n<xsd:attribute name=\"aB\" />\r\n<xsd:attribute name=\"aC\" />\r\n<xsd:attribute name=\"aD\" />\r\n<xsd:attribute name=\"aE\" />\r\n<xsd:attribute name=\"aF\" />\r\n<xsd:attribute name=\"aG\" />\r\n<xsd:attribute name=\"aH\" />\r\n<xsd:attribute name=\"aI\" />\r\n<xsd:attribute name=\"aJ\" />\r\n<xsd:attribute name=\"aK\" type=\"xsd:date\" />\r\n<xsd:attribute name=\"aL\" type=\"xsd:integer\" />\r\n<xsd:attribute name=\"aM\" type=\"xsd:base64Binary\" />\r\n<xsd:attribute name=\"aN\" type=\"foo:listOfBytes\" />\r\n\r\n<xsd:attribute name=\"aBoolean\" type=\"xsd:boolean\" />\r\n\r\n<xsd:complexType name=\"F\">\r\n  <xsd:sequence>\r\n    <xsd:element ref=\"foo:AB\"/>\r\n  </xsd:sequence>\r\n  <xsd:attribute ref=\"foo:aB\" />\r\n  <xsd:attribute ref=\"foo:aC\" />\r\n  <xsd:attribute ref=\"foo:aA\" use=\"required\"/>\r\n</xsd:complexType>\r\n\r\n<xsd:complexType name=\"extended_F\">\r\n  <xsd:complexContent>\r\n    <xsd:extension base=\"foo:F\">\r\n      <xsd:anyAttribute namespace=\"##any\" />\r\n    </xsd:extension>\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"F\" type=\"foo:F\" nillable=\"true\"/>\r\n\r\n<xsd:element name=\"G\" nillable=\"true\">\r\n  <xsd:complexType>\r\n    <xsd:sequence>\r\n      <xsd:element ref=\"foo:AB\" minOccurs=\"0\"/>\r\n    </xsd:sequence>\r\n    <xsd:attribute ref=\"foo:aB\" />\r\n    <xsd:attribute ref=\"foo:aC\" />\r\n    <xsd:attribute ref=\"foo:aA\" use=\"required\"/>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"G2\">\r\n  <xsd:complexType><!-- Don't make it a named type -->\r\n      <xsd:anyAttribute namespace=\"##any\" />\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"G3\"><!-- Don't make it nillable -->\r\n  <xsd:complexType>\r\n      <xsd:sequence>\r\n        <xsd:element name=\"A\" />\r\n      </xsd:sequence>\r\n      <xsd:anyAttribute namespace=\"##any\" />\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"G4\">\r\n  <xsd:complexType>\r\n    <xsd:attribute name=\"aK\" type=\"xsd:positiveInteger\" use=\"required\"/>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"H\">\r\n  <xsd:complexType>\r\n    <xsd:sequence>\r\n      <xsd:element name=\"A\" minOccurs=\"0\"/>\r\n      <xsd:any namespace=\"urn:eoo urn:goo\" />\r\n      <xsd:element name=\"B\" />\r\n      <xsd:any namespace=\"##other\" />\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"H2\">\r\n  <xsd:complexType>\r\n    <xsd:sequence>\r\n      <xsd:any namespace=\"##other\" minOccurs=\"0\" />\r\n      <xsd:any namespace=\"##targetNamespace ##local\" />\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"H3\">\r\n  <xsd:complexType>\r\n    <xsd:sequence>\r\n      <xsd:any namespace=\"urn:none_01\" minOccurs=\"0\" /> <!-- so that \"urn:none_01\" gets in uri partition. -->\r\n      <xsd:element ref=\"foo:AB\" />\r\n      <xsd:any namespace=\"##any\" minOccurs=\"0\" />\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<!--<xsd:element name=\"H4\">\r\n  <xsd:complexType>\r\n    <xsd:sequence>\r\n      <xsd:sequence>\r\n        <xsd:any namespace=\"##local ##targetNamespace\" minOccurs=\"0\" maxOccurs=\"2\"/>\r\n        <xsd:element ref=\"hoo:AC\" minOccurs=\"0\"/>\r\n        <xsd:sequence>\r\n          <xsd:any namespace=\"urn:goo\" minOccurs=\"0\" />\r\n          <xsd:element ref=\"hoo:AB\" minOccurs=\"0\"/>\r\n        </xsd:sequence>\r\n      </xsd:sequence>\r\n      <xsd:any namespace=\"urn:ioo\" minOccurs=\"0\" />\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>-->\r\n\r\n<xsd:element name=\"I\" nillable=\"true\">\r\n  <xsd:complexType>\r\n    <xsd:choice>\r\n      <xsd:element name=\"A\">\r\n        <xsd:complexType>\r\n          <xsd:simpleContent>\r\n            <xsd:extension base=\"xsd:anySimpleType\">\r\n              <xsd:anyAttribute namespace=\"urn:hoo urn:none_02 urn:goo urn:foo urn:hoo urn:hoo ##local\" />\r\n            </xsd:extension>\r\n          </xsd:simpleContent>\r\n        </xsd:complexType>\r\n      </xsd:element>\r\n      <xsd:element name=\"B\">\r\n        <xsd:complexType>\r\n          <xsd:simpleContent>\r\n            <xsd:extension base=\"xsd:anySimpleType\">\r\n              <xsd:anyAttribute namespace=\"##other\" />\r\n            </xsd:extension>\r\n          </xsd:simpleContent>\r\n        </xsd:complexType>\r\n      </xsd:element>\r\n    </xsd:choice>\r\n    <xsd:attribute ref=\"foo:aF\" />\r\n    <xsd:attribute ref=\"foo:aI\" use=\"required\" />\r\n    <xsd:attribute ref=\"foo:aC\" />\r\n    <xsd:anyAttribute namespace=\"##any\" />\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:complexType name=\"I2\">\r\n  <xsd:sequence>\r\n    <xsd:element name=\"A\" minOccurs=\"0\" />\r\n    <xsd:element name=\"B\" minOccurs=\"0\" />\r\n  </xsd:sequence>\r\n  <xsd:attribute ref=\"foo:aF\" />\r\n  <xsd:attribute ref=\"foo:aI\" use=\"required\" />\r\n  <xsd:attribute ref=\"foo:aC\" />\r\n  <xsd:anyAttribute namespace=\"urn:hoo urn:none_03 urn:goo\" />\r\n</xsd:complexType>\r\n\r\n<xsd:complexType name=\"extended_I2\">\r\n  <xsd:complexContent>\r\n    <xsd:extension base=\"foo:I2\">\r\n      <xsd:anyAttribute namespace=\"##targetNamespace ##local\" />\r\n    </xsd:extension>\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"I2\" type=\"foo:I2\" nillable=\"true\" />\r\n\r\n<xsd:element name=\"J\">\r\n  <xsd:complexType>\r\n    <xsd:sequence maxOccurs=\"2\">\r\n      <xsd:element ref=\"foo:AB\"/>\r\n      <xsd:element ref=\"foo:AC\" minOccurs=\"0\" maxOccurs=\"2\"/>\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"K\" type=\"foo:finalString\"/>\r\n\r\n<xsd:element name=\"L\">\r\n  <xsd:complexType>\r\n    <xsd:sequence minOccurs=\"2\" maxOccurs=\"2\">\r\n      <xsd:element ref=\"foo:AB\"/>\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"M\">\r\n  <xsd:complexType>\r\n    <xsd:attribute ref=\"foo:aA\" />\r\n    <xsd:attribute ref=\"foo:aB\" use=\"required\" />\r\n    <xsd:attribute ref=\"foo:aC\" />\r\n    <xsd:attribute ref=\"foo:aD\" />\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:attribute name=\"aJ4M2\" type=\"xsd:hexBinary\" />\r\n<xsd:attribute name=\"aK4M2\" type=\"xsd:hexBinary\" />\r\n<xsd:attribute name=\"aL4M2\" type=\"xsd:hexBinary\" />\r\n<xsd:attribute name=\"aM4M2\" type=\"xsd:hexBinary\" />\r\n<xsd:attribute name=\"aN4M2\" type=\"xsd:hexBinary\" />\r\n<xsd:attribute name=\"aO4M2\" type=\"xsd:hexBinary\" />\r\n\r\n<xsd:element name=\"M2\">\r\n  <xsd:complexType>\r\n    <xsd:sequence maxOccurs=\"unbounded\">\r\n      <xsd:element name=\"A\">\r\n        <xsd:complexType>\r\n          <xsd:attribute ref=\"foo:aK\" /><!-- xsd:date -->\r\n          <xsd:attribute ref=\"foo:aL\" /><!-- xsd:integer -->\r\n          <xsd:attribute ref=\"foo:aM\" /><!-- xsd:base64Binary -->\r\n          <xsd:attribute ref=\"foo:aN\" /><!-- xsd:listOfBytes -->\r\n        </xsd:complexType>\r\n      </xsd:element>\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"N\">\r\n  <xsd:complexType>\r\n    <xsd:sequence maxOccurs=\"unbounded\">\r\n      <xsd:element name=\"A\" type=\"foo:extendedDate\" />\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"P\">\r\n  <xsd:complexType>\r\n    <xsd:choice maxOccurs=\"unbounded\">\r\n      <xsd:element ref=\"foo:P1\" />\r\n      <xsd:element ref=\"foo:P2\" />\r\n      <xsd:element ref=\"foo:P3\" />\r\n    </xsd:choice>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:complexType name=\"P1\">\r\n  <xsd:sequence>\r\n    <xsd:element ref=\"foo:AB\" />\r\n  </xsd:sequence>\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"P1\" type=\"foo:P1\" />\r\n\r\n<xsd:complexType name=\"extended_P1\">\r\n  <xsd:complexContent>\r\n    <xsd:extension base=\"foo:P1\">\r\n      <xsd:sequence>\r\n        <xsd:element ref=\"foo:AB\" />\r\n      </xsd:sequence>\r\n    </xsd:extension>\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n<xsd:complexType name=\"P2\">\r\n  <xsd:sequence>\r\n    <xsd:element ref=\"foo:AC\" />\r\n  </xsd:sequence>\r\n  <xsd:attribute ref=\"foo:aA\" />\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"P2\" type=\"foo:P2\" />\r\n\r\n<xsd:complexType name=\"extended_P2\">\r\n  <xsd:complexContent>\r\n    <xsd:extension base=\"foo:P2\">\r\n      <xsd:sequence>\r\n        <xsd:element ref=\"foo:AC\" />\r\n      </xsd:sequence>\r\n    </xsd:extension>\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n<xsd:complexType name=\"P3\">\r\n  <xsd:sequence>\r\n    <xsd:element ref=\"foo:AD\" />\r\n  </xsd:sequence>\r\n  <xsd:anyAttribute namespace=\"urn:eoo urn:foo\" />\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"P3\" type=\"foo:P3\" />\r\n\r\n<xsd:complexType name=\"extended_P3\">\r\n  <xsd:complexContent>\r\n    <xsd:extension base=\"foo:P3\">\r\n      <xsd:sequence>\r\n        <xsd:element ref=\"foo:AD\" />\r\n      </xsd:sequence>\r\n    </xsd:extension>\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"Q\">\r\n  <xsd:complexType mixed=\"true\">\r\n    <xsd:sequence>\r\n      <xsd:element name=\"Qc\" form=\"qualified\" minOccurs=\"0\">\r\n        <xsd:complexType/>\r\n      </xsd:element>\r\n      <xsd:any namespace=\"##other\" minOccurs=\"0\" />\r\n      <xsd:element ref=\"foo:Qb\" minOccurs=\"0\" /><!-- element Qb constitutes a substitution group. see below. -->\r\n      <xsd:any namespace=\"##local\" minOccurs=\"0\"/>\r\n      <xsd:element name=\"Qa\" form=\"qualified\" minOccurs=\"0\">\r\n        <xsd:complexType/>\r\n      </xsd:element>\r\n    </xsd:sequence>\r\n    <xsd:attribute ref=\"foo:aL\" /><!-- xsd:integer -->\r\n    <xsd:attribute ref=\"foo:aK\" /><!-- xsd:date -->\r\n    <xsd:anyAttribute namespace=\"urn:hoo urn:goo ##local urn:foo\" />\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"Qb\" type=\"foo:tQb\" />\r\n<xsd:element name=\"Qz\" type=\"foo:tQz\" substitutionGroup=\"foo:Qb\" />\r\n\r\n<xsd:complexType name=\"tQb\"/>\r\n<xsd:complexType name=\"tQz\">\r\n  <xsd:complexContent>\r\n    <xsd:extension base=\"foo:tQb\" />\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n</xsd:schema>\r\n";

	Grammars g = SchemaInformedTest.getGrammarFromSchemaAsString(schema);

	String xml = "<foo:F xmlns:foo='urn:foo' xsi:nil='true'" + " \n "
			+ " foo:aA='' foo:aB='' foo:aC=''" + " \n "
			+ " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>"
			+ " \n " + "  <!-- No content here. -->" + " \n " + "</foo:F>";

	EXIFactory ef = DefaultEXIFactory.newInstance();
	ef.setGrammars(g);
	// ef.getFidelityOptions().setFidelity(FidelityOptions.FEATURE_COMMENT,
	// true);

	ByteArrayOutputStream baos = new ByteArrayOutputStream();

	// encode
	{
		EXIResult exiResult = new EXIResult(ef);
		exiResult.setOutputStream(baos);
		XMLReader xmlReader = XMLReaderFactory.createXMLReader();
		xmlReader.setContentHandler(exiResult.getHandler());
		xmlReader.parse(new InputSource(new ByteArrayInputStream(xml
				.getBytes())));
	}

	// decode
	ByteArrayOutputStream baosDecXML = new ByteArrayOutputStream();
	{
		InputSource is = new InputSource(new ByteArrayInputStream(
				baos.toByteArray()));
		XMLReader exiReader = new SAXFactory(ef).createEXIReader();

		Result result = new StreamResult(baosDecXML);
		SAXSource exiSource = new SAXSource(is);
		exiSource.setXMLReader(exiReader);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer();
		transformer.transform(exiSource, result);
	}

	// System.out.println(new String(baosDecXML.toByteArray()));

}
 
Example 17
Source File: FuncionEvaluacionBean.java    From KEEL with GNU General Public License v3.0 4 votes vote down vote up
private void parsea(String xml) throws ParserConfigurationException,
        IOException, SAXException {
    SAXParserFactory spf = null;
    SAXParser saxParser = null;
    XMLReader xmlReader = null;

    spf = SAXParserFactory.newInstance();

    saxParser = spf.newSAXParser();

    xmlReader = saxParser.getXMLReader();

    xmlReader.setContentHandler(new FuncionEvaluacionBeanHandler(xmlReader));

    StringReader reader = new StringReader(xml);

    InputSource source = new InputSource(reader);

    xmlReader.parse(source);
}
 
Example 18
Source File: XmiCasDeserializerTest.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
public void testMultipleSofas() throws Exception {
    try {
      CAS cas = CasCreationUtils.createCas(typeSystem, new TypePriorities_impl(),
              new FsIndexDescription[0]);
      // set document text for the initial view
      cas.setDocumentText("This is a test");
      // create a new view and set its document text
      CAS cas2 = cas.createView("OtherSofa");
      cas2.setDocumentText("This is only a test");

      // Change this test to create an instance of TOP because you cannot add an annotation to other than 
      //   the view it is created in. https://issues.apache.org/jira/browse/UIMA-4099
      // create a TOP and add to index of both views
      Type topType = cas.getTypeSystem().getTopType();
      FeatureStructure aTOP = cas.createFS(topType);
      cas.getIndexRepository().addFS(aTOP);
      cas2.getIndexRepository().addFS(aTOP); 
      FSIterator<FeatureStructure> it = cas.getIndexRepository().getAllIndexedFS(topType);
      FSIterator<FeatureStructure> it2 = cas2.getIndexRepository().getAllIndexedFS(topType);
      it.next(); it.next();
      it2.next(); it2.next(); 
      assertFalse(it.hasNext());
      assertFalse(it2.hasNext());

      // 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]);
      for (int i = 0; i < 2; i++) {
        XmiCasDeserializer newDeser = new XmiCasDeserializer(newCas.getTypeSystem());
        ContentHandler newDeserHandler = newDeser.getXmiCasHandler(newCas);
        SAXParserFactory fact = SAXParserFactory.newInstance();
        SAXParser parser = fact.newSAXParser();
        XMLReader xmlReader = parser.getXMLReader();
        xmlReader.setContentHandler(newDeserHandler);
        xmlReader.parse(new InputSource(new StringReader(xml)));

        // check sofas
        assertEquals("This is a test", newCas.getDocumentText());
        CAS newCas2 = newCas.getView("OtherSofa");
        assertEquals("This is only a test", newCas2.getDocumentText());

        // check that annotation is still indexed in both views
        // check that annotation is still indexed in both views
        it = newCas.getIndexRepository().getAllIndexedFS(topType);
        it2 = newCas2.getIndexRepository().getAllIndexedFS(topType);
        it.next(); it.next();
        it2.next(); it2.next(); 
        assertFalse(it.hasNext());
//        assertFalse(it2.hasNext());        assertTrue(tIndex.size() == 2); // document annot and this one
//        assertTrue(t2Index.size() == 2); // ditto

        newCas.reset();
      }
    } catch (Exception e) {
      JUnitExtension.handleException(e);
    }
  }
 
Example 19
Source File: TestXmlUtils.java    From tracecompass with Eclipse Public License 2.0 3 votes vote down vote up
/**
 * Test the SAX parser factory
 *
 * @throws SAXException
 *             the file could not parse
 * @throws ParserConfigurationException
 *             the parser was badly configured, should not happen
 * @throws IOException
 *             permission issue, should not happen
 */
@Test
public void testNewSafeSaxParserFactory() throws SAXException, ParserConfigurationException, IOException {
    SAXParserFactory parserFactory = XmlUtils.newSafeSaxParserFactory();
    assertNotNull(parserFactory);
    XMLReader xmlReader = parserFactory.newSAXParser().getXMLReader();
    assertNotNull(xmlReader);
    xmlReader.parse(new InputSource(new StringReader(SAFE_DOCUMENT)));
}
 
Example 20
Source File: XmlCasDeserializer.java    From uima-uimaj with Apache License 2.0 3 votes vote down vote up
/**
 * Deserializes a CAS from XMI or XCAS, version returning the SerialFormat
 * 
 * @param aStream
 *          input stream from which to read the XML document
 * @param aCAS
 *          CAS into which to deserialize. This CAS must be set up with a type system that is
 *          compatible with that in the XML
 * @param aLenient
 *          if true, unknown Types will be ignored. If false, unknown Types will cause an
 *          exception. The default is false.
 * @return the format of the data  
 * 
 * @throws SAXException
 *           if an XML Parsing error occurs
 * @throws IOException
 *           if an I/O failure occurs
 */
static SerialFormat deserializeR(InputStream aStream, CAS aCAS, boolean aLenient)
    throws SAXException, IOException {
  XMLReader xmlReader = XMLUtils.createXMLReader();
  XmlCasDeserializerHandler handler = new XmlCasDeserializerHandler(aCAS, aLenient);
  xmlReader.setContentHandler(handler);
  xmlReader.parse(new InputSource(aStream));
  return (handler.mDelegateHandler instanceof XmiCasDeserializer.XmiCasDeserializerHandler)
           ? SerialFormat.XMI
           : SerialFormat.XCAS;
}