public StaxDocumentLoader( String packageName, Path resourceBase, NodeHandler topLevelNodeHandler) { super(packageName, resourceBase); this.topLevelNodeHandler = topLevelNodeHandler; factory = XMLInputFactory.newFactory(); }
private static XMLInputFactory createXmlInputFactory() { final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory(); xmlInputFactory.setProperty(XMLInputFactory.IS_VALIDATING, FALSE); xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, FALSE); xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, FALSE); return xmlInputFactory; }
public static XMLStreamReader createSafeReader(StreamSource source) throws XMLStreamException { if (source == null) { throw new IllegalArgumentException("The provided source cannot be null"); } XMLInputFactory xif = XMLInputFactory.newFactory(); xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); return xif.createXMLStreamReader(source); }
package xxe; import javax.xml.bind.*; import javax.xml.stream.*; import javax.xml.transform.stream.StreamSource; public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Customer.class); XMLInputFactory xif = XMLInputFactory.newFactory(); xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource("src/xxe/input.xml")); Unmarshaller unmarshaller = jc.createUnmarshaller(); Customer customer = (Customer) unmarshaller.unmarshal(xsr); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(customer, System.out); } }
/** * Convert the REST server's response to an XML reader. * * @param response The REST server's response. * @return A reader over the parsed XML document. * @throws IOException If the document fails to parse */ private XMLStreamReader getInputStream(Response response) throws IOException { try { // Prevent the parser from reading XMl with external entities defined XMLInputFactory xif = XMLInputFactory.newFactory(); xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); return xif.createXMLStreamReader(new ByteArrayInputStream(response.getBody())); } catch (XMLStreamException e) { throw new IOException("Failed to parse XML", e); } } }
package forum11465653; import java.io.FileReader; import javax.xml.bind.*; import javax.xml.stream.*; public class Demo { public static void main(String[] args) throws Exception{ XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("src/forum11465653/input.xml")); xsr.nextTag(); // Advance to Envelope tag xsr.nextTag(); // Advance to Body tag xsr.nextTag(); // Advance to getNumberResponse tag System.out.println(xsr.getNamespaceContext().getNamespaceURI("ns")); JAXBContext jc = JAXBContext.newInstance(Response.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); JAXBElement<Response> je = unmarshaller.unmarshal(xsr, Response.class); System.out.println(je.getName()); System.out.println(je.getValue()); } }
/** * Get a client configuration instance loaded from a file called {@code wildfly-config.xml}, either from the * root of the specified class loader or within the {@code META-INF} folder. * If no configurations are found or are loadable, {@code null} is returned. * * @param classLoader the class loader from which to load the configuration resource * @return the client configuration instance, or {@code null} if no configuration is found */ public static ClientConfiguration getInstance(ClassLoader classLoader) { URL resource = classLoader.getResource("wildfly-config.xml"); if (resource == null) { resource = classLoader.getResource("META-INF/wildfly-config.xml"); if (resource == null) { return null; } } try { return new ClientConfiguration(XMLInputFactory.newFactory(), resource.toURI(), resource::openStream); } catch (URISyntaxException e) { return null; } }
private LoadedIdentity loadIdentityPrivileged(final boolean skipCredentials, final boolean skipAttributes) throws RealmUnavailableException { try (InputStream inputStream = Files.newInputStream(path, READ)) { final XMLInputFactory inputFactory = XMLInputFactory.newFactory(); inputFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE); inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE); inputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); try (final AutoCloseableXMLStreamReaderHolder holder = new AutoCloseableXMLStreamReaderHolder(inputFactory.createXMLStreamReader(inputStream, "UTF-8"))) { final XMLStreamReader streamReader = holder.getXmlStreamReader(); return parseIdentity(streamReader, skipCredentials, skipAttributes); } catch (XMLStreamException e) { throw ElytronMessages.log.fileSystemRealmFailedToRead(path, name, e); } } catch (NoSuchFileException | FileNotFoundException ignored) { return null; } catch (IOException e) { throw ElytronMessages.log.fileSystemRealmFailedToOpen(path, name, e); } }
private void parse(String xml) throws XMLStreamException { XMLInputFactory factory = XMLInputFactory.newFactory(); XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(new StringReader(xml)); Path path = Paths.get("/tmp/fake.txt"); Qualifiers qualifiers = Qualifiers.fromParentDir(path.getParent()); staxDocumentLoader.doParse(xmlStreamReader, new XmlContext("pkg", path, qualifiers)); } }
@Override @SuppressWarnings("unchecked") public <T> T fromString(String content, Class<T> classOfT) { try (StringReader reader = new StringReader(content)) { JAXBContext jaxbContext = JAXBContext.newInstance(classOfT); XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory(); xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, true); XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(reader); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); return (T) unmarshaller.unmarshal(xmlStreamReader); } catch (JAXBException | XMLStreamException e) { throw new PippoRuntimeException(e, "Failed to deserialize content to '{}'", classOfT.getName()); } }
/** * Creates an XML reader from an input object. * * @param input THe input object, see {@link #reader(Object)} for details. */ public static XMLStreamReader xmlReader(Object input) throws IOException { YsldInput in = reader(input); try { XMLInputFactory xmlFactory = XMLInputFactory.newFactory(); try { return xmlFactory.createXMLStreamReader(in.reader); } catch (XMLStreamException e) { throw new IOException(e); } } finally { in.close(); } }
XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(new File("file.xml")));
SldTransformer transformer(String dirname, String filename) throws Exception { StringWriter yaml = new StringWriter(); XMLInputFactory factory = XMLInputFactory.newFactory(); XMLStreamReader xml = factory.createXMLStreamReader(YsldTests.sld(dirname, filename)); return new SldTransformer(xml, yaml); }
/** * Returns the StAX input factory specified in this parsing context. * If a factory is not explicitly specified, then a default factory * instance is created and returned. The default factory instance is * configured to be namespace-aware and to apply reasonable security * using the {@link #IGNORING_STAX_ENTITY_RESOLVER}. * * @return StAX input factory * @since Apache Tika 1.13 */ public static XMLInputFactory getXMLInputFactory() { XMLInputFactory factory = XMLInputFactory.newFactory(); tryToSetStaxProperty(factory, XMLInputFactory.IS_NAMESPACE_AWARE, true); tryToSetStaxProperty(factory, XMLInputFactory.IS_VALIDATING, false); factory.setXMLResolver(IGNORING_STAX_ENTITY_RESOLVER); trySetStaxSecurityManager(factory); return factory; }
/** * Creates a new StAX XMLInputFactory, with sensible defaults */ public static XMLInputFactory newXMLInputFactory(XmlOptionsBean options) { XMLInputFactory factory = XMLInputFactory.newFactory(); trySetProperty(factory, XMLInputFactory.IS_NAMESPACE_AWARE, true); trySetProperty(factory, XMLInputFactory.IS_VALIDATING, false); trySetProperty(factory, XMLInputFactory.SUPPORT_DTD, options.isLoadDTDGrammar()); trySetProperty(factory, XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, options.isLoadExternalDTD()); return factory; }
@Override public void setReader(Reader input) throws CDKException { try { XMLInputFactory xmlfact = XMLInputFactory.newFactory(); xmlfact.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true); parser = xmlfact.createXMLStreamReader(input); this.input = input; } catch (Exception exception) { throw new CDKException("Error while creating reader: " + exception.getMessage(), exception); } }
/** * Returns an instance of {@link javax.xml.stream.XMLInputFactory} * according to the classpath. */ public static XMLInputFactory newInputFactory() { return inputFactoryProvider != null ? inputFactoryProvider.createInputFactory() : XMLInputFactory.newFactory(); }
public ChangesetScanner(InputStream changesetStream) throws XMLStreamException, FactoryConfigurationError { this.reader = XMLInputFactory.newFactory().createXMLStreamReader(changesetStream, "UTF-8"); reader.nextTag(); reader.require(START_ELEMENT, null, "osm"); reader.nextTag(); }
public void run(ConfigParser parser) { try { InputStream is = new ByteArrayInputStream(xmlContent.getBytes()); XMLStreamReader reader = XMLInputFactory.newFactory().createXMLStreamReader(is, "utf-8"); parser.parse(reader, doc, Dom.unwrap((ConfigBeanProxy) parent)); } catch (XMLStreamException e) { LOG.log(Level.SEVERE, ConfigApiLoggerInfo.DEFAULT_CFG_READ_FAILED, e); } } }
@Test public void testDom2StAXEventReader() throws Exception { final XMLInputFactory newFactory = XMLInputFactory.newFactory(); final DOMSource source = new DOMSource(this.document); newFactory.createXMLEventReader(source); }