Search in sources :

Example 96 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project lucene-solr by apache.

the class EnumField method init.

/**
   * {@inheritDoc}
   */
@Override
protected void init(IndexSchema schema, Map<String, String> args) {
    super.init(schema, args);
    enumsConfigFile = args.get(PARAM_ENUMS_CONFIG);
    if (enumsConfigFile == null) {
        throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "No enums config file was configured.");
    }
    enumName = args.get(PARAM_ENUM_NAME);
    if (enumName == null) {
        throw new SolrException(SolrException.ErrorCode.NOT_FOUND, "No enum name was configured.");
    }
    InputStream is = null;
    try {
        is = schema.getResourceLoader().openResource(enumsConfigFile);
        final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            final Document doc = dbf.newDocumentBuilder().parse(is);
            final XPathFactory xpathFactory = XPathFactory.newInstance();
            final XPath xpath = xpathFactory.newXPath();
            final String xpathStr = String.format(Locale.ROOT, "/enumsConfig/enum[@name='%s']", enumName);
            final NodeList nodes = (NodeList) xpath.evaluate(xpathStr, doc, XPathConstants.NODESET);
            final int nodesLength = nodes.getLength();
            if (nodesLength == 0) {
                String exceptionMessage = String.format(Locale.ENGLISH, "No enum configuration found for enum '%s' in %s.", enumName, enumsConfigFile);
                throw new SolrException(SolrException.ErrorCode.NOT_FOUND, exceptionMessage);
            }
            if (nodesLength > 1) {
                if (log.isWarnEnabled())
                    log.warn("More than one enum configuration found for enum '{}' in {}. The last one was taken.", enumName, enumsConfigFile);
            }
            final Node enumNode = nodes.item(nodesLength - 1);
            final NodeList valueNodes = (NodeList) xpath.evaluate("value", enumNode, XPathConstants.NODESET);
            for (int i = 0; i < valueNodes.getLength(); i++) {
                final Node valueNode = valueNodes.item(i);
                final String valueStr = valueNode.getTextContent();
                if ((valueStr == null) || (valueStr.length() == 0)) {
                    final String exceptionMessage = String.format(Locale.ENGLISH, "A value was defined with an no value in enum '%s' in %s.", enumName, enumsConfigFile);
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, exceptionMessage);
                }
                if (enumStringToIntMap.containsKey(valueStr)) {
                    final String exceptionMessage = String.format(Locale.ENGLISH, "A duplicated definition was found for value '%s' in enum '%s' in %s.", valueStr, enumName, enumsConfigFile);
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, exceptionMessage);
                }
                enumIntToStringMap.put(i, valueStr);
                enumStringToIntMap.put(valueStr, i);
            }
        } catch (ParserConfigurationException | XPathExpressionException | SAXException e) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing enums config.", e);
        }
    } catch (IOException e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error while opening enums config.", e);
    } finally {
        try {
            if (is != null) {
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if ((enumStringToIntMap.size() == 0) || (enumIntToStringMap.size() == 0)) {
        String exceptionMessage = String.format(Locale.ENGLISH, "Invalid configuration was defined for enum '%s' in %s.", enumName, enumsConfigFile);
        throw new SolrException(SolrException.ErrorCode.NOT_FOUND, exceptionMessage);
    }
    args.remove(PARAM_ENUMS_CONFIG);
    args.remove(PARAM_ENUM_NAME);
}
Also used : XPath(javax.xml.xpath.XPath) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SolrException(org.apache.solr.common.SolrException)

Example 97 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project jmeter by apache.

the class TemplateManager method initXStream.

private XStream initXStream() {
    XStream xstream = new XStream(new DomDriver() {

        /**
             * Create the DocumentBuilderFactory instance.
             * See https://blog.compass-security.com/2012/08/secure-xml-parser-configuration/
             * See https://github.com/x-stream/xstream/issues/25
             * @return the new instance
             */
        @Override
        protected DocumentBuilderFactory createDocumentBuilderFactory() {
            final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            try {
                factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
                factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
            } catch (ParserConfigurationException e) {
                throw new StreamException(e);
            }
            factory.setExpandEntityReferences(false);
            return factory;
        }
    });
    xstream.alias("template", Template.class);
    xstream.alias("templates", Templates.class);
    xstream.useAttributeFor(Template.class, "isTestPlan");
    // templates i
    xstream.addImplicitMap(Templates.class, // $NON-NLS-1$
    "templates", Template.class, // $NON-NLS-1$
    "name");
    return xstream;
}
Also used : DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XStream(com.thoughtworks.xstream.XStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) StreamException(com.thoughtworks.xstream.io.StreamException)

Example 98 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project jackrabbit by apache.

the class ParsingContentHandler method parse.

/**
     * Utility method that parses the given input stream using this handler.
     * The parser is namespace-aware and will not resolve external entity
     * references.
     *
     * @param in XML input stream
     * @throws IOException if an I/O error occurs
     * @throws SAXException if an XML parsing error occurs
     */
public void parse(InputStream in) throws IOException, SAXException {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.newSAXParser().parse(new InputSource(in), this);
    } catch (ParserConfigurationException e) {
        throw new SAXException("SAX parser configuration error", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 99 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project jackrabbit by apache.

the class WebdavResponseImpl method sendXmlResponse.

/**
     * Send Xml response body.
     *
     * @param serializable
     * @param status
     * @throws IOException
     * @see DavServletResponse#sendXmlResponse(XmlSerializable, int)
     */
public void sendXmlResponse(XmlSerializable serializable, int status) throws IOException {
    httpResponse.setStatus(status);
    if (serializable != null) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            Document doc = DomUtil.createDocument();
            doc.appendChild(serializable.toXml(doc));
            // JCR-2636: Need to use an explicit OutputStreamWriter
            // instead of relying on the built-in UTF-8 serialization
            // to avoid problems with surrogate pairs on Sun JRE 1.5.
            Writer writer = new OutputStreamWriter(out, "UTF-8");
            DomUtil.transformDocument(doc, writer);
            writer.flush();
            // TODO: Should this be application/xml? See JCR-1621
            httpResponse.setContentType("text/xml; charset=UTF-8");
            httpResponse.setContentLength(out.size());
            out.writeTo(httpResponse.getOutputStream());
        } catch (ParserConfigurationException e) {
            log.error(e.getMessage());
            throw new IOException(e.getMessage());
        } catch (TransformerException e) {
            log.error(e.getMessage());
            throw new IOException(e.getMessage());
        } catch (SAXException e) {
            log.error(e.getMessage());
            throw new IOException(e.getMessage());
        }
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Example 100 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project sling by apache.

the class MyErrorHandler method parseXMLDocument.

// --------------------------------------------------------- Public Methods
/**
     * Parse the specified XML document, and return a <code>TreeNode</code>
     * that corresponds to the root node of the document tree.
     *
     * @param uri URI of the XML document being parsed
     * @param is Input source containing the deployment descriptor
     *
     * @exception JasperException if an input/output error occurs
     * @exception JasperException if a parsing error occurs
     */
public TreeNode parseXMLDocument(String uri, InputSource is) throws JasperException {
    Document document = null;
    // Perform an XML parse of this document, via JAXP
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setValidating(validating);
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(entityResolver);
        builder.setErrorHandler(errorHandler);
        document = builder.parse(is);
    } catch (ParserConfigurationException ex) {
        throw new JasperException(Localizer.getMessage("jsp.error.parse.xml", uri), ex);
    } catch (SAXParseException ex) {
        throw new JasperException(Localizer.getMessage("jsp.error.parse.xml.line", uri, Integer.toString(ex.getLineNumber()), Integer.toString(ex.getColumnNumber())), ex);
    } catch (SAXException sx) {
        throw new JasperException(Localizer.getMessage("jsp.error.parse.xml", uri), sx);
    } catch (IOException io) {
        throw new JasperException(Localizer.getMessage("jsp.error.parse.xml", uri), io);
    }
    // Convert the resulting document to a graph of TreeNodes
    return (convert(null, document.getDocumentElement()));
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) JasperException(org.apache.sling.scripting.jsp.jasper.JasperException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Aggregations

ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1353 SAXException (org.xml.sax.SAXException)975 IOException (java.io.IOException)891 Document (org.w3c.dom.Document)710 DocumentBuilder (javax.xml.parsers.DocumentBuilder)631 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)569 Element (org.w3c.dom.Element)372 InputSource (org.xml.sax.InputSource)246 NodeList (org.w3c.dom.NodeList)226 Node (org.w3c.dom.Node)210 SAXParser (javax.xml.parsers.SAXParser)175 TransformerException (javax.xml.transform.TransformerException)163 File (java.io.File)162 InputStream (java.io.InputStream)158 SAXParserFactory (javax.xml.parsers.SAXParserFactory)137 ByteArrayInputStream (java.io.ByteArrayInputStream)129 StringReader (java.io.StringReader)117 ArrayList (java.util.ArrayList)115 DOMSource (javax.xml.transform.dom.DOMSource)109 StreamResult (javax.xml.transform.stream.StreamResult)93