List of usage examples for javax.xml.parsers DocumentBuilderFactory setFeature
public abstract void setFeature(String name, boolean value) throws ParserConfigurationException;
From source file:com.esri.geoportal.harvester.migration.MigrationDataBuilder.java
private Document strToDom(String strXml) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); factory.setFeature("http://xml.org/sax/features/external-general-entities", false); factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); factory.setXIncludeAware(false);/* www . j a v a 2s . co m*/ factory.setExpandEntityReferences(false); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(new InputSource(new StringReader(strXml))); }
From source file:com.norconex.committer.gsa.XmlOutputTest.java
@Test public void testWriteMultipleRecords() throws Exception { File file = tempFolder.newFile(); FileOutputStream out = new FileOutputStream(file); XmlOutput xmlOutput = new XmlOutput(out); Properties metadata1 = new Properties(); metadata1.addString("url", "http://www.corp.enterprise.com/hello01"); metadata1.addString("mimetype", "text/plain"); metadata1.addString("collector.content-type", "text/plain"); metadata1.addString("last-modified", "Tue, 6 Nov 2007 12:45:26 GMT"); metadata1.addString("Date", "Tue, 6 Nov 2007 12:45:26 GMT"); String content1 = "This is hello01"; Properties metadata2 = new Properties(); metadata2.addString("url", "http://www.corp.enterprise.com/hello02"); metadata2.addString("mimetype", "text/plain"); metadata2.addString("collector.content-type", "text/plain"); metadata2.addString("last-modified", "Tue, 6 Nov 2009 22:45:26 GMT"); metadata2.addString("Date", "Tue, 6 Nov 2009 22:45:26 GMT"); String content2 = "This is hello02"; xmlOutput.write(Arrays.asList(buildMockAddOperation(metadata1, content1), buildMockAddOperation(metadata2, content2))); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); Document doc = dbf.newDocumentBuilder().parse(file); XPath xpath = XPathFactory.newInstance().newXPath(); NodeList nodes = (NodeList) xpath.evaluate("/gsafeed/group/record", doc, XPathConstants.NODESET); assertEquals(2, nodes.getLength());//from ww w.j a v a2 s . c o m }
From source file:org.solmix.runtime.support.spring.TunedDocumentLoader.java
@Override protected DocumentBuilderFactory createDocumentBuilderFactory(int validationMode, boolean namespaceAware) throws ParserConfigurationException { DocumentBuilderFactory factory = super.createDocumentBuilderFactory(validationMode, namespaceAware); try {//from w w w .j av a 2s .c o m factory.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false); } catch (Throwable e) { // we can get all kinds of exceptions from this // due to old copies of Xerces and whatnot. } return factory; }
From source file:com.qualinsight.plugins.sonarqube.badges.internal.SVGImageFontReplacer.java
/** * IoC constructor//from w w w . j a v a2 s. c o m * * @throws SVGImageFontReplacementException if a problem occurs during initialization */ public SVGImageFontReplacer() throws SVGImageFontReplacementException { try { InputStream xslInputStream = null; try { xslInputStream = getClass().getClassLoader() .getResourceAsStream("com/qualinsight/plugins/sonarqube/badges/internal/svg.xsl"); final TransformerFactory transformerFactory = TransformerFactory.newInstance(); this.transformer = transformerFactory.newTransformer(new StreamSource(xslInputStream)); } finally { if (null != xslInputStream) { xslInputStream.close(); } } final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setValidating(false); builderFactory.setNamespaceAware(true); builderFactory.setFeature("http://xml.org/sax/features/namespaces", false); builderFactory.setFeature("http://xml.org/sax/features/validation", false); builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); this.builder = builderFactory.newDocumentBuilder(); } catch (final IOException | TransformerConfigurationException | ParserConfigurationException e) { throw new SVGImageFontReplacementException(e); } }
From source file:com.qualinsight.plugins.sonarqube.badges.ws.SVGImageMinimizer.java
/** * IoC constructor//from ww w . j a v a 2 s . c o m * * @throws SVGImageMinimizerException if a problem occurs during initialization */ public SVGImageMinimizer() throws SVGImageMinimizerException { try { InputStream xslInputStream = null; try { xslInputStream = getClass().getClassLoader() .getResourceAsStream("com/qualinsight/plugins/sonarqube/badges/ws/svg-minimizer.xsl"); final TransformerFactory transformerFactory = TransformerFactory.newInstance(); this.transformer = transformerFactory.newTransformer(new StreamSource(xslInputStream)); } finally { if (null != xslInputStream) { xslInputStream.close(); } } final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setValidating(false); builderFactory.setNamespaceAware(true); builderFactory.setFeature("http://xml.org/sax/features/namespaces", false); builderFactory.setFeature("http://xml.org/sax/features/validation", false); builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); this.builder = builderFactory.newDocumentBuilder(); } catch (final IOException | TransformerConfigurationException | ParserConfigurationException e) { throw new SVGImageMinimizerException(e); } }
From source file:hydrograph.ui.dataviewer.utilities.ViewDataSchemaHelper.java
/** * This function will read schema file and return schema fields * @param schemaFilePath//from w w w . ja va2 s . co m * @return Fields */ public Fields getFieldsFromSchema(String schemaFilePath) { Fields fields = null; if (StringUtils.isNotBlank(schemaFilePath)) { String filePath = ((IPath) new Path(schemaFilePath)).removeFileExtension() .addFileExtension(Constants.XML_EXTENSION_FOR_IPATH).toString(); File file = new File(filePath); if (file.exists()) { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setExpandEntityReferences(false); builderFactory.setNamespaceAware(true); builderFactory.setFeature(Constants.DISALLOW_DOCTYPE_DECLARATION, true); DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder(); Document document = documentBuilder.parse(file); JAXBContext jaxbContext = JAXBContext.newInstance(Schema.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Schema schema = (Schema) jaxbUnmarshaller.unmarshal(document); fields = schema.getFields(); for (Field field : fields.getField()) { logger.debug("Type:{}, Name:{}, Format:{}" + field.getType(), field.getName(), field.getFormat()); } } catch (JAXBException | ParserConfigurationException | SAXException | IOException exception) { logger.error("Invalid xml file: ", exception); } } } return fields; }
From source file:com.ebay.jetstream.event.processor.esper.raw.EsperTestConfigurationValidator.java
public boolean validate(String instance, String schema) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try {/*from w ww .ja va2 s. co m*/ warnings = 0; errors = 0; fatalErrors = 0; try { //Set the validation feature factory.setFeature("http://xml.org/sax/features/validation", true); //Set the schema validation feature factory.setFeature("http://apache.org/xml/features/validation/schema", true); //Set schema full grammar checking factory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true); // If there is an external schema set it if (schema != null) { SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema s = sf.newSchema(new StreamSource(schema)); factory.setSchema(s); } DocumentBuilder parser = factory.newDocumentBuilder(); parser.setErrorHandler(this); // Parse and validate parser.parse(instance); // Return true if we made it this far with no errors return ((errors == 0) && (fatalErrors == 0)); } catch (SAXException e) { log.error("Could not activate validation features - " + e.getMessage()); } } catch (Exception e) { log.error(e.getMessage()); } return false; }
From source file:de.uzk.hki.da.metadata.XsltGenerator.java
/** * @return/*from w w w . ja va2s .c o m*/ * @throws TransformerException * @throws IOException */ public String generate() throws TransformerException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setValidating(false); dbf.setNamespaceAware(true); try { dbf.setFeature("http://xml.org/sax/features/namespaces", false); dbf.setFeature("http://xml.org/sax/features/validation", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (ParserConfigurationException e2) { throw new RuntimeException(e2); } DocumentBuilder parser; Document doc = null; try { parser = dbf.newDocumentBuilder(); doc = parser.parse(inputStream); } catch (Exception e) { throw new RuntimeException(e); } Source xmlSource = new DOMSource(doc); for (String key : params.keySet()) { transformer.setParameter(key, params.get(key)); } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); transformer.transform(xmlSource, new StreamResult(outputStream)); try { return outputStream.toString("UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } finally { inputStream.close(); outputStream.close(); } }
From source file:au.csiro.casda.sodalint.ValidateCapabilities.java
/** * Validate the content of the capabilities element. * /*from ww w. j a v a 2 s .com*/ * @param reporter * validation message destination * @param xmlContent * The capabilities XML text * @param sodaService * The service being tested. */ void validateCapabilities(Reporter reporter, String xmlContent, SodaService sodaService) { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setValidating(false); builderFactory.setNamespaceAware(true); builderFactory.setFeature("http://xml.org/sax/features/validation", false); builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); DocumentBuilder builder = builderFactory.newDocumentBuilder(); byte[] bytes = xmlContent.getBytes("UTF-8"); ByteArrayInputStream is = new ByteArrayInputStream(bytes); Document document = builder.parse(is); checkForSyncAsync(reporter, document, sodaService); } catch (ParserConfigurationException | UnsupportedEncodingException | XPathExpressionException e) { reporter.report(SodaCode.E_CPRS, "Unexpected error processing capability", e); } catch (SAXException e) { reporter.report(FixedCode.E_CPSX, "Error parsing capabilities metadata", e); } catch (IOException e) { reporter.report(FixedCode.E_CPIO, "Error reading capabilities metadata", e); } }
From source file:demo.SourceHttpMessageConverter.java
private DOMSource readDOMSource(InputStream body) throws IOException { try {/*from w ww . j a v a2s . c o m*/ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities()); documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd()); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); if (!isProcessExternalEntities()) { documentBuilder.setEntityResolver(NO_OP_ENTITY_RESOLVER); } Document document = documentBuilder.parse(body); return new DOMSource(document); } catch (ParserConfigurationException ex) { throw new HttpMessageNotReadableException("Could not set feature: " + ex.getMessage(), ex); } catch (SAXException ex) { throw new HttpMessageNotReadableException("Could not parse document: " + ex.getMessage(), ex); } }