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.apache.fop.cli.InputHandler.java
private XMLReader getXMLReader() throws ParserConfigurationException, SAXException { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://xml.org/sax/features/namespaces", true); spf.setFeature("http://apache.org/xml/features/xinclude", true); XMLReader xr = spf.newSAXParser().getXMLReader(); return xr;/*from ww w . j a va2 s . co m*/ }
From source file:org.apache.geode.internal.cache.xmlcache.CacheXmlParser.java
/** * Parses XML data and from it creates an instance of <code>CacheXmlParser</code> that can be used * to {@link #create}the {@link Cache}, etc. * * @param is the <code>InputStream</code> of XML to be parsed * * @return a <code>CacheXmlParser</code>, typically used to create a cache from the parsed XML * * @throws CacheXmlException Something went wrong while parsing the XML * * @since GemFire 4.0// www . java 2 s . com * */ public static CacheXmlParser parse(InputStream is) { /** * The API doc http://java.sun.com/javase/6/docs/api/org/xml/sax/InputSource.html for the SAX * InputSource says: "... standard processing of both byte and character streams is to close * them on as part of end-of-parse cleanup, so applications should not attempt to re-use such * streams after they have been handed to a parser." * * In order to block the parser from closing the stream, we wrap the InputStream in a filter, * i.e., UnclosableInputStream, whose close() function does nothing. * */ class UnclosableInputStream extends BufferedInputStream { public UnclosableInputStream(InputStream stream) { super(stream); } @Override public void close() { } } CacheXmlParser handler = new CacheXmlParser(); try { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setFeature(DISALLOW_DOCTYPE_DECL_FEATURE, true); factory.setValidating(true); factory.setNamespaceAware(true); UnclosableInputStream bis = new UnclosableInputStream(is); try { SAXParser parser = factory.newSAXParser(); // Parser always reads one buffer plus a little extra worth before // determining that the DTD is there. Setting mark twice the parser // buffer size. bis.mark((Integer) parser.getProperty(BUFFER_SIZE) * 2); parser.setProperty(JAXP_SCHEMA_LANGUAGE, XMLConstants.W3C_XML_SCHEMA_NS_URI); parser.parse(bis, new DefaultHandlerDelegate(handler)); } catch (CacheXmlException e) { if (null != e.getCause() && e.getCause().getMessage().contains(DISALLOW_DOCTYPE_DECL_FEATURE)) { // Not schema based document, try dtd. bis.reset(); factory.setFeature(DISALLOW_DOCTYPE_DECL_FEATURE, false); SAXParser parser = factory.newSAXParser(); parser.parse(bis, new DefaultHandlerDelegate(handler)); } else { throw e; } } return handler; } catch (Exception ex) { if (ex instanceof CacheXmlException) { while (true /* ex instanceof CacheXmlException */) { Throwable cause = ex.getCause(); if (!(cause instanceof CacheXmlException)) { break; } else { ex = (CacheXmlException) cause; } } throw (CacheXmlException) ex; } else if (ex instanceof SAXException) { // Silly JDK 1.4.2 XML parser wraps RunTime exceptions in a // SAXException. Pshaw! SAXException sax = (SAXException) ex; Exception cause = sax.getException(); if (cause instanceof CacheXmlException) { while (true /* cause instanceof CacheXmlException */) { Throwable cause2 = cause.getCause(); if (!(cause2 instanceof CacheXmlException)) { break; } else { cause = (CacheXmlException) cause2; } } throw (CacheXmlException) cause; } } throw new CacheXmlException(LocalizedStrings.CacheXmlParser_WHILE_PARSING_XML.toLocalizedString(), ex); } }
From source file:org.apache.jackrabbit.jcr2spi.SessionImpl.java
/** * @see javax.jcr.Session#importXML(String, java.io.InputStream, int) *///from ww w . j a v a2 s . co m @Override public void importXML(String parentAbsPath, InputStream in, int uuidBehavior) throws IOException, PathNotFoundException, ItemExistsException, ConstraintViolationException, VersionException, InvalidSerializedDataException, LockException, RepositoryException { // NOTE: checks are performed by 'getImportContentHandler' ImportHandler handler = (ImportHandler) getImportContentHandler(parentAbsPath, uuidBehavior); try { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setFeature("http://xml.org/sax/features/namespace-prefixes", false); SAXParser parser = factory.newSAXParser(); parser.parse(new InputSource(in), handler); } catch (SAXException se) { // check for wrapped repository exception Exception e = se.getException(); if (e != null && e instanceof RepositoryException) { throw (RepositoryException) e; } else { String msg = "failed to parse XML stream"; log.debug(msg); throw new InvalidSerializedDataException(msg, se); } } catch (ParserConfigurationException e) { throw new RepositoryException("SAX parser configuration error", e); } finally { in.close(); // JCR-2903 } }
From source file:org.apache.oozie.util.GraphGenerator.java
/** * Stream the PNG file to client//from ww w . j av a 2 s .co m * @param out * @throws Exception */ public void write(OutputStream out) throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://xml.org/sax/features/external-general-entities", false); spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false); spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); spf.setNamespaceAware(true); SAXParser saxParser = spf.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler(new XMLParser(out)); xmlReader.parse(new InputSource(new StringReader(xml))); }
From source file:org.apache.tomcat.util.digester.XercesParser.java
/** * Configure schema validation as recommended by the Xerces spec. * Both DTD and Schema validation will be enabled simultaneously. * @param factory SAXParserFactory to be configured *//*from w w w . j ava2 s .c om*/ private static void configureXerces(SAXParserFactory factory) throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException { factory.setFeature(XERCES_DYNAMIC, true); factory.setFeature(XERCES_SCHEMA, true); }
From source file:org.easyxml.parser.EasySAXParser.java
/** * // w w w .j ava 2s . c o m * Parse XML as an InputSource. If external schema is referred, then it * would fail to locate the DTD file. * * @param is * - InputSource to be parsed. * * @return org.easyxml.xml.Document instance if it is parsed successfully, * otherwise null. */ public static Document parse(InputSource is) { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); try { saxParserFactory.setValidating(true); saxParserFactory.setFeature("http://apache.org/xml/features/validation/schema", true); SAXParser saxParser = saxParserFactory.newSAXParser(); EasySAXParser handler = new EasySAXParser(); saxParser.parse(is, handler); return handler.getDocument(); } catch (SAXException | IOException | ParserConfigurationException e) { e.printStackTrace(); } return null; }
From source file:org.eclim.plugin.core.util.XmlUtils.java
/** * Validate the supplied xml file.//from w w w . j a va2 s . c om * * @param project The project name. * @param filename The file path to the xml file. * @param schema True to use schema validation relying on the * xsi:schemaLocation attribute of the document. * @param handler The content handler to use while parsing the file. * @return A possibly empty list of errors. */ public static List<Error> validateXml(String project, String filename, boolean schema, DefaultHandler handler) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(true); if (schema) { factory.setFeature("http://apache.org/xml/features/validation/schema", true); factory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true); } SAXParser parser = factory.newSAXParser(); filename = ProjectUtils.getFilePath(project, filename); filename = filename.replace('\\', '/'); ErrorAggregator errorHandler = new ErrorAggregator(filename); EntityResolver entityResolver = new EntityResolver(FileUtils.getFullPath(filename)); try { parser.parse(new File(filename), getHandler(handler, errorHandler, entityResolver)); } catch (SAXParseException spe) { ArrayList<Error> errors = new ArrayList<Error>(); errors.add(new Error(spe.getMessage(), filename, spe.getLineNumber(), spe.getColumnNumber(), false)); return errors; } return errorHandler.getErrors(); }
From source file:org.eclim.plugin.core.util.XmlUtils.java
/** * Validate the supplied xml file against the specified xsd. * * @param project The project name./*from w ww .j a va 2 s .c o m*/ * @param filename The file path to the xml file. * @param schema The file path to the xsd. * @return A possibly empty array of errors. */ public static List<Error> validateXml(String project, String filename, String schema) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(true); factory.setFeature("http://apache.org/xml/features/validation/schema", true); factory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true); SAXParser parser = factory.newSAXParser(); parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); if (!schema.startsWith("file:")) { schema = "file://" + schema; } parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", schema); parser.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", schema.replace('\\', '/')); filename = ProjectUtils.getFilePath(project, filename); filename = filename.replace('\\', '/'); ErrorAggregator errorHandler = new ErrorAggregator(filename); EntityResolver entityResolver = new EntityResolver(FileUtils.getFullPath(filename)); try { parser.parse(new File(filename), getHandler(null, errorHandler, entityResolver)); } catch (SAXParseException spe) { ArrayList<Error> errors = new ArrayList<Error>(); errors.add(new Error(spe.getMessage(), filename, spe.getLineNumber(), spe.getColumnNumber(), false)); return errors; } return errorHandler.getErrors(); }
From source file:org.formix.dsx.XmlElement.java
private static SAXParser createSaxParser() throws ParserConfigurationException, SAXNotRecognizedException, SAXNotSupportedException, SAXException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false);//from w w w. j a va 2 s. c o m factory.setValidating(false); factory.setFeature("http://xml.org/sax/features/namespaces", false); 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); SAXParser parser = factory.newSAXParser(); return parser; }
From source file:org.geosdi.geoplatform.services.GPWMSServiceImpl.java
/** * @param serverURL/* w w w . j av a 2 s .c o m*/ * @param layerName * @return {@link GPLayerTypeResponse} * @throws Exception */ @Override public GPLayerTypeResponse getLayerType(String serverURL, String layerName) throws Exception { checkArgument((serverURL != null) && !(serverURL.trim().isEmpty()), "The Parameter serverURL must not be null or an empty string."); checkArgument((layerName != null) && !(layerName.trim().isEmpty()), "The Parameter layerName must not be null or an empty string."); logger.debug( "###########################TRYING TO RETRIEVE LAYER_TYPE with serverURL : {} - layerName : {]\n", serverURL, layerName); int index = serverURL.indexOf("?"); if (index != -1) { serverURL = serverURL.substring(0, index); } String decribeLayerUrl = serverURL.concat("?service=WMS&request=DescribeLayer&version=1.1.1&layers=") .concat(layerName); logger.info("#########################DESCRIBE_LAYER_URL : {}\n", decribeLayerUrl); try { HttpClient httpClient = new HttpClient(); GetMethod getMethod = new GetMethod(decribeLayerUrl); httpClient.executeMethod(getMethod); InputStream inputStream = getMethod.getResponseBodyAsStream(); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", FALSE); spf.setFeature("http://xml.org/sax/features/validation", FALSE); XMLReader xmlReader = spf.newSAXParser().getXMLReader(); InputSource inputSource = new InputSource(new InputStreamReader(inputStream)); SAXSource source = new SAXSource(xmlReader, inputSource); JAXBContext jaxbContext = JAXBContext.newInstance(WMSDescribeLayerResponse.class); WMSDescribeLayerResponse describeLayerResponse = (WMSDescribeLayerResponse) jaxbContext .createUnmarshaller().unmarshal(source); return new GPLayerTypeResponse(describeLayerResponse); } catch (Exception ex) { ex.printStackTrace(); throw new ServerInternalFault(ex.getMessage()); } }