use of javax.xml.stream.XMLInputFactory in project webservices-axiom by apache.
the class StAXUtils method getXMLInputFactory.
/**
* Get a cached {@link XMLInputFactory} instance using the specified configuration.
*
* @param configuration
* the configuration applied to the requested factory
* @return an {@link XMLInputFactory} instance.
*/
public static XMLInputFactory getXMLInputFactory(StAXParserConfiguration configuration) {
if (configuration == null) {
configuration = StAXParserConfiguration.DEFAULT;
}
XMLInputFactory f = inputFactoryMap.get(configuration);
if (f == null) {
f = newXMLInputFactory(StAXUtils.class.getClassLoader(), configuration);
inputFactoryMap.put(configuration, f);
if (log.isDebugEnabled()) {
if (f != null) {
log.debug("Created singleton XMLInputFactory " + f.getClass() + " with configuration " + configuration);
}
}
}
return f;
}
use of javax.xml.stream.XMLInputFactory in project ddf by codice.
the class TransactionRequestConverter method getElementFromXml.
private <T> T getElementFromXml(String xml, Class<T> clazz) {
JAXBElement<T> root;
try {
JAXBContext jaxbContext = getJaxBContext();
XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(xml));
root = jaxbContext.createUnmarshaller().unmarshal(xmlStreamReader, clazz);
} catch (JAXBException | XMLStreamException e) {
throw new ConversionException(e);
}
return root.getValue();
}
use of javax.xml.stream.XMLInputFactory in project bnd by bndtools.
the class ObrContentProvider method checkStream.
public CheckResult checkStream(String name, InputStream stream) throws IOException {
XMLStreamReader reader = null;
try {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
inputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
reader = inputFactory.createXMLStreamReader(stream);
ParserState state = ParserState.beforeRoot;
while (reader.hasNext()) {
int type = reader.next();
String localName;
switch(type) {
case PROCESSING_INSTRUCTION:
if (PI_TARGET_STYLESHEET.equals(reader.getPITarget()) && PI_DATA_STYLESHEET.equals(reader.getPIData()))
return new CheckResult(accept, "Recognised stylesheet", null);
break;
case START_ELEMENT:
localName = reader.getLocalName();
switch(state) {
case beforeRoot:
String nsUri = reader.getNamespaceURI();
if (nsUri != null)
return CheckResult.fromBool(NS_URI.equals(nsUri), "Correct namespace on root element", "Incorrect namespace on root element: " + nsUri, null);
if (!TAG_REPOSITORY.equals(localName))
return new CheckResult(reject, "Incorrect root element name", null);
state = ParserState.inRoot;
break;
case inRoot:
if (TAG_RESOURCE.equals(localName)) {
state = ParserState.inResource;
} else if (!TAG_REFERRAL.equals(localName)) {
return new CheckResult(reject, String.format("Incorrect element '%s', expected '%s' or '%s'.", localName, TAG_RESOURCE, TAG_REFERRAL), null);
}
break;
case inResource:
if (TAG_CAPABILITY.equals(localName)) {
state = ParserState.inCapability;
}
break;
case inCapability:
return CheckResult.fromBool(TAG_PROPERTY.equals(localName), "Found 'p' tag inside 'capability'", String.format("Incorrect element '%s' inside '%s'; expected '%s'.", localName, TAG_CAPABILITY, TAG_PROPERTY), null);
}
break;
case END_ELEMENT:
localName = reader.getLocalName();
if (state == ParserState.inResource && TAG_RESOURCE.equals(localName))
state = ParserState.inRoot;
if (state == ParserState.inCapability && TAG_CAPABILITY.equals(localName))
state = ParserState.inResource;
break;
}
}
return new CheckResult(undecided, "Reached end of stream", null);
} catch (XMLStreamException e) {
return new CheckResult(reject, "Invalid XML", e);
} finally {
if (reader != null)
try {
reader.close();
} catch (XMLStreamException e) {
}
}
}
use of javax.xml.stream.XMLInputFactory in project bnd by bndtools.
the class R5RepoContentProvider method checkStream.
public CheckResult checkStream(String name, InputStream stream) throws IOException {
XMLStreamReader reader = null;
try {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
inputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
reader = inputFactory.createXMLStreamReader(stream);
ParserState state = ParserState.beforeRoot;
while (reader.hasNext()) {
int type = reader.next();
String localName;
switch(type) {
case START_ELEMENT:
localName = reader.getLocalName();
switch(state) {
case beforeRoot:
String nsUri = reader.getNamespaceURI();
if (nsUri != null)
return CheckResult.fromBool(NS_URI.equals(nsUri), "Corrent namespace", "Incorrect namespace: " + nsUri, null);
if (!TAG_REPOSITORY.equals(localName))
return new CheckResult(reject, "Incorrect root element name", null);
state = ParserState.inRoot;
break;
case inRoot:
if (TAG_RESOURCE.equals(localName)) {
state = ParserState.inResource;
}
break;
case inResource:
if (TAG_REQUIREMENT.equals(localName))
return new CheckResult(accept, "Recognised element 'requirement' in 'resource'", null);
if (TAG_CAPABILITY.equals(localName))
state = ParserState.inCapability;
break;
case inCapability:
if (TAG_ATTRIBUTE.equals(localName) || TAG_DIRECTIVE.equals(localName)) {
return new CheckResult(accept, "Recognised element '%s' in 'capability'", null);
}
break;
}
break;
case END_ELEMENT:
localName = reader.getLocalName();
if (state == ParserState.inResource && TAG_RESOURCE.equals(localName))
state = ParserState.inRoot;
if (state == ParserState.inCapability && TAG_CAPABILITY.equals(localName))
state = ParserState.inResource;
break;
default:
break;
}
}
return new CheckResult(undecided, "Reached end of stream", null);
} catch (XMLStreamException e) {
return new CheckResult(reject, "Invalid XML", e);
} finally {
if (reader != null)
try {
reader.close();
} catch (XMLStreamException e) {
}
}
}
use of javax.xml.stream.XMLInputFactory in project bnd by bndtools.
the class R5RepoContentProvider method parseIndex.
public void parseIndex(InputStream stream, URI baseUri, IRepositoryIndexProcessor listener, LogService log) throws Exception {
XMLStreamReader reader = null;
try {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true);
inputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
reader = inputFactory.createXMLStreamReader(stream);
ResourceBuilder resourceBuilder = null;
CapReqBuilder capReqBuilder = null;
while (reader.hasNext()) {
int type = reader.next();
String localName;
switch(type) {
case START_ELEMENT:
localName = reader.getLocalName();
if (TAG_REFERRAL.equals(localName)) {
String url = reader.getAttributeValue(null, ATTR_REFERRAL_URL);
String depth = reader.getAttributeValue(null, ATTR_REFERRAL_DEPTH);
Referral referral = new Referral(url, parseInt(depth));
listener.processReferral(baseUri, referral, 0, 0);
} else if (TAG_RESOURCE.equals(localName)) {
resourceBuilder = new ResourceBuilder();
} else if (TAG_CAPABILITY.equals(localName) || TAG_REQUIREMENT.equals(localName)) {
String namespace = reader.getAttributeValue(null, ATTR_NAMESPACE);
capReqBuilder = new CapReqBuilder(namespace);
} else if (TAG_ATTRIBUTE.equals(localName)) {
String name = reader.getAttributeValue(null, ATTR_NAME);
String valueStr = reader.getAttributeValue(null, ATTR_VALUE);
String typeAttr = reader.getAttributeValue(null, ATTR_TYPE);
if (capReqBuilder != null) {
// base URI.
if (ContentNamespace.CONTENT_NAMESPACE.equals(capReqBuilder.getNamespace()) && ContentNamespace.CAPABILITY_URL_ATTRIBUTE.equals(name)) {
URI resolvedUri = resolveUri(valueStr, baseUri);
capReqBuilder.addAttribute(name, resolvedUri);
} else {
Object convertedAttr = convertAttribute(valueStr, typeAttr);
capReqBuilder.addAttribute(name, convertedAttr);
}
}
} else if (TAG_DIRECTIVE.equals(localName)) {
String name = reader.getAttributeValue(null, ATTR_NAME);
String valueStr = reader.getAttributeValue(null, ATTR_VALUE);
if (capReqBuilder != null)
capReqBuilder.addDirective(name, valueStr);
}
break;
case END_ELEMENT:
localName = reader.getLocalName();
if (TAG_CAPABILITY.equals(localName)) {
if (resourceBuilder != null && capReqBuilder != null)
resourceBuilder.addCapability(capReqBuilder);
capReqBuilder = null;
} else if (TAG_REQUIREMENT.equals(localName)) {
if (resourceBuilder != null && capReqBuilder != null)
resourceBuilder.addRequirement(capReqBuilder);
capReqBuilder = null;
} else if (TAG_RESOURCE.equals(localName)) {
if (resourceBuilder != null) {
Resource resource = resourceBuilder.build();
listener.processResource(resource);
resourceBuilder = null;
}
}
break;
}
}
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
}
}
}
}
Aggregations