List of usage examples for javax.xml.parsers SAXParserFactory setFeature
public abstract void setFeature(String name, boolean value) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException;
From source file:org.jajuk.base.Collection.java
/** * Parse collection.xml file and put all collection information into memory * * @param file /*w w w .ja v a2s . co m*/ * * @throws SAXException the SAX exception * @throws ParserConfigurationException the parser configuration exception * @throws JajukException the jajuk exception * @throws IOException Signals that an I/O exception has occurred. */ public static void load(File file) throws SAXException, ParserConfigurationException, JajukException, IOException { // If we load the regular collection.xml file, try to recover it from previous crash java.io.File regularFile = SessionService.getConfFileByPath(Const.FILE_COLLECTION); if (file.equals(regularFile)) { UtilSystem.recoverFileIfRequired(regularFile); } Log.debug("Loading: " + file.getName()); if (!file.exists()) { throw new JajukException(5, file.toString()); } lTime = System.currentTimeMillis(); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setValidating(false); spf.setNamespaceAware(false); // See http://xerces.apache.org/xerces-j/features.html for details spf.setFeature("http://xml.org/sax/features/external-general-entities", false); spf.setFeature("http://xml.org/sax/features/string-interning", true); SAXParser saxParser = spf.newSAXParser(); saxParser.parse(file.toURI().toURL().toString(), getInstance()); }
From source file:org.onehippo.repository.bootstrap.instructions.ContentResourceInstruction.java
InputStream getPartialContentInputStream(InputStream in, final String contextRelPath) throws IOException, RepositoryException { File file = File.createTempFile("bootstrap-", ".xml"); OutputStream out = null;/* w w w . j av a 2 s.c o m*/ try { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setFeature("http://xml.org/sax/features/namespace-prefixes", false); SAXParser parser = factory.newSAXParser(); out = new FileOutputStream(file); TransformerHandler handler = ((SAXTransformerFactory) SAXTransformerFactory.newInstance()) .newTransformerHandler(); Transformer transformer = handler.getTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); handler.setResult(new StreamResult(out)); parser.parse(new InputSource(in), new DefaultContentHandler(new PartialSystemViewFilter(handler, contextRelPath))); return new TempFileInputStream(file); } catch (FactoryConfigurationError e) { throw new RepositoryException("SAX parser implementation not available", e); } catch (ParserConfigurationException e) { throw new RepositoryException("SAX parser configuration error", e); } catch (SAXException e) { Exception exception = e.getException(); if (exception instanceof RepositoryException) { throw (RepositoryException) exception; } else if (exception instanceof IOException) { throw (IOException) exception; } else { throw new InvalidSerializedDataException("Error parsing XML import", e); } } catch (TransformerConfigurationException e) { throw new RepositoryException("SAX transformation error", e); } finally { IOUtils.closeQuietly(out); } }
From source file:org.onehippo.repository.bootstrap.util.ContentFileInfo.java
public static ContentFileInfo readInfo(final Node item) { ContentFileInfoReader contentFileInfoReader = null; InputStream in = null;//from ww w.j a v a 2 s . c o m String contentResource = null; try { contentResource = StringUtils.trim(item.getProperty(HIPPO_CONTENTRESOURCE).getString()); if (contentResource.endsWith(".zip") || contentResource.endsWith(".jar")) { return null; } final String contentRoot = StringUtils.trim(item.getProperty(HIPPO_CONTENTROOT).getString()); contentFileInfoReader = new ContentFileInfoReader(contentRoot); final URL contentResourceURL = getResource(item, contentResource); if (contentResourceURL != null) { in = contentResourceURL.openStream(); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setFeature("http://xml.org/sax/features/namespace-prefixes", false); factory.newSAXParser().parse(new InputSource(in), contentFileInfoReader); } } catch (ContentFileInfoReadingShortCircuitException ignore) { } catch (FactoryConfigurationError | SAXException | ParserConfigurationException | IOException | RepositoryException e) { BootstrapConstants.log.error("Could not read root node name from content file {}", contentResource, e); } finally { IOUtils.closeQuietly(in); } return contentFileInfoReader == null ? null : contentFileInfoReader.getContentFileInfo(); }
From source file:org.onehippo.repository.bootstrap.util.PartialSystemViewFilterTest.java
public String getPartialContent(InputStream in, String startPath) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true);//from ww w . j av a 2 s.co m factory.setFeature("http://xml.org/sax/features/namespace-prefixes", false); SAXParser parser = factory.newSAXParser(); StringWriter out = new StringWriter(); TransformerHandler handler = ((SAXTransformerFactory) SAXTransformerFactory.newInstance()) .newTransformerHandler(); Transformer transformer = handler.getTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); handler.setResult(new StreamResult(out)); parser.parse(new InputSource(in), new DefaultContentHandler(new PartialSystemViewFilter(handler, startPath))); return out.toString(); }
From source file:org.openo.sdnhub.common.restconf.SerializeUtil.java
/** * Change the XML object to the class type.<br> * * @param xml is the object to be changed * @param clazz the class type that the XML is changed to * @return the object that the XML is changed to * @service ServiceException//from w ww .j a v a 2 s . c o m * @since SDNHUB 0.5 */ public static <T> T fromXml(String xml, Class<T> clazz) throws ServiceException { String sError = "formXml failed."; if (StringUtils.isEmpty(xml)) { return null; } try { String trimXml = trimBodyStr(xml); JAXBContext context = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = context.createUnmarshaller(); StringReader reader = new StringReader(trimXml); SAXParserFactory sax = SAXParserFactory.newInstance(); sax.setNamespaceAware(false); String feature = "http://apache.org/xml/features/disallow-doctype-decl"; sax.setFeature(feature, true); feature = "http://xml.org/sax/features/external-general-entities"; sax.setFeature(feature, false); feature = "http://xml.org/sax/features/external-parameter-entities"; sax.setFeature(feature, false); XMLReader xmlReader = sax.newSAXParser().getXMLReader(); Source source = new SAXSource(xmlReader, new InputSource(reader)); return (T) unmarshaller.unmarshal(source); } catch (JAXBException | SAXException | ParserConfigurationException e) { LOGGER.error(sError, e); throw new ServiceException(sError, e); } }
From source file:org.openstreetmap.josm.tools.Utils.java
/** * Returns a new secure SAX parser, supporting XML namespaces. * @return a new secure SAX parser, supporting XML namespaces * @throws ParserConfigurationException if a parser cannot be created which satisfies the requested configuration. * @throws SAXException for SAX errors./*w ww . j a v a 2 s . co m*/ * @since 8287 */ public static SAXParser newSafeSAXParser() throws ParserConfigurationException, SAXException { SAXParserFactory parserFactory = SAXParserFactory.newInstance(); parserFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); parserFactory.setNamespaceAware(true); return parserFactory.newSAXParser(); }
From source file:org.pentaho.di.core.xml.XMLParserFactoryProducer.java
/** * Creates an instance of {@link SAXParserFactory} class with enabled {@link XMLConstants#FEATURE_SECURE_PROCESSING} property. * Enabling this feature prevents from some XXE attacks (e.g. XML bomb) * * @throws ParserConfigurationException if a parser cannot * be created which satisfies the requested configuration. * * @throws SAXNotRecognizedException When the underlying XMLReader does * not recognize the property name. * * @throws SAXNotSupportedException When the underlying XMLReader * recognizes the property name but doesn't support the * property.//from w w w .j av a2 s . c o m */ public static SAXParserFactory createSecureSAXParserFactory() throws SAXNotSupportedException, SAXNotRecognizedException, ParserConfigurationException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, 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); return factory; }
From source file:org.plasma.sdo.helper.PlasmaXMLHelper.java
private void validateSAX(InputStream inputStream, String locationURI, XMLOptions options) throws IOException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true);/*from w w w . j a v a2 s. co m*/ factory.setNamespaceAware(true); SAXParser parser; try { factory.setFeature("http://xml.org/sax/features/validation", true); factory.setFeature("http://apache.org/xml/features/validation/schema", true); factory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true); parser = factory.newSAXParser(); try { parser.setProperty(XMLConstants.JAXP_SCHEMA_LANGUAGE, SchemaConstants.XMLSCHEMA_NAMESPACE_URI); } catch (SAXNotRecognizedException e) { log.warn("parses does not support JAXP 1.2"); } if (locationURI != null) { parser.setProperty(XMLConstants.JAXP_NO_NAMESPACE_SCHEMA_SOURCE, locationURI); } XMLReader xmlReader = parser.getXMLReader(); //xmlReader.setEntityResolver(new SchemaLoader()); if (options.getErrorHandler() == null) xmlReader.setErrorHandler(new DefaultErrorHandler(options)); else xmlReader.setErrorHandler(options.getErrorHandler()); if (log.isDebugEnabled()) log.debug("validating..."); xmlReader.parse(new InputSource(inputStream)); } catch (SAXNotRecognizedException e) { throw new PlasmaDataObjectException(e); } catch (SAXNotSupportedException e) { throw new PlasmaDataObjectException(e); } catch (ParserConfigurationException e) { throw new PlasmaDataObjectException(e); } catch (SAXException e) { throw new PlasmaDataObjectException(e); } }
From source file:org.sonar.java.xml.XmlParser.java
private static void disableXmlValidation(SAXParserFactory factory) throws ParserConfigurationException, SAXException { factory.setValidating(false);// ww w . j a va 2s. com factory.setFeature("http://xml.org/sax/features/validation", false); factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); factory.setFeature("http://xml.org/sax/features/external-general-entities", false); factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); }
From source file:org.unitils.dbunit.datasetfactory.DataSetFactoryFactory.java
protected void disableValidation(SAXParserFactory saxParserFactory) { saxParserFactory.setValidating(false); try {/*from w w w. j a v a 2s.c om*/ saxParserFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); } catch (Exception e) { logger.debug( "Unable to set http://xml.org/sax/features/external-parameter-entities feature on SAX parser factory to false. Ignoring exception: " + e.getMessage()); } try { saxParserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (Exception e) { logger.debug( "Unable to set http://apache.org/xml/features/nonvalidating/load-external-dtd feature on SAX parser factory to false. Ignoring exception: " + e.getMessage()); } }