Search in sources :

Example 1 with EventFilter

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;
}
Also used : JAXBException(javax.xml.bind.JAXBException) ArrayList(java.util.ArrayList) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) EventFilter(javax.xml.stream.EventFilter) LineNumberReader(java.io.LineNumberReader) StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 2 with EventFilter

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);
}
Also used : InputStream(java.io.InputStream) CTCustomGeometry2D(org.apache.poi.sl.draw.binding.CTCustomGeometry2D) JAXBContext(javax.xml.bind.JAXBContext) EventFilter(javax.xml.stream.EventFilter) StartElement(javax.xml.stream.events.StartElement) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) CustomGeometry(org.apache.poi.sl.draw.geom.CustomGeometry) Unmarshaller(javax.xml.bind.Unmarshaller) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 3 with EventFilter

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));
    }
}
Also used : StartElement(javax.xml.stream.events.StartElement) CTCustomGeometry2D(org.apache.poi.sl.draw.binding.CTCustomGeometry2D) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) EventFilter(javax.xml.stream.EventFilter) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

JAXBContext (javax.xml.bind.JAXBContext)3 Unmarshaller (javax.xml.bind.Unmarshaller)3 EventFilter (javax.xml.stream.EventFilter)3 XMLEventReader (javax.xml.stream.XMLEventReader)3 XMLInputFactory (javax.xml.stream.XMLInputFactory)3 StartElement (javax.xml.stream.events.StartElement)3 XMLEvent (javax.xml.stream.events.XMLEvent)3 CTCustomGeometry2D (org.apache.poi.sl.draw.binding.CTCustomGeometry2D)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 LineNumberReader (java.io.LineNumberReader)1 ArrayList (java.util.ArrayList)1 JAXBException (javax.xml.bind.JAXBException)1 CustomGeometry (org.apache.poi.sl.draw.geom.CustomGeometry)1