Search in sources :

Example 21 with XMLReader

use of org.xml.sax.XMLReader in project translationstudio8 by heartsome.

the class Xlsx2TmxHelper method parse.

public void parse(InputStream sheetInputStream, ReadOnlySharedStringsTable sharedStringsTable, AbstractWriter tmxWriter) throws ParserConfigurationException, SAXException, IOException {
    InputSource sheetSource = new InputSource(sheetInputStream);
    SAXParserFactory saxFactory = SAXParserFactory.newInstance();
    SAXParser saxParser = saxFactory.newSAXParser();
    XMLReader sheetParser = saxParser.getXMLReader();
    ContentHandler handler = new XSSFHander(sharedStringsTable);
    sheetParser.setContentHandler(handler);
    sheetParser.parse(sheetSource);
    if (langCodes.isEmpty()) {
        throw new SAXException("EMPTY-LANG-CODE");
    }
    writeEnd();
}
Also used : InputSource(org.xml.sax.InputSource) SAXParser(javax.xml.parsers.SAXParser) XMLReader(org.xml.sax.XMLReader) ContentHandler(org.xml.sax.ContentHandler) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 22 with XMLReader

use of org.xml.sax.XMLReader in project camel by apache.

the class JingValidator method process.

public void process(Exchange exchange) throws Exception {
    Jaxp11XMLReaderCreator xmlCreator = new Jaxp11XMLReaderCreator();
    DefaultValidationErrorHandler errorHandler = new DefaultValidationErrorHandler();
    PropertyMapBuilder mapBuilder = new PropertyMapBuilder();
    mapBuilder.put(ValidateProperty.XML_READER_CREATOR, xmlCreator);
    mapBuilder.put(ValidateProperty.ERROR_HANDLER, errorHandler);
    PropertyMap propertyMap = mapBuilder.toPropertyMap();
    Validator validator = getSchema().createValidator(propertyMap);
    Message in = exchange.getIn();
    SAXSource saxSource = in.getBody(SAXSource.class);
    if (saxSource == null) {
        Source source = exchange.getIn().getMandatoryBody(Source.class);
        saxSource = ExchangeHelper.convertToMandatoryType(exchange, SAXSource.class, source);
    }
    InputSource bodyInput = saxSource.getInputSource();
    // now lets parse the body using the validator
    XMLReader reader = xmlCreator.createXMLReader();
    reader.setContentHandler(validator.getContentHandler());
    reader.setDTDHandler(validator.getDTDHandler());
    reader.setErrorHandler(errorHandler);
    reader.parse(bodyInput);
    errorHandler.handleErrors(exchange, schema);
}
Also used : InputSource(org.xml.sax.InputSource) Jaxp11XMLReaderCreator(com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator) PropertyMap(com.thaiopensource.util.PropertyMap) SAXSource(javax.xml.transform.sax.SAXSource) Message(org.apache.camel.Message) PropertyMapBuilder(com.thaiopensource.util.PropertyMapBuilder) DefaultValidationErrorHandler(org.apache.camel.processor.validation.DefaultValidationErrorHandler) Validator(com.thaiopensource.validate.Validator) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader)

Example 23 with XMLReader

use of org.xml.sax.XMLReader in project camel by apache.

the class XmlConverter method toSAXSourceFromStream.

@Converter
public SAXSource toSAXSourceFromStream(StreamSource source, Exchange exchange) throws SAXException {
    InputSource inputSource;
    if (source.getReader() != null) {
        inputSource = new InputSource(source.getReader());
    } else {
        inputSource = new InputSource(source.getInputStream());
    }
    inputSource.setSystemId(source.getSystemId());
    inputSource.setPublicId(source.getPublicId());
    XMLReader xmlReader = null;
    try {
        // use the SAXPaserFactory which is set from exchange
        if (exchange != null) {
            SAXParserFactory sfactory = exchange.getProperty(Exchange.SAXPARSER_FACTORY, SAXParserFactory.class);
            if (sfactory != null) {
                if (!sfactory.isNamespaceAware()) {
                    sfactory.setNamespaceAware(true);
                }
                xmlReader = sfactory.newSAXParser().getXMLReader();
            }
        }
        if (xmlReader == null) {
            if (xmlReaderPool == null) {
                xmlReaderPool = new XMLReaderPool(createSAXParserFactory());
            }
            xmlReader = xmlReaderPool.createXMLReader();
        }
    } catch (Exception ex) {
        LOG.warn("Cannot create the SAXParser XMLReader, due to {}", ex);
    }
    return new SAXSource(xmlReader, inputSource);
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) XMLStreamException(javax.xml.stream.XMLStreamException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) Converter(org.apache.camel.Converter)

Example 24 with XMLReader

use of org.xml.sax.XMLReader in project robovm by robovm.

the class TransformerFactoryImpl method newTemplates.

/**
   * Process the source into a Templates object, which is likely
   * a compiled representation of the source. This Templates object
   * may then be used concurrently across multiple threads.  Creating
   * a Templates object allows the TransformerFactory to do detailed
   * performance optimization of transformation instructions, without
   * penalizing runtime transformation.
   *
   * @param source An object that holds a URL, input stream, etc.
   * @return A Templates object capable of being used for transformation purposes.
   *
   * @throws TransformerConfigurationException May throw this during the parse when it
   *            is constructing the Templates object and fails.
   */
public Templates newTemplates(Source source) throws TransformerConfigurationException {
    String baseID = source.getSystemId();
    if (null != baseID) {
        baseID = SystemIDResolver.getAbsoluteURI(baseID);
    }
    if (source instanceof DOMSource) {
        DOMSource dsource = (DOMSource) source;
        Node node = dsource.getNode();
        if (null != node)
            return processFromNode(node, baseID);
        else {
            String messageStr = XSLMessages.createMessage(XSLTErrorResources.ER_ILLEGAL_DOMSOURCE_INPUT, null);
            throw new IllegalArgumentException(messageStr);
        }
    }
    TemplatesHandler builder = newTemplatesHandler();
    builder.setSystemId(baseID);
    try {
        InputSource isource = SAXSource.sourceToInputSource(source);
        isource.setSystemId(baseID);
        XMLReader reader = null;
        if (source instanceof SAXSource)
            reader = ((SAXSource) source).getXMLReader();
        if (null == reader) {
            // Use JAXP1.1 ( if possible )
            try {
                javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
                factory.setNamespaceAware(true);
                if (m_isSecureProcessing) {
                    try {
                        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
                    } catch (org.xml.sax.SAXException se) {
                    }
                }
                javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser();
                reader = jaxpParser.getXMLReader();
            } catch (javax.xml.parsers.ParserConfigurationException ex) {
                throw new org.xml.sax.SAXException(ex);
            } catch (javax.xml.parsers.FactoryConfigurationError ex1) {
                throw new org.xml.sax.SAXException(ex1.toString());
            } catch (NoSuchMethodError ex2) {
            } catch (AbstractMethodError ame) {
            }
        }
        if (null == reader)
            reader = XMLReaderFactory.createXMLReader();
        // If you set the namespaces to true, we'll end up getting double 
        // xmlns attributes.  Needs to be fixed.  -sb
        // reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        reader.setContentHandler(builder);
        reader.parse(isource);
    } catch (org.xml.sax.SAXException se) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(new TransformerException(se));
            } catch (TransformerConfigurationException ex1) {
                throw ex1;
            } catch (TransformerException ex1) {
                throw new TransformerConfigurationException(ex1);
            }
        } else {
            throw new TransformerConfigurationException(se.getMessage(), se);
        }
    } catch (Exception e) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(new TransformerException(e));
                return null;
            } catch (TransformerConfigurationException ex1) {
                throw ex1;
            } catch (TransformerException ex1) {
                throw new TransformerConfigurationException(ex1);
            }
        } else {
            throw new TransformerConfigurationException(e.getMessage(), e);
        }
    }
    return builder.getTemplates();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Node(org.w3c.dom.Node) TemplatesHandler(javax.xml.transform.sax.TemplatesHandler) TransformerException(javax.xml.transform.TransformerException) StopParseException(org.apache.xml.utils.StopParseException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

Example 25 with XMLReader

use of org.xml.sax.XMLReader in project robovm by robovm.

the class ProcessorInclude method parse.

/**
   * Set off a new parse for an included or imported stylesheet.  This will 
   * set the {@link StylesheetHandler} to a new state, and recurse in with 
   * a new set of parse events.  Once this function returns, the state of 
   * the StylesheetHandler should be restored.
   *
   * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
   * @param uri The Namespace URI, which should be the XSLT namespace.
   * @param localName The local name (without prefix), which should be "include" or "import".
   * @param rawName The qualified name (with prefix).
   * @param attributes The list of attributes on the xsl:include or xsl:import element.
   *
   * @throws org.xml.sax.SAXException Any SAX exception, possibly
   *            wrapping another exception.
   */
protected void parse(StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes) throws org.xml.sax.SAXException {
    TransformerFactoryImpl processor = handler.getStylesheetProcessor();
    URIResolver uriresolver = processor.getURIResolver();
    try {
        Source source = null;
        if (null != uriresolver) {
            // There is a user provided URI resolver.
            // At the startElement() call we would
            // have tried to obtain a Source from it
            // which we now retrieve
            source = handler.peekSourceFromURIResolver();
            if (null != source && source instanceof DOMSource) {
                Node node = ((DOMSource) source).getNode();
                // There is a user provided URI resolver.
                // At the startElement() call we would
                // have already pushed the system ID, obtained
                // from either the source.getSystemId(), if non-null
                // or from SystemIDResolver.getAbsoluteURI() as a backup
                // which we now retrieve.
                String systemId = handler.peekImportURL();
                // stylesheet module onto the stack.
                if (systemId != null)
                    handler.pushBaseIndentifier(systemId);
                TreeWalker walker = new TreeWalker(handler, new org.apache.xml.utils.DOM2Helper(), systemId);
                try {
                    walker.traverse(node);
                } catch (org.xml.sax.SAXException se) {
                    throw new TransformerException(se);
                }
                if (systemId != null)
                    handler.popBaseIndentifier();
                return;
            }
        }
        if (null == source) {
            String absURL = SystemIDResolver.getAbsoluteURI(getHref(), handler.getBaseIdentifier());
            source = new StreamSource(absURL);
        }
        // possible callback to a class that over-rides this method.
        source = processSource(handler, source);
        XMLReader reader = null;
        if (source instanceof SAXSource) {
            SAXSource saxSource = (SAXSource) source;
            // may be null
            reader = saxSource.getXMLReader();
        }
        InputSource inputSource = SAXSource.sourceToInputSource(source);
        if (null == reader) {
            // Use JAXP1.1 ( if possible )
            try {
                javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
                factory.setNamespaceAware(true);
                if (handler.getStylesheetProcessor().isSecureProcessing()) {
                    try {
                        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
                    } catch (org.xml.sax.SAXException se) {
                    }
                }
                javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser();
                reader = jaxpParser.getXMLReader();
            } catch (javax.xml.parsers.ParserConfigurationException ex) {
                throw new org.xml.sax.SAXException(ex);
            } catch (javax.xml.parsers.FactoryConfigurationError ex1) {
                throw new org.xml.sax.SAXException(ex1.toString());
            } catch (NoSuchMethodError ex2) {
            } catch (AbstractMethodError ame) {
            }
        }
        if (null == reader)
            reader = XMLReaderFactory.createXMLReader();
        if (null != reader) {
            reader.setContentHandler(handler);
            // Push the absolute URI of the included/imported
            // stylesheet module onto the stack.
            handler.pushBaseIndentifier(inputSource.getSystemId());
            try {
                reader.parse(inputSource);
            } finally {
                handler.popBaseIndentifier();
            }
        }
    } catch (IOException ioe) {
        handler.error(XSLTErrorResources.ER_IOEXCEPTION, new Object[] { getHref() }, ioe);
    } catch (TransformerException te) {
        handler.error(te.getMessage(), te);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) Node(org.w3c.dom.Node) URIResolver(javax.xml.transform.URIResolver) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) TransformerException(javax.xml.transform.TransformerException) XMLReader(org.xml.sax.XMLReader) StreamSource(javax.xml.transform.stream.StreamSource) TreeWalker(org.apache.xml.utils.TreeWalker) IOException(java.io.IOException) SAXSource(javax.xml.transform.sax.SAXSource)

Aggregations

XMLReader (org.xml.sax.XMLReader)234 InputSource (org.xml.sax.InputSource)186 SAXException (org.xml.sax.SAXException)82 IOException (java.io.IOException)75 SAXParserFactory (javax.xml.parsers.SAXParserFactory)51 SAXSource (javax.xml.transform.sax.SAXSource)48 SAXParser (javax.xml.parsers.SAXParser)42 StringReader (java.io.StringReader)37 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)35 InputStream (java.io.InputStream)28 ExpatReader (org.apache.harmony.xml.ExpatReader)24 ContentHandler (org.xml.sax.ContentHandler)20 TransformerException (javax.xml.transform.TransformerException)19 DOMSource (javax.xml.transform.dom.DOMSource)18 StreamSource (javax.xml.transform.stream.StreamSource)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 FileReader (java.io.FileReader)16 InputStreamReader (java.io.InputStreamReader)12 SAXParseException (org.xml.sax.SAXParseException)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10