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);
}
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;
}
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);
}
}
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());
}
}
}
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()));
}
Aggregations