use of javax.xml.stream.XMLEventReader in project bazel by bazelbuild.
the class AndroidManifestProcessor method writeManifestPackage.
/**
* Overwrite the package attribute of {@code <manifest>} in an AndroidManifest.xml file.
*
* @param manifest The input manifest.
* @param customPackage The package to write to the manifest.
* @param output The output manifest to generate.
* @return The output manifest if generated or the input manifest if no overwriting is required.
*/
/* TODO(apell): switch from custom xml parsing to Gradle merger with NO_PLACEHOLDER_REPLACEMENT
* set when android common is updated to version 2.5.0.
*/
public Path writeManifestPackage(Path manifest, String customPackage, Path output) {
if (Strings.isNullOrEmpty(customPackage)) {
return manifest;
}
try {
Files.createDirectories(output.getParent());
XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(Files.newInputStream(manifest), UTF_8.name());
XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(Files.newOutputStream(output), UTF_8.name());
XMLEventFactory eventFactory = XMLEventFactory.newInstance();
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (event.isStartElement() && event.asStartElement().getName().toString().equalsIgnoreCase("manifest")) {
StartElement element = event.asStartElement();
@SuppressWarnings("unchecked") Iterator<Attribute> attributes = element.getAttributes();
ImmutableList.Builder<Attribute> newAttributes = ImmutableList.builder();
while (attributes.hasNext()) {
Attribute attr = attributes.next();
if (attr.getName().toString().equalsIgnoreCase("package")) {
newAttributes.add(eventFactory.createAttribute("package", customPackage));
} else {
newAttributes.add(attr);
}
}
writer.add(eventFactory.createStartElement(element.getName(), newAttributes.build().iterator(), element.getNamespaces()));
} else {
writer.add(event);
}
}
writer.flush();
} catch (XMLStreamException | FactoryConfigurationError | IOException e) {
throw new RuntimeException(e);
}
return output;
}
use of javax.xml.stream.XMLEventReader in project hibernate-orm by hibernate.
the class MappingBinder method doBind.
@Override
protected Binding doBind(XMLEventReader staxEventReader, StartElement rootElementStartEvent, Origin origin) {
final String rootElementLocalName = rootElementStartEvent.getName().getLocalPart();
if ("hibernate-mapping".equals(rootElementLocalName)) {
log.debugf("Performing JAXB binding of hbm.xml document : %s", origin.toString());
XMLEventReader hbmReader = new HbmEventReader(staxEventReader, xmlEventFactory);
JaxbHbmHibernateMapping hbmBindings = jaxb(hbmReader, LocalSchema.HBM.getSchema(), hbmJaxbContext(), origin);
return new Binding<JaxbHbmHibernateMapping>(hbmBindings, origin);
} else {
try {
final XMLEventReader reader = new JpaOrmXmlEventReader(staxEventReader, xmlEventFactory);
return new Binding<Document>(toDom4jDocument(reader, origin), origin);
} catch (JpaOrmXmlEventReader.BadVersionException e) {
throw new UnsupportedOrmXsdVersionException(e.getRequestedVersion(), origin);
}
}
}
use of javax.xml.stream.XMLEventReader in project midpoint by Evolveum.
the class MidpointFunctionsImpl method parseXmlToMap.
@Override
public Map<String, String> parseXmlToMap(String xml) {
Map<String, String> resultingMap = new HashMap<String, String>();
if (xml != null && !xml.isEmpty()) {
XMLInputFactory factory = XMLInputFactory.newInstance();
String value = "";
String startName = "";
InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
boolean isRootElement = true;
try {
XMLEventReader eventReader = factory.createXMLEventReader(stream);
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
Integer code = event.getEventType();
if (code == XMLStreamConstants.START_ELEMENT) {
StartElement startElement = event.asStartElement();
startName = startElement.getName().getLocalPart();
if (!isRootElement) {
resultingMap.put(startName, null);
} else {
isRootElement = false;
}
} else if (code == XMLStreamConstants.CHARACTERS) {
Characters characters = event.asCharacters();
if (!characters.isWhiteSpace()) {
StringBuilder valueBuilder;
if (value != null) {
valueBuilder = new StringBuilder(value).append(" ").append(characters.getData().toString());
} else {
valueBuilder = new StringBuilder(characters.getData().toString());
}
value = valueBuilder.toString();
}
} else if (code == XMLStreamConstants.END_ELEMENT) {
EndElement endElement = event.asEndElement();
String endName = endElement.getName().getLocalPart();
if (endName.equals(startName)) {
if (value != null) {
resultingMap.put(endName, value);
value = null;
}
} else {
LOGGER.trace("No value between xml tags, tag name : {0}", endName);
}
} else if (code == XMLStreamConstants.END_DOCUMENT) {
isRootElement = true;
}
}
} catch (XMLStreamException e) {
StringBuilder error = new StringBuilder("Xml stream exception wile parsing xml string").append(e.getLocalizedMessage());
throw new SystemException(error.toString());
}
} else {
LOGGER.trace("Input xml string null or empty.");
}
return resultingMap;
}
use of javax.xml.stream.XMLEventReader in project opennms by OpenNMS.
the class HypericAckProcessor method parseHypericAlerts.
/**
* <p>parseHypericAlerts</p>
*
* @param reader a {@link java.io.Reader} object.
* @return a {@link java.util.List} object.
* @throws javax.xml.bind.JAXBException if any.
* @throws javax.xml.stream.XMLStreamException if any.
*/
public static List<HypericAlertStatus> parseHypericAlerts(Reader reader) throws JAXBException, XMLStreamException {
List<HypericAlertStatus> retval = new ArrayList<HypericAlertStatus>();
// Instantiate a JAXB context to parse the alert status
JAXBContext context = JAXBContext.newInstance(new Class[] { HypericAlertStatuses.class, HypericAlertStatus.class });
XMLInputFactory xmlif = XMLInputFactory.newInstance();
XMLEventReader xmler = xmlif.createXMLEventReader(reader);
EventFilter filter = new EventFilter() {
@Override
public boolean accept(XMLEvent event) {
return event.isStartElement();
}
};
XMLEventReader xmlfer = xmlif.createFilteredReader(xmler, filter);
// Read up until the beginning of the root element
StartElement startElement = (StartElement) xmlfer.nextEvent();
// Fetch the root element name for {@link HypericAlertStatus} objects
String rootElementName = context.createJAXBIntrospector().getElementName(new HypericAlertStatuses()).getLocalPart();
if (rootElementName.equals(startElement.getName().getLocalPart())) {
Unmarshaller unmarshaller = context.createUnmarshaller();
// Use StAX to pull parse the incoming alert statuses
while (xmlfer.peek() != null) {
Object object = unmarshaller.unmarshal(xmler);
if (object instanceof HypericAlertStatus) {
HypericAlertStatus alertStatus = (HypericAlertStatus) object;
retval.add(alertStatus);
}
}
} else {
// Try to pull in the HTTP response to give the user a better idea of what went wrong
StringBuffer errorContent = new StringBuffer();
LineNumberReader lineReader = new LineNumberReader(reader);
try {
String line;
while (true) {
line = lineReader.readLine();
if (line == null) {
break;
} else {
errorContent.append(line.trim());
}
}
} catch (IOException e) {
errorContent.append("Exception while trying to print out message content: " + e.getMessage());
}
// Throw an exception and include the erroneous HTTP response in the exception text
throw new JAXBException("Found wrong root element in Hyperic XML document, expected: \"" + rootElementName + "\", found \"" + startElement.getName().getLocalPart() + "\"\n" + errorContent.toString());
}
return retval;
}
use of javax.xml.stream.XMLEventReader in project voltdb by VoltDB.
the class JDBCSQLXML method createStAXSource.
/**
* Retrieves a new StAXSource for reading the XML value designated by this
* SQLXML instance. <p>
*
* @param sourceClass The class of the source
* @throws java.sql.SQLException if there is an error processing the XML
* value or if the given <tt>sourceClass</tt> is not supported.
* @return a new StAXSource for reading the XML value designated by this
* SQLXML instance
*/
@SuppressWarnings("unchecked")
protected <T extends Source> T createStAXSource(Class<T> sourceClass) throws SQLException {
StAXSource source = null;
Constructor sourceCtor = null;
Reader reader = null;
XMLInputFactory factory = null;
XMLEventReader eventReader = null;
try {
factory = XMLInputFactory.newInstance();
} catch (FactoryConfigurationError ex) {
throw Exceptions.sourceInstantiation(ex);
}
try {
sourceCtor = (sourceClass == null) ? StAXSource.class.getConstructor(XMLEventReader.class) : sourceClass.getConstructor(XMLEventReader.class);
} catch (SecurityException ex) {
throw Exceptions.sourceInstantiation(ex);
} catch (NoSuchMethodException ex) {
throw Exceptions.sourceInstantiation(ex);
}
reader = getCharacterStreamImpl();
try {
eventReader = factory.createXMLEventReader(reader);
} catch (XMLStreamException ex) {
throw Exceptions.sourceInstantiation(ex);
}
try {
source = (StAXSource) sourceCtor.newInstance(eventReader);
} catch (SecurityException ex) {
throw Exceptions.sourceInstantiation(ex);
} catch (IllegalArgumentException ex) {
throw Exceptions.sourceInstantiation(ex);
} catch (IllegalAccessException ex) {
throw Exceptions.sourceInstantiation(ex);
} catch (InstantiationException ex) {
throw Exceptions.sourceInstantiation(ex);
} catch (InvocationTargetException ex) {
throw Exceptions.sourceInstantiation(ex.getTargetException());
} catch (ClassCastException ex) {
throw Exceptions.sourceInstantiation(ex);
}
return (T) source;
}
Aggregations