List of usage examples for org.xml.sax XMLReader parse
public void parse(String systemId) throws IOException, SAXException;
From source file:org.intalio.tempo.workflow.wds.core.xforms.XFormsProcessor.java
/** * Process an XForms document.//from ww w . j a v a 2 s.c o m * <ul> * <li>If the model schema is specified as a relative URI, it is changed to comply to the form URI, * e.g. if a form is stored at (oxf:/)my/uri/form1.xform and it specified "form1.xsd" * as its model schema, then the value of the "schema" attribute of the "xforms:model" * element will be rewritten as: "oxf:/my/uri/form1.xsd"</li> * <li>The XForms document is reformatted in a "pretty-print" way.</li> * </ul> */ @SuppressWarnings("deprecation") public static Item processXForm(final String itemUri, InputStream inputStream) throws IOException, ParserConfigurationException, SAXException { if (LOG.isDebugEnabled()) LOG.debug("Processing " + itemUri); StringWriter out = new StringWriter(); SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); OutputFormat format = new OutputFormat(); format.setEncoding(CharEncoding.UTF_8); format.setOmitXMLDeclaration(false); format.setOmitComments(false); format.setPreserveSpace(true); SchemaURLRewriter ser = new SchemaURLRewriter(out, format, itemUri); XMLReader reader = parser.getXMLReader(); reader.setContentHandler(ser); reader.parse(new InputSource(inputStream)); return new Item(itemUri, XFORMS_CONTENT_TYPE, out.toString().getBytes(CharEncoding.UTF_8)); }
From source file:org.intermine.modelviewer.jaxb.ConfigParser.java
/** * Unmarshalls an XML document using the very forgiving event driven parser provided * by SAX. No issues with namespaces and so forth here. * /*from w w w .j a va2 s. c o m*/ * @param context The type of objects expected from the XML file. * @param file The file to load. * * @return The Object created as a result of reading the file. * Its type will depend on <code>context</code>. * * @throws SAXException if there is a problem parsing with SAX. * @throws ParserConfigurationException if there is a configuration problem with * the SAX system. * @throws IOException if there is a low level I/O problem. * * @see CoreHandler * @see GenomicHandler * @see ProjectHandler * @see GenomicCoreHandler */ private Object saxParse(Context context, File file) throws SAXException, ParserConfigurationException, IOException { //Schema schema = schemas.get(context); //schema.newValidator().validate(new StreamSource(file)); BackupContentHandler handler; switch (context) { case CORE: handler = new CoreHandler(); break; case GENOMIC: handler = new GenomicHandler(); break; case PROJECT: handler = new ProjectHandler(); break; default: throw new UnsupportedOperationException("Cannot load things of type " + context); } XMLReader reader = XMLReaderFactory.createXMLReader(); reader.setContentHandler(handler); reader.parse(new InputSource(new FileReader(file))); return handler.getResult(); }
From source file:org.itracker.web.util.ImportExportUtilities.java
/** * Takes an XML file matching the ITracker import/export DTD and returns an array * of AbstractBean objects. The array will contain all of the projects, components * versions, users, custom fields, and issues contained in the XML. * * @param xmlReader an xml reader to import * @throws ImportExportException thrown if the xml can not be parsed into the appropriate objects *///from ww w .j a v a 2 s . c o m public static AbstractEntity[] importIssues(Reader xmlReader) throws ImportExportException { AbstractEntity[] abstractBeans; try { logger.debug("Starting XML data import."); XMLReader reader = XMLReaderFactory.createXMLReader(); ImportHandler handler = new ImportHandler(); reader.setContentHandler(handler); reader.setErrorHandler(handler); reader.parse(new InputSource(xmlReader)); abstractBeans = handler.getModels(); logger.debug("Imported a total of " + abstractBeans.length + " beans."); } catch (Exception e) { logger.error("Exception.", e); throw new ImportExportException(e.getMessage()); } return abstractBeans; }
From source file:org.jasig.cas.client.util.XmlUtils.java
/** * Retrieve the text for a group of elements. Each text element is an entry * in a list.// w w w.ja v a 2 s .co m * <p>This method is currently optimized for the use case of two elements in a list. * * @param xmlAsString the xml response * @param element the element to look for * @return the list of text from the elements. */ public static List getTextForElements(final String xmlAsString, final String element) { final List elements = new ArrayList(2); final XMLReader reader = getXmlReader(); final DefaultHandler handler = new DefaultHandler() { private boolean foundElement = false; private StringBuffer buffer = new StringBuffer(); public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException { if (localName.equals(element)) { this.foundElement = true; } } public void endElement(final String uri, final String localName, final String qName) throws SAXException { if (localName.equals(element)) { this.foundElement = false; elements.add(this.buffer.toString()); this.buffer = new StringBuffer(); } } public void characters(char[] ch, int start, int length) throws SAXException { if (this.foundElement) { this.buffer.append(ch, start, length); } } }; reader.setContentHandler(handler); reader.setErrorHandler(handler); try { reader.parse(new InputSource(new StringReader(xmlAsString))); } catch (final Exception e) { LOG.error(e, e); return null; } return elements; }
From source file:org.jasig.cas.client.util.XmlUtils.java
/** * Retrieve the text for a specific element (when we know there is only * one).//from ww w. j av a 2 s . c o m * * @param xmlAsString the xml response * @param element the element to look for * @return the text value of the element. */ public static String getTextForElement(final String xmlAsString, final String element) { final XMLReader reader = getXmlReader(); final StringBuffer buffer = new StringBuffer(); final DefaultHandler handler = new DefaultHandler() { private boolean foundElement = false; public void startElement(final String uri, final String localName, final String qName, final Attributes attributes) throws SAXException { if (localName.equals(element)) { this.foundElement = true; } } public void endElement(final String uri, final String localName, final String qName) throws SAXException { if (localName.equals(element)) { this.foundElement = false; } } public void characters(char[] ch, int start, int length) throws SAXException { if (this.foundElement) { buffer.append(ch, start, length); } } }; reader.setContentHandler(handler); reader.setErrorHandler(handler); try { reader.parse(new InputSource(new StringReader(xmlAsString))); } catch (final Exception e) { LOG.error(e, e); return null; } return buffer.toString(); }
From source file:org.kalypso.gml.test.GmlParsingTester.java
protected GMLWorkspace parseGml(final InputStream is, final URL context) throws ParserConfigurationException, SAXException, IOException, GMLException { final SAXParser saxParser = m_saxFactory.newSAXParser(); // make namespace-prefxes visible to content handler // used to allow necessary schemas from gml document // saxParser.setProperty( "http://xml.org/sax/features/namespace-prefixes", Boolean.TRUE ); final XMLReader reader = saxParser.getXMLReader(); // TODO: also set an error handler here // TODO: use progress-monitors to show progress and let the user cancel parsing final GMLorExceptionContentHandler exceptionHandler = new GMLorExceptionContentHandler(reader, null, context, null);//from w w w .j av a2 s . c om reader.setContentHandler(exceptionHandler); final InputSource inputSource = new InputSource(is); reader.parse(inputSource); return exceptionHandler.getWorkspace(); }
From source file:org.kalypso.gmlschema.types.AbstractOldFormatMarshallingTypeHandlerAdapter.java
/** * @see org.kalypso.gmlschema.types.IMarshallingTypeHandler#marshal(java.lang.Object, org.xml.sax.ContentHandler, * org.xml.sax.ext.LexicalHandler, java.net.URL) *//*from ww w .j a v a2 s.c o m*/ @Override public void marshal(final Object value, final XMLReader reader, final URL context, final String gmlVersion) throws SAXException { try { final Document document = m_builder.newDocument(); final Node node = marshall(value, document, context); // value is encoded in xml in document object final StringWriter writer = new StringWriter(); // REMARK: we do not write the dom formatted here (argument false), because later the // content handler will probably do the formatting (IndentContentHandler). If we format here, we will get empty // lines later. XMLHelper.writeDOM(node, "UTF-8", writer, false); //$NON-NLS-1$ IOUtils.closeQuietly(writer); // value is encoded in string final String xmlString = writer.toString(); final String xmlStringNoHeader = XMLUtilities.removeXMLHeader(xmlString); final InputSource input = new InputSource(new StringReader(xmlStringNoHeader)); SAX_FACTORY.setNamespaceAware(true); final SAXParser saxParser = SAX_FACTORY.newSAXParser(); final XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler(reader.getContentHandler()); xmlReader.parse(input); } catch (final SAXException e) { throw e; } catch (final Exception e) { throw new SAXException(e); } }
From source file:org.kalypso.gmlschema.types.SimpleDOMTypeHandler.java
@Override public void marshal(final Object value, final XMLReader reader, final URL context, final String gmlVersion) throws SAXException { try {//w w w.jav a2 s .com final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final Document document = builder.newDocument(); final Node node = internalMarshall(value, document, context); // value is encoded in xml in document object final StringWriter writer = new StringWriter(); XMLHelper.writeDOM(node, "UTF-8", writer); //$NON-NLS-1$ IOUtils.closeQuietly(writer); // value is encoded in string final String xmlString = writer.toString(); final InputSource input = new InputSource(new StringReader(xmlString)); final SAXParserFactory saxFac = SAXParserFactory.newInstance(); saxFac.setNamespaceAware(true); final SAXParser saxParser = saxFac.newSAXParser(); final XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler(reader.getContentHandler()); xmlReader.parse(input); } catch (final SAXException saxe) { throw saxe; } catch (final Exception e) { throw new SAXException(e); } }
From source file:org.kalypso.mapserver.utils.MapFileUtilities.java
/** * This function loads a map file from XML. * * @param inputStream// w w w .j av a 2s.co m * The input stream. * @return The contents of the map file. */ public static Map loadFromXML(final InputStream inputStream) throws JAXBException, SAXException, ParserConfigurationException, IOException { /* Create the unmarshaller. */ final Unmarshaller unmarshaller = JC.createUnmarshaller(); /* Get the sax parser factory. */ final SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); spf.setXIncludeAware(true); /* Get the xml reader. */ final XMLReader xr = spf.newSAXParser().getXMLReader(); xr.setContentHandler(unmarshaller.getUnmarshallerHandler()); xr.parse(new InputSource(inputStream)); return (Map) unmarshaller.getUnmarshallerHandler().getResult(); }
From source file:org.kalypso.ogc.gml.GisTemplateHelper.java
public static final Gismapview loadGisMapView(final InputSource is) throws JAXBException, SAXException, ParserConfigurationException, IOException { final Unmarshaller unmarshaller = TemplateUtilities.createGismapviewUnmarshaller(); // XInclude awareness final SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true);//from w w w .j a v a2 s .c om spf.setXIncludeAware(true); final XMLReader xr = spf.newSAXParser().getXMLReader(); xr.setContentHandler(unmarshaller.getUnmarshallerHandler()); xr.parse(is); return (Gismapview) unmarshaller.getUnmarshallerHandler().getResult(); }