/** * Creates an XMLStreamReader. * * @param setUseCatalog a flag indicates whether USE_CATALOG shall be set * through the factory * @param useCatalog the value of USE_CATALOG * @param catalog the path to a catalog * @param xml the xml to be parsed * @param resolver a resolver to be set on the reader * @return an instance of the XMLStreamReader * @throws FileNotFoundException * @throws XMLStreamException */ XMLStreamReader getStreamReader(boolean setUseCatalog, boolean useCatalog, String catalog, String xml, XMLResolver resolver) throws FileNotFoundException, XMLStreamException { XMLInputFactory factory = XMLInputFactory.newInstance(); if (catalog != null) { factory.setProperty(CatalogFeatures.Feature.FILES.getPropertyName(), catalog); } factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true); factory.setProperty(XMLInputFactory.IS_COALESCING, true); if (resolver != null) { factory.setProperty(XMLInputFactory.RESOLVER, resolver); } if (setUseCatalog) { factory.setProperty(XMLConstants.USE_CATALOG, useCatalog); } InputStream entityxml = new FileInputStream(xml); XMLStreamReader streamReader = factory.createXMLStreamReader(xml, entityxml); return streamReader; }
public static XMLInputFactory createXMLInputFactory(boolean nsAware) { XMLInputFactory factory = XMLInputFactory.newInstance(); setProperty(factory, XMLInputFactory.IS_NAMESPACE_AWARE, nsAware); setProperty(factory, XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); setProperty(factory, XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE); setProperty(factory, XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); factory.setXMLResolver(new XMLResolver() { public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException { throw new XMLStreamException("Reading external entities is disabled"); } }); if (isWoodstox(factory)) { // just log a debug as we are good then LOG.debug("Created Woodstox XMLInputFactory: {}", factory); } else { // log a hint that woodstock may be a better factory to use LOG.info("Created XMLInputFactory: {}. DOMSource/DOMResult may have issues with {}. We suggest using Woodstox.", factory, factory); } return factory; }
/** * Return a new factory so that the caller can set sticky parameters. * @param nsAware */ public static XMLInputFactory createXMLInputFactory(boolean nsAware) { XMLInputFactory factory = XMLInputFactory.newInstance(); setProperty(factory, XMLInputFactory.IS_NAMESPACE_AWARE, nsAware); setProperty(factory, XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); setProperty(factory, XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE); setProperty(factory, XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); factory.setXMLResolver(new XMLResolver() { public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException { throw new XMLStreamException("Reading external entities is disabled"); } }); return factory; }
private static XMLInputFactory createXMLInputFactory() throws FactoryConfigurationError { XMLInputFactory factory = XMLInputFactory.newInstance(); if (!SUPPORT_DTD) { factory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); //these next ones are somewhat redundant, we set them just in case the DTD support property is not respected factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); factory.setXMLResolver(new XMLResolver() { @Override public Object resolveEntity(String arg0, String arg1, String arg2, String arg3) throws XMLStreamException { throw new XMLStreamException("Reading external entities is disabled"); //$NON-NLS-1$ } }); } return factory; }
AbstractXMLIterator(final InputStream in,final String jaxbPath,final Boolean namespaceaware) throws JAXBException,XMLStreamException { this.in = in; this.jc = JAXBContext.newInstance(jaxbPath); this.unmarshaller =jc.createUnmarshaller(); final XMLInputFactory xmlInputFactory=XMLInputFactory.newFactory(); xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE,namespaceaware); xmlInputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE); xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); xmlInputFactory.setXMLResolver(new XMLResolver() { @Override public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException { LOG.warn("ignoring resolveEntity "+systemID+" "+baseURI+" "+namespace); return new ByteArrayInputStream(new byte[0]); } }); final StreamSource streamSource = new StreamSource(in); r=xmlInputFactory.createXMLEventReader(streamSource); }
public void testStAX(boolean setUseCatalog, boolean useCatalog, String catalog, String xml, XMLResolver resolver, String expected) throws Exception { XMLStreamReader streamReader = getStreamReader( setUseCatalog, useCatalog, catalog, xml, resolver); String text = getText(streamReader, XMLStreamConstants.CHARACTERS); Assert.assertEquals(text.trim(), expected); }
public void testStAXNegative(boolean setUseCatalog, boolean useCatalog, String catalog, String xml, XMLResolver resolver, String expected) throws Exception { XMLStreamReader streamReader = getStreamReader( setUseCatalog, useCatalog, catalog, xml, resolver); String text = getText(streamReader, XMLStreamConstants.ENTITY_REFERENCE); Assert.assertEquals(text.trim(), expected); }
public Canonicalizer20010315Test() throws Exception { this.xmlInputFactory = XMLInputFactory.newInstance(); this.xmlInputFactory.setEventAllocator(new XMLSecEventAllocator()); XMLResolver xmlResolver = new XMLResolver() { @Override public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException { return this.getClass().getClassLoader().getResourceAsStream( "org/apache/xml/security/c14n/in/" + systemID); } }; this.xmlInputFactory.setXMLResolver(xmlResolver); }
public Canonicalizer11Test() throws Exception { this.xmlInputFactory = XMLInputFactory.newInstance(); this.xmlInputFactory.setEventAllocator(new XMLSecEventAllocator()); XMLResolver xmlResolver = new XMLResolver() { @Override public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException { return this.getClass().getClassLoader().getResourceAsStream( "org/apache/xml/security/c14n/in/" + systemID); } }; this.xmlInputFactory.setXMLResolver(xmlResolver); }
public void setProperty(String name, Object value) throws IllegalArgumentException { if (name.equals(IS_NAMESPACE_AWARE)) namespaceAware = ((Boolean) value).booleanValue(); else if (name.equals(IS_VALIDATING)) validating = ((Boolean) value).booleanValue(); else if (name.equals(IS_COALESCING)) coalescing = ((Boolean) value).booleanValue(); else if (name.equals(IS_REPLACING_ENTITY_REFERENCES)) replacingEntityReferences = ((Boolean) value).booleanValue(); else if (name.equals(IS_SUPPORTING_EXTERNAL_ENTITIES)) externalEntities = ((Boolean) value).booleanValue(); else if (name.equals(SUPPORT_DTD)) supportDTD = ((Boolean) value).booleanValue(); else if (name.equals(REPORTER)) reporter = (XMLReporter) value; else if (name.equals(RESOLVER)) resolver = (XMLResolver) value; else if (name.equals(ALLOCATOR)) allocator = (XMLEventAllocator) value; else if (name.equals("gnu.xml.stream.stringInterning")) stringInterning = ((Boolean) value).booleanValue(); else if (name.equals("gnu.xml.stream.baseAware")) baseAware = ((Boolean) value).booleanValue(); else if (name.equals("gnu.xml.stream.xIncludeAware")) xIncludeAware = ((Boolean) value).booleanValue(); else throw new IllegalArgumentException(name); }
@Override public XMLInputFactory2 newXMLInputFactory2() { XMLInputFactory factory = XMLInputFactory.newInstance(); factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.FALSE); factory.setXMLResolver(new XMLResolver() { @Override public Object resolveEntity(String arg0, String arg1, String arg2, String arg3) throws XMLStreamException { throw new XMLStreamException("Reading external entities is disabled"); } }); return new StaxXMLInputFactory2(factory); }
public XMLResolver asXMLResolver() { return new XMLResolver() { @Override public Object resolveEntity(String publicId, String systemId, String baseURI, String namespace) throws XMLStreamException { try { final InputSource src = SystemIdResolver.this.resolveEntity(null, publicId, baseURI, systemId); return (src == null) ? null : src.getByteStream(); } catch (IOException ioe) { throw new XMLStreamException("Cannot resolve entity", ioe); } } }; }
/** * Constructor used when creating a complete new (main-level) reader that * does not share its input buffers or state with another reader. */ protected StreamScanner(WstxInputSource input, ReaderConfig cfg, XMLResolver res) { super(); mInput = input; // 17-Jun-2004, TSa: Need to know root-level input source mRootInput = input; mConfig = cfg; mSymbols = cfg.getSymbols(); int cf = cfg.getConfigFlags(); mCfgNsEnabled = (cf & CFG_NAMESPACE_AWARE) != 0; mCfgReplaceEntities = (cf & CFG_REPLACE_ENTITY_REFS) != 0; mNormalizeLFs = mConfig.willNormalizeLFs(); mInputBuffer = null; mInputPtr = mInputEnd = 0; mEntityResolver = res; mCfgTreatCharRefsAsEntities = mConfig.willTreatCharRefsAsEnts(); if (mCfgTreatCharRefsAsEntities) { mCachedEntities = new HashMap<String,IntEntity>(); } else { mCachedEntities = Collections.emptyMap(); } }
@Override public WstxInputSource expand(WstxInputSource parent, XMLResolver res, ReaderConfig cfg, int xmlVersion) { /* 26-Dec-2006, TSa: Better leave source as null, since internal * entity declaration context should never be used: when expanding, * reference context is to be used. */ return InputSourceFactory.constructCharArraySource //(parent, mName, mRepl, 0, mRepl.length, mContentLocation, getSource()); (parent, mName, mRepl, 0, mRepl.length, mContentLocation, null); }
@Override public WstxInputSource expand(WstxInputSource parent, XMLResolver res, ReaderConfig cfg, int xmlVersion) throws IOException, XMLStreamException { /* 05-Feb-2006, TSa: If xmlVersion not explicitly known, it defaults * to 1.0 */ if (xmlVersion == XmlConsts.XML_V_UNKNOWN) { xmlVersion = XmlConsts.XML_V_10; } return DefaultInputResolver.resolveEntity (parent, mContext, mName, getPublicId(), getSystemId(), res, cfg, xmlVersion); }
@Override public WstxInputSource expand(WstxInputSource parent, XMLResolver res, ReaderConfig cfg, int xmlVersion) { // Should never get called, actually... throw new IllegalStateException("Internal error: createInputSource() called for unparsed (external) entity."); }
/** * Basic external resource resolver implementation; usable both with * DTD and entity resolution. * * @param parent Input source that contains reference to be expanded. * @param pathCtxt Reference context to use for resolving path, if * known. If null, reference context of the parent will * be used; and if that is null (which is possible), the * current working directory will be assumed. * @param entityName Name/id of the entity being expanded, if this is an * entity expansion; null otherwise (for example, when resolving external * subset). * @param publicId Public identifier of the resource, if known; null/empty * otherwise. Default implementation just ignores the identifier. * @param systemId System identifier of the resource. Although interface * allows null/empty, default implementation considers this an error. * @param xmlVersion Xml version as declared by the main parsed * document. Currently only relevant for checking that XML 1.0 document * does not include XML 1.1 external parsed entities. * If XML_V_UNKNOWN, no checks will be done. * @param customResolver Custom resolver to use first for resolution, * if any (may be null). * @param cfg Reader configuration object used by the parser that is * resolving the entity * * @return Input source, if entity could be resolved; null if it could * not be resolved. In latter case processor may use its own default * resolution mechanism. */ public static WstxInputSource resolveEntity (WstxInputSource parent, URL pathCtxt, String entityName, String publicId, String systemId, XMLResolver customResolver, ReaderConfig cfg, int xmlVersion) throws IOException, XMLStreamException { if (pathCtxt == null) { pathCtxt = parent.getSource(); if (pathCtxt == null) { pathCtxt = URLUtil.urlFromCurrentDir(); } } // Do we have a custom resolver that may be able to resolve it? if (customResolver != null) { Object source = customResolver.resolveEntity(publicId, systemId, pathCtxt.toExternalForm(), entityName); if (source != null) { return sourceFrom(parent, cfg, entityName, xmlVersion, source); } } // Have to have a system id, then... if (systemId == null) { throw new XMLStreamException("Can not resolve " +((entityName == null) ? "[External DTD subset]" : ("entity '"+entityName+"'"))+" without a system id (public id '" +publicId+"')"); } URL url = URLUtil.urlFromSystemId(systemId, pathCtxt); return sourceFromURL(parent, cfg, entityName, xmlVersion, url, publicId); }
/** * A very simple utility expansion method used generally when the * only way to resolve an entity is via passed resolver; and where * failing to resolve it is not fatal. */ public static WstxInputSource resolveEntityUsing (WstxInputSource refCtxt, String entityName, String publicId, String systemId, XMLResolver resolver, ReaderConfig cfg, int xmlVersion) throws IOException, XMLStreamException { URL ctxt = (refCtxt == null) ? null : refCtxt.getSource(); if (ctxt == null) { ctxt = URLUtil.urlFromCurrentDir(); } Object source = resolver.resolveEntity(publicId, systemId, ctxt.toExternalForm(), entityName); return (source == null) ? null : sourceFrom(refCtxt, cfg, entityName, xmlVersion, source); }
@Test public void testCreateBasicInputFactory() throws Exception { XMLInputFactory factory = XmlUtils.createBasicInputFactory(); factory.setXMLResolver(new XMLResolver() { @Override public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException { Assert.assertFalse("DTD should not be resolved.", "http://bogus.url/foo.dtd".equals(systemID)); return null; } }); XMLStreamReader reader = factory.createXMLStreamReader(new StringReader("<!DOCTYPE foo SYSTEM \"http://bogus.url/foo.dtd\"><xml/>")); while(reader.hasNext()) { reader.next(); } }
private XMLInputFactory createXMLInputFactory() { final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory(); xmlInputFactory.setXMLResolver(new XMLResolver() { @Override public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) throws XMLStreamException { LOG.debug("Ignoring resolve Entity"); return new ByteArrayInputStream(new byte[0]); } }); return xmlInputFactory; }
/** Creates a new instance of StaxEntityResolverWrapper */ public StaxEntityResolverWrapper(XMLResolver resolver) { fStaxResolver = resolver ; }
public void setStaxEntityResolver(XMLResolver resolver ){ fStaxResolver = resolver ; }
public XMLResolver getStaxEntityResolver(){ return fStaxResolver ; }
@Override public XMLResolver getXMLResolver() { throw new UnsupportedOperationException("Not supported yet."); }
@Override public void setXMLResolver(XMLResolver resolver) { throw new UnsupportedOperationException("Not supported yet."); }
@Test(dataProvider = "data_StAXA") public void testStAXA(boolean setUseCatalog, boolean useCatalog, String catalog, String xml, XMLResolver resolver, String expected) throws Exception { testStAX(setUseCatalog, useCatalog, catalog, xml, resolver, expected); }
@Test(dataProvider = "data_StAXC") public void testStAXC(boolean setUseCatalog, boolean useCatalog, String catalog, String xml, XMLResolver resolver, String expected) throws Exception { testStAXNegative(setUseCatalog, useCatalog, catalog, xml, resolver, expected); }
@Test(dataProvider = "data_StAXC", expectedExceptions = XMLStreamException.class) public void testStAXC(boolean setUseCatalog, boolean useCatalog, String catalog, String xml, XMLResolver resolver, String expected) throws Exception { testStAX(setUseCatalog, useCatalog, catalog, xml, resolver, expected); }
@Test(dataProvider = "data_StAXC") public void testStAXC(boolean setUseCatalog, boolean useCatalog, String catalog, String xml, XMLResolver resolver, String expected) throws Exception { testStAX(setUseCatalog, useCatalog, catalog, xml, resolver, expected); }