use of javax.xml.stream.EventFilter 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.EventFilter in project poi by apache.
the class DrawSimpleShape method getCustomGeometry.
protected static CustomGeometry getCustomGeometry(String name, Graphics2D graphics) {
@SuppressWarnings("unchecked") Map<String, CustomGeometry> presets = (graphics == null) ? null : (Map<String, CustomGeometry>) graphics.getRenderingHint(Drawable.PRESET_GEOMETRY_CACHE);
if (presets == null) {
presets = new HashMap<String, CustomGeometry>();
if (graphics != null) {
graphics.setRenderingHint(Drawable.PRESET_GEOMETRY_CACHE, presets);
}
String packageName = "org.apache.poi.sl.draw.binding";
InputStream presetIS = Drawable.class.getResourceAsStream("presetShapeDefinitions.xml");
// StAX:
EventFilter startElementFilter = new EventFilter() {
@Override
public boolean accept(XMLEvent event) {
return event.isStartElement();
}
};
try {
XMLInputFactory staxFactory = XMLInputFactory.newInstance();
XMLEventReader staxReader = staxFactory.createXMLEventReader(presetIS);
XMLEventReader staxFiltRd = staxFactory.createFilteredReader(staxReader, startElementFilter);
// Ignore StartElement:
staxFiltRd.nextEvent();
// JAXB:
JAXBContext jaxbContext = JAXBContext.newInstance(packageName);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
while (staxFiltRd.peek() != null) {
StartElement evRoot = (StartElement) staxFiltRd.peek();
String cusName = evRoot.getName().getLocalPart();
// XMLEvent ev = staxReader.nextEvent();
JAXBElement<org.apache.poi.sl.draw.binding.CTCustomGeometry2D> el = unmarshaller.unmarshal(staxReader, CTCustomGeometry2D.class);
CTCustomGeometry2D cusGeom = el.getValue();
presets.put(cusName, new CustomGeometry(cusGeom));
}
staxFiltRd.close();
staxReader.close();
} catch (Exception e) {
throw new RuntimeException("Unable to load preset geometries.", e);
} finally {
IOUtils.closeQuietly(presetIS);
}
}
return presets.get(name);
}
use of javax.xml.stream.EventFilter in project poi by apache.
the class PresetGeometries method init.
@SuppressWarnings("unused")
public void init(InputStream is) throws XMLStreamException, JAXBException {
// StAX:
EventFilter startElementFilter = new EventFilter() {
@Override
public boolean accept(XMLEvent event) {
return event.isStartElement();
}
};
XMLInputFactory staxFactory = XMLInputFactory.newFactory();
XMLEventReader staxReader = staxFactory.createXMLEventReader(is);
XMLEventReader staxFiltRd = staxFactory.createFilteredReader(staxReader, startElementFilter);
// ignore StartElement:
/* XMLEvent evDoc = */
staxFiltRd.nextEvent();
// JAXB:
JAXBContext jaxbContext = JAXBContext.newInstance(BINDING_PACKAGE);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
long cntElem = 0;
while (staxFiltRd.peek() != null) {
StartElement evRoot = (StartElement) staxFiltRd.peek();
String name = evRoot.getName().getLocalPart();
JAXBElement<CTCustomGeometry2D> el = unmarshaller.unmarshal(staxReader, CTCustomGeometry2D.class);
CTCustomGeometry2D cus = el.getValue();
cntElem++;
if (containsKey(name)) {
LOG.log(POILogger.WARN, "Duplicate definition of " + name);
}
put(name, new CustomGeometry(cus));
}
}
Aggregations