@Override protected String getDocumentText(String absolutePath, String encoding) throws IOException { String text; try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setValidating(false); spf.setNamespaceAware(true); spf.setFeature("http://xml.org/sax/features/namespaces", true); spf.setFeature("http://xml.org/sax/features/validation", false); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); SAXParser saxParser = spf.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); TeiToTxtSaxHandler handler = new TeiToTxtSaxHandler(); xmlReader.setContentHandler(handler); xmlReader.parse(new InputSource(absolutePath)); text = handler.getText(); } catch (ParserConfigurationException | SAXException e) { logger.error("Could not read TEI document: {}", absolutePath); logger.warn("Ignoring TEI document " + absolutePath); text = ""; this.failedFileCounter++; } return text; }
/** * {@link SAXParserFactoryUtil#setXIncludeAware}のテストです。 * * @throws Exception */ public void testSetXIncludeAware() throws Exception { SAXParserFactory spf = SAXParserFactoryUtil.newInstance(); SAXParserFactoryUtil.setXIncludeAware(spf, true); spf.setNamespaceAware(true); SAXParser parser = SAXParserFactoryUtil.newSAXParser(spf); InputSource is = new InputSource(ResourceUtil.getResourceAsStream("org/seasar/framework/util/include.xml")); is.setSystemId("include.xml"); parser.parse( is, new DefaultHandler() { @Override public void startElement( String uri, String localName, String qName, Attributes attributes) throws SAXException { if ("bar".equals(qName)) { included = true; } } @Override public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException { InputSource is = new InputSource( ResourceUtil.getResourceAsStream("org/seasar/framework/util/included.xml")); is.setSystemId("included.xml"); return is; } }); assertTrue(included); }
/** * Create a SAX parser from the JAXP factory. The result can be used to parse XML files. * * <p>See class Javadoc for hints on setting an entity resolver. This parser has its entity * resolver set to the system entity resolver chain. * * @param validate if true, a validating parser is returned * @param namespaceAware if true, a namespace aware parser is returned * @throws FactoryConfigurationError Application developers should never need to directly catch * errors of this type. * @throws SAXException if a parser fulfilling given parameters can not be created * @return XMLReader configured according to passed parameters */ public static XMLReader createXMLReader(boolean validate, boolean namespaceAware) throws SAXException { SAXParserFactory factory; if (!validate && useFastSAXParserFactory) { try { factory = createFastSAXParserFactory(); } catch (ParserConfigurationException ex) { factory = SAXParserFactory.newInstance(); } catch (SAXException ex) { factory = SAXParserFactory.newInstance(); } } else { useFastSAXParserFactory = false; factory = SAXParserFactory.newInstance(); } factory.setValidating(validate); factory.setNamespaceAware(namespaceAware); try { return factory.newSAXParser().getXMLReader(); } catch (ParserConfigurationException ex) { throw new SAXException( "Cannot create parser satisfying configuration parameters", ex); // NOI18N } }
/** Private constructor */ private XMLReaders(int validate) { SAXParserFactory fac = SAXParserFactory.newInstance(); boolean val = false; // All JDOM parsers are namespace aware. fac.setNamespaceAware(true); switch (validate) { case 0: fac.setValidating(false); break; case 1: fac.setValidating(true); val = true; break; case 2: fac.setValidating(false); try { SchemaFactory sfac = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sfac.newSchema(); fac.setSchema(schema); val = true; } catch (SAXException se) { // we could not get a validating system, set the fac to null fac = null; } break; } jaxpfactory = fac; validates = val; }
public GpxReader() throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException { javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance(); factory.setNamespaceAware(true); this.parser = factory.newSAXParser(); }
public static SAXParser getSaxParser() throws ParserConfigurationException, SAXException { SAXParserFactory parserFactory = SAXParserFactory.newInstance(); parserFactory.setNamespaceAware(false); parserFactory.setValidating(true); parserFactory.setXIncludeAware(false); return parserFactory.newSAXParser(); }
protected void validateXmlString(final String xml) throws Exception { if (getSchemaFile() == null) { LOG.warn("skipping validation, schema file not set"); return; } final SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); final File schemaFile = new File(getSchemaFile()); LOG.debug("Validating using schema file: {}", schemaFile); final Schema schema = schemaFactory.newSchema(schemaFile); final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setValidating(true); saxParserFactory.setNamespaceAware(true); saxParserFactory.setSchema(schema); assertTrue("make sure our SAX implementation can validate", saxParserFactory.isValidating()); final Validator validator = schema.newValidator(); final ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes()); final Source source = new StreamSource(inputStream); validator.validate(source); }
public static BehavioralPatternsCatalog load(final Reader reader) { BehavioralPatternsCatalog catalog = null; try { final BehavioralPatternsCatalogSaxHandler handler = new BehavioralPatternsCatalogSaxHandler(); final SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); final XMLReader xmlReader = factory.newSAXParser().getXMLReader(); xmlReader.setContentHandler(handler); xmlReader.setErrorHandler(handler); xmlReader.setEntityResolver(handler); xmlReader.parse(new InputSource(reader)); catalog = handler.getCatalog(); reader.close(); } catch (final SAXException e) { BehavioralAnalysisPlugin.logError("Error parsing Behavioral Patterns Catalog.", e); } catch (final ParserConfigurationException e) { BehavioralAnalysisPlugin.logError("Error parsing Behavioral Patterns Catalog.", e); } catch (final IOException e) { BehavioralAnalysisPlugin.logError("Error opening Behavioral Patterns Catalog.", e); } return catalog; }
public static ParentalControlConcern parse(File file) { try { log.info("Parsing '" + file.getCanonicalPath() + "'..."); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(true); SAXParser parser = factory.newSAXParser(); parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); ParentalControlConcernHandler handler = new ParentalControlConcernHandler(); parser.parse(file, handler); log.info("Parsed '" + file.getCanonicalPath() + "'"); return handler.result; } catch (SAXParseException ex) { log.error( "Could not validate the parental control concern XML file! Validation error follows."); log.error(ex.getMessage()); ex.printStackTrace(); return null; } catch (Exception ex) { log.error(ex); return null; } }
/** * Return a <code>SAXParserFactory</code> instance that is non-validating and is namespace * aware. * * @return configured <code>SAXParserFactory</code> */ private SAXParserFactory getConfiguredFactory() { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); return factory; } // END getConfiguredFactory
/** * Test XMLSequence reading using XML Parsers. * * @throws Exception if something goes wrong */ public void testXMLSequence() throws Exception { File testFile = new File(testDir, "XMLSequence.txt"); XMLSequence sequence = new XMLSequence(new BufferedInputStream(new FileInputStream(testFile))); if (!sequence.markSupported()) throw new AssertionFailedError("Mark is not supported for XMLSequence"); sequence.mark((int) testFile.length() + 1); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); XMLReader xmlReader = factory.newSAXParser().getXMLReader(); int i = 0; while (sequence.hasNext()) { InputStream input = sequence.next(); InputSource source = new InputSource(input); xmlReader.parse(source); input.close(); i++; } sequence.reset(); i = 0; while (sequence.hasNext()) { InputStream input = sequence.next(); InputSource source = new InputSource(input); xmlReader.parse(source); input.close(); i++; } sequence.close(); }
private XMLReader getXMLReader(ContentHandler contentHandler, ErrorHandler errorHandler) throws ParserConfigurationException, SAXException { // setup sax factory ; be sure just one instance! SAXParserFactory saxFactory = SAXParserFactory.newInstance(); // Enable validation stuff saxFactory.setValidating(true); saxFactory.setNamespaceAware(true); // Create xml reader SAXParser saxParser = saxFactory.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); // Setup xmlreader xmlReader.setProperty( XMLReaderObjectFactory.APACHE_PROPERTIES_INTERNAL_GRAMMARPOOL, grammarPool); xmlReader.setFeature(Namespaces.SAX_VALIDATION, true); xmlReader.setFeature(Namespaces.SAX_VALIDATION_DYNAMIC, false); xmlReader.setFeature(XMLReaderObjectFactory.APACHE_FEATURES_VALIDATION_SCHEMA, true); xmlReader.setFeature(XMLReaderObjectFactory.APACHE_PROPERTIES_LOAD_EXT_DTD, true); xmlReader.setFeature(Namespaces.SAX_NAMESPACES_PREFIXES, true); xmlReader.setContentHandler(contentHandler); xmlReader.setErrorHandler(errorHandler); return xmlReader; }
@Override public XMLLoader init(SolrParams args) { // Init StAX parser: inputFactory = XMLInputFactory.newInstance(); EmptyEntityResolver.configureXMLInputFactory(inputFactory); inputFactory.setXMLReporter(xmllog); try { // The java 1.6 bundled stax parser (sjsxp) does not currently have a thread-safe // XMLInputFactory, as that implementation tries to cache and reuse the // XMLStreamReader. Setting the parser-specific "reuse-instance" property to false // prevents this. // All other known open-source stax parsers (and the bea ref impl) // have thread-safe factories. inputFactory.setProperty("reuse-instance", Boolean.FALSE); } catch (IllegalArgumentException ex) { // Other implementations will likely throw this exception since "reuse-instance" // isimplementation specific. log.debug("Unable to set the 'reuse-instance' property for the input chain: " + inputFactory); } // Init SAX parser (for XSL): saxFactory = SAXParserFactory.newInstance(); saxFactory.setNamespaceAware(true); // XSL needs this! EmptyEntityResolver.configureSAXParserFactory(saxFactory); xsltCacheLifetimeSeconds = XSLT_CACHE_DEFAULT; if (args != null) { xsltCacheLifetimeSeconds = args.getInt(XSLT_CACHE_PARAM, XSLT_CACHE_DEFAULT); log.info("xsltCacheLifetimeSeconds=" + xsltCacheLifetimeSeconds); } return this; }
/** * Contact the remote WebDAV server and do a PROPFIND lookup, returning a {@link List} of all * scavenged resources. */ private List propfind(Location location) throws IOException { /* Create the new HttpClient instance associated with the location */ final HttpClient client = new HttpClient(location); client.addRequestHeader("Depth", "1"); client.setAcceptableStatus(207).connect("PROPFIND", true); /* Get the XML SAX Parser and parse the output of the PROPFIND */ try { final SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); final SAXParser parser = factory.newSAXParser(); final String systemId = location.toString(); final InputSource source = new InputSource(systemId); final Handler handler = new Handler(location); source.setByteStream(client.getResponseStream()); parser.parse(source, handler); return handler.list; } catch (ParserConfigurationException exception) { Exception throwable = new IOException("Error creating XML parser"); throw (IOException) throwable.initCause(exception); } catch (SAXException exception) { Exception throwable = new IOException("Error creating XML parser"); throw (IOException) throwable.initCause(exception); } finally { client.disconnect(); } }
/** * @see MessageBodyReader#readFrom(Class, Type, MediaType, Annotation[], MultivaluedMap, * InputStream) */ @Override public Object readFrom( Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException { try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setXIncludeAware(isXIncludeAware()); spf.setNamespaceAware(true); spf.setValidating(isValidatingDtd()); spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, isSecureProcessing()); spf.setFeature( "http://xml.org/sax/features/external-general-entities", isExpandingEntityRefs()); spf.setFeature( "http://xml.org/sax/features/external-parameter-entities", isExpandingEntityRefs()); XMLReader reader = spf.newSAXParser().getXMLReader(); JAXBContext jaxbContext = getJaxbContext(type); Unmarshaller um = jaxbContext.createUnmarshaller(); return um.unmarshal(new SAXSource(reader, new InputSource(entityStream))); } catch (Exception e) { throw new IOException("Could not unmarshal to " + type.getName()); } }
/** * 通过XSD(XML Schema)校验XML * * @throws FileNotFoundException */ public boolean validateXMLByXSD(String testXSD, String xmlFileName) throws FileNotFoundException { // String xmlFileName = "Q:\\_dev_stu\\xsdtest\\src\\note.xml"; String xsdFileName = "E:/datashare/" + testXSD; FileInputStream fls = new FileInputStream(new File(xmlFileName)); try { // 创建默认的XML错误处理器 XMLErrorHandler errorHandler = new XMLErrorHandler(); // 获取基于 SAX 的解析器的实例 SAXParserFactory factory = SAXParserFactory.newInstance(); // 解析器在解析时验证 XML 内容。 factory.setValidating(true); // 指定由此代码生成的解析器将提供对 XML 名称空间的支持。 factory.setNamespaceAware(true); // 使用当前配置的工厂参数创建 SAXParser 的一个新实例。 SAXParser parser = factory.newSAXParser(); // 创建一个读取工具 SAXReader xmlReader = new SAXReader(); // 获取要校验xml文档实例 Document xmlDocument = (Document) xmlReader.read(fls); // 设置 XMLReader 的基础实现中的特定属性。核心功能和属性列表可以在 // [url]http://sax.sourceforge.net/?selected=get-set[/url] 中找到。 parser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); parser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaSource", "file:" + xsdFileName); // 创建一个SAXValidator校验工具,并设置校验工具的属性 SAXValidator validator = new SAXValidator(parser.getXMLReader()); // 设置校验工具的错误处理器,当发生错误时,可以从处理器对象中得到错误信息。 validator.setErrorHandler(errorHandler); // 校验 validator.validate(xmlDocument); XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint()); // 如果错误信息不为空,说明校验失败,打印错误信息 if (errorHandler.getErrors().hasContent()) { System.out.println("XML文件校验失败!"); writer.write(errorHandler.getErrors()); return false; } else { System.out.println("XML文件校验成功!"); return true; } } catch (Exception ex) { System.out.println( "XML文件: " + xmlFileName + " 通过XSD文件:" + xsdFileName + "检验失败。\n原因: " + ex.getMessage()); // ex.printStackTrace(); return false; } finally { try { fls.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
private SAXParser getParser() { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setNamespaceAware(true); try { return saxParserFactory.newSAXParser(); } catch (Exception e) { return null; } }
/** * Creates a new instance of the validator. * * @param grammarCache system grammar cache for retrieving grammars used for validation * @throws ParserConfigurationException if the JAXP parser factory is misconfigured. * @throws SAXException if the JAXP parser factory is misconfigured. */ public XMLValidator(XMLGrammarCache grammarCache) throws ParserConfigurationException, SAXException { this.grammarCache = grammarCache; SAXParserFactory parserFactory = SAXParserFactory.newInstance(); parserFactory.setNamespaceAware(true); parserFactory.setValidating(false); saxParser = parserFactory.newSAXParser(); }
/** * Main import method. Pass in an input stream (perhaps from an HTTP POST) and get a list of * PVConfigs. * * @param configFileName * @return * @throws Exception */ public static LinkedList<PVConfig> importEngineConfig(InputStream is) throws Exception { SAXParserFactory sfac = SAXParserFactory.newInstance(); sfac.setNamespaceAware(false); sfac.setValidating(false); SAXParser parser = sfac.newSAXParser(); EngineConfigParser econfig = new EngineConfigParser(); parser.parse(is, econfig); return econfig.pvConfigs; }
/** * Creates a non-validating, non-namespace-aware {@link XMLReader} using the specified {@link * ContentHandler}. * * <p>If the given {@link ContentHandler} is <code>null</code>, the {@link XMLReader} is not * initialised. * * @param handler The content handler to use. * @return The requested {@link XMLReader} * @throws SAXException Should a SAX exception occur * @throws ParserConfigurationException Should a parser config exception occur */ public static XMLReader makeXMLReader(ContentHandler handler) throws SAXException, ParserConfigurationException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); factory.setValidating(false); XMLReader reader = factory.newSAXParser().getXMLReader(); if (handler != null) reader.setContentHandler(handler); return reader; }
private static void parse(final InputSource input, final JDOParser recognizer) throws SAXException, ParserConfigurationException, IOException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); XMLReader parser = factory.newSAXParser().getXMLReader(); parser.setEntityResolver(new JDOEntityResolver()); parser.setContentHandler(recognizer); parser.setErrorHandler(recognizer.getDefaultErrorHandler()); parser.parse(input); }
public AssemblingParser(EntityResolver er, InputSource in) throws ConfigException { this.er = er; this.main = in; factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); TransformerFactory tf = TransformerFactory.newInstance(); if (!tf.getFeature(SAXSource.FEATURE) || !tf.getFeature(SAXResult.FEATURE)) throw new ConfigException("XSLT not supported"); transfactory = (SAXTransformerFactory) tf; }
protected static SAXParser createParser() throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); spf.setValidating(true); SAXParser parser = spf.newSAXParser(); parser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); return parser; }
static { // SAXParserFactory factory = SAXParserFactory.newInstance(); factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); // We add it to pick up the element of the msd namespace. factory.setValidating(false); invalidCharset.add("EUC_JP"); invalidCharset.add("EUCJP"); invalidCharset.add("EUC_KR"); invalidCharset.add("EUCKR"); }
/** Reads a reply from the socket and makes sure its shape is in order. */ private void receiveAndVerify(DatagramSocket s) throws IOException, SAXException, ParserConfigurationException { DatagramPacket p = new DatagramPacket(new byte[1024], 1024); s.receive(p); String xml = new String(p.getData(), 0, p.getLength(), "UTF-8"); System.out.println(xml); // make sure at least this XML parses SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); spf.newSAXParser().parse(new InputSource(new StringReader(xml)), new DefaultHandler()); }
/** * Return the registered SAX parser factory or null if one does not exist. * @return * * @return returns a SAXParserFactory */ public SAXParserFactory getFactory() { if (saxParserFactory == null) { saxParserFactory = new SAXParserFactoryImpl(); } SAXParserFactory theFactory = saxParserFactory; if (theFactory != null) { theFactory.setNamespaceAware(true); theFactory.setXIncludeAware(false); theFactory.setValidating(false); } return theFactory; }
private SAXParser buildSaxParser() throws JoranException { try { SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setValidating(false); spf.setNamespaceAware(true); return spf.newSAXParser(); } catch (Exception pce) { String errMsg = "Parser configuration error occurred"; addError(errMsg, pce); throw new JoranException(errMsg, pce); } }
private void init() { boolean success = false; // If JAXP is prefered (comes with Sun JVM 1.4.0 and higher) if (!success) { try { javax.xml.parsers.SAXParserFactory spf = javax.xml.parsers.SAXParserFactory.newInstance(); spf.setNamespaceAware(true); javax.xml.parsers.SAXParser saxParser = spf.newSAXParser(); parser = saxParser.getXMLReader(); logger.info("Using JAXP/SAX XML parser."); success = true; } catch (ParserConfigurationException | SAXException e) { logger.warn("Could not instantiate JAXP/SAX XML reader: ", e.getMessage()); logger.debug(e); } } // Aelfred is first alternative. if (!success) { try { parser = (XMLReader) this.getClass() .getClassLoader() .loadClass("gnu.xml.aelfred2.XmlReader") .newInstance(); logger.info("Using Aelfred2 XML parser."); success = true; } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { logger.warn("Could not instantiate Aelfred2 XML reader!"); logger.debug(e); } } // Xerces is second alternative if (!success) { try { parser = (XMLReader) this.getClass() .getClassLoader() .loadClass("org.apache.xerces.parsers.SAXParser") .newInstance(); logger.info("Using Xerces XML parser."); success = true; } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { logger.warn("Could not instantiate Xerces XML reader!"); logger.debug(e); } } if (!success) { logger.error("Could not instantiate any XML parser!"); } }
/** Loads a Configuration from an XMLFile... */ public void loadConfig(String XMLFile) { try { sectionHandling = new HashMap<String, EnumMap<SIT, EnumMap<CIT, Boolean>>>(); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); SAXParser sp = factory.newSAXParser(); DefaultHandler handler = new ConfigLoader(this); sp.parse(XMLFile, handler); } catch (Exception e) { System.err.println(e); loadConfig(); } }
/** Attaches the reader to the catalog. */ private void attachReaderToCatalog(Catalog catalog) { SAXParserFactory spf = new SAXParserFactoryImpl(); spf.setNamespaceAware(true); spf.setValidating(false); SAXCatalogReader saxReader = new SAXCatalogReader(spf); saxReader.setCatalogParser( OASISXMLCatalogReader.namespaceName, "catalog", "com.sun.org.apache.xml.internal.resolver.readers.OASISXMLCatalogReader"); catalog.addReader("application/xml", saxReader); }