Search in sources :

Example 36 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project uPortal by Jasig.

the class XsltPortalUrlProviderTest method setup.

@Before
public void setup() throws Exception {
    final ResourceLoaderURIResolver resolver = new ResourceLoaderURIResolver(new ClassRelativeResourceLoader(getClass()));
    this.tFactory = TransformerFactory.newInstance();
    this.tFactory.setURIResolver(resolver);
    this.tFactory.setErrorListener(new SimpleTransformErrorListener(LogFactory.getLog(getClass())));
    // Load the XML document so it reads the same way the rendering pipeline reads XML
    final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
    final XMLEventReader eventReader = xmlInputFactory.createXMLEventReader(this.getClass().getResourceAsStream("test.xml"));
    final XMLStreamReader streamReader = new FixedXMLEventStreamReader(eventReader);
    xmlSource = new StAXSource(streamReader);
    xslTemplate = tFactory.newTemplates(new StreamSource(this.getClass().getResourceAsStream("test.xsl")));
    expected = IOUtils.toString(this.getClass().getResourceAsStream("result.xml"));
}
Also used : ResourceLoaderURIResolver(org.apereo.portal.xml.ResourceLoaderURIResolver) ClassRelativeResourceLoader(org.springframework.core.io.ClassRelativeResourceLoader) XMLStreamReader(javax.xml.stream.XMLStreamReader) SimpleTransformErrorListener(org.springframework.util.xml.SimpleTransformErrorListener) FixedXMLEventStreamReader(org.springframework.util.xml.FixedXMLEventStreamReader) StreamSource(javax.xml.transform.stream.StreamSource) XMLEventReader(javax.xml.stream.XMLEventReader) StAXSource(javax.xml.transform.stax.StAXSource) XMLInputFactory(javax.xml.stream.XMLInputFactory) Before(org.junit.Before)

Example 37 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project Payara by payara.

the class DomainXmlTransformer method transform.

public File transform(boolean keepPorts) {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    XMLEventReader parser = null;
    XMLEventWriter writer = null;
    XMLInputFactory xif = (XMLInputFactory.class.getClassLoader() == null) ? XMLInputFactory.newInstance() : XMLInputFactory.newInstance(XMLInputFactory.class.getName(), XMLInputFactory.class.getClassLoader());
    Set<String> empty_elements = (keepPorts) ? EMPTY_ELEMENTS_KEEP_PORTS : EMPTY_ELEMENTS;
    try {
        fis = new FileInputStream(in);
        out = File.createTempFile("domain", "xml");
        if (_logger.isLoggable(Level.FINE)) {
            _logger.fine("[DomainXmlTransformer] Creating temp domain file: " + out);
        }
        if (System.getProperty(EJBContainerProviderImpl.KEEP_TEMPORARY_FILES) == null) {
            out.deleteOnExit();
        }
        fos = new FileOutputStream(out);
        parser = xif.createXMLEventReader(fis);
        writer = xof.createXMLEventWriter(fos);
        boolean fixedDasConfig = false;
        while (parser.hasNext()) {
            XMLEvent event = parser.nextEvent();
            if (event.isStartElement()) {
                String name = event.asStartElement().getName().getLocalPart();
                if (SKIP_ELEMENTS.contains(name)) {
                    if (_logger.isLoggable(Level.FINE)) {
                        _logger.fine("[DomainXmlTransformer] Skipping all of: " + name);
                    }
                    getEndEventFor(parser, name);
                    continue;
                }
                boolean skip_to_end = false;
                if (empty_elements.contains(name)) {
                    if (_logger.isLoggable(Level.FINE)) {
                        _logger.fine("[DomainXmlTransformer] Skipping details of: " + name);
                    }
                    skip_to_end = true;
                } else if (SKIP_SETTINGS_ELEMENTS.contains(name)) {
                    // Make sure lazy init is not enabled by creating a new start element
                    // based on the original but that never includes the lazy init attribute
                    event = getSkippedElementStartEvent(event);
                    skip_to_end = true;
                } else if (DISABLE_ELEMENTS.contains(name)) {
                    // Disable this element
                    event = getReplaceAttributeInStartEvent(event, ENABLED, FALSE);
                    skip_to_end = true;
                } else if (JMS_HOST.equals(name)) {
                    // Set lazy-init to false
                    event = getReplaceAttributeInStartEvent(event, LAZY_INIT_ATTR, FALSE);
                    skip_to_end = true;
                } else if (DAS_CONFIG.equals(name)) {
                    // Set dynamic-reload-enabled to false
                    event = getReplaceAttributeInStartEvent(event, DYNAMIC_RELOAD_ENABLED, FALSE);
                    fixedDasConfig = true;
                    skip_to_end = true;
                } else if (JAVA_CONFIG.equals(name)) {
                    // Add jvm-options
                    writer.add(event);
                    event = getAddedEvent(event, writer, JVM_OPTIONS, INITIALIZE_ON_DEMAND);
                }
                if (skip_to_end) {
                    writer.add(event);
                    event = getEndEventFor(parser, name);
                }
            } else if (event.isEndElement()) {
                String name = event.asEndElement().getName().getLocalPart();
                if (ADMIN_SERVICE.equals(name)) {
                    if (!fixedDasConfig) {
                        writer.add(getAddedEventBeforeEndElement(event, writer, DAS_CONFIG, DYNAMIC_RELOAD_ENABLED, FALSE));
                    }
                    // for the next config
                    fixedDasConfig = false;
                }
            }
            if (_logger.isLoggable(Level.FINEST)) {
                _logger.finest("[DomainXmlTransformer] Processing: " + event);
            }
            writer.add(event);
        }
        writer.flush();
        writer.close();
    } catch (Exception e) {
        _logger.log(Level.SEVERE, "ejb.embedded.tmp_file_create_error", e.getMessage());
        _logger.log(Level.FINE, e.getMessage(), e);
        return null;
    } finally {
        try {
            if (parser != null) {
                parser.close();
            }
        } catch (Exception e) {
        }
        try {
            if (fis != null) {
                fis.close();
            }
        } catch (Exception e) {
        }
        try {
            if (writer != null) {
                writer.close();
            }
        } catch (Exception e) {
        }
        try {
            if (fos != null) {
                fos.close();
            }
        } catch (Exception e) {
        }
    }
    if (_logger.isLoggable(Level.FINE)) {
        _logger.fine("[DomainXmlTransformer] Created temp domain file: " + out);
    }
    return out;
}
Also used : XMLEventWriter(javax.xml.stream.XMLEventWriter) FileOutputStream(java.io.FileOutputStream) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) FileInputStream(java.io.FileInputStream) XMLInputFactory(javax.xml.stream.XMLInputFactory) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException)

Example 38 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project tomee by apache.

the class StaxCompare method compare.

public static void compare(final String a, final String b) throws Exception {
    final StringBuilder message = new StringBuilder();
    final XMLInputFactory factory = XMLInputFactory.newInstance();
    factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
    final XMLEventReader rA = factory.createXMLEventReader(new StringReader(a));
    final XMLEventReader rB = factory.createXMLEventReader(new StringReader(b));
    if (!compare(rA, rB, message)) {
        throw new Exception(message.toString());
    }
}
Also used : StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 39 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project languagetool by languagetool-org.

the class IpaExtractor method run.

private void run(FileInputStream fis) throws XMLStreamException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLEventReader reader = factory.createXMLEventReader(fis);
    String title = null;
    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        if (event.getEventType() == XMLStreamConstants.START_ELEMENT) {
            String elementName = event.asStartElement().getName().getLocalPart();
            switch(elementName) {
                case "title":
                    XMLEvent nextEvent = reader.nextEvent();
                    title = nextEvent.asCharacters().getData();
                    articleCount++;
                    break;
                case "text":
                    ipaCount += handleTextElement(title, reader);
                    break;
            }
        }
    }
}
Also used : XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 40 with XMLEventReader

use of javax.xml.stream.XMLEventReader in project languagetool by languagetool-org.

the class AtomFeedParser method getAtomFeedItems.

List<AtomFeedItem> getAtomFeedItems(InputStream xml) throws XMLStreamException {
    String id = null;
    String title = null;
    Date date = null;
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader eventReader = inputFactory.createXMLEventReader(xml);
    try {
        List<AtomFeedItem> items = new ArrayList<>();
        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            if (event.isStartElement()) {
                String localPart = event.asStartElement().getName().getLocalPart();
                switch(localPart) {
                    case "id":
                        id = getCharacterData(eventReader);
                        break;
                    case "title":
                        title = getCharacterData(eventReader);
                        break;
                    case "updated":
                        String dateString = getCharacterData(eventReader);
                        try {
                            // e.g. 2013-12-03T09:48:29Z - got this from http://stackoverflow.com/questions/6038136,
                            // with SimpleDateParser the hour is off by one:
                            date = DatatypeFactory.newInstance().newXMLGregorianCalendar(dateString).toGregorianCalendar().getTime();
                        } catch (Exception e) {
                            throw new RuntimeException("Could not parse date string '" + dateString + "'", e);
                        }
                        break;
                    case "summary":
                        if (id == null || title == null || date == null) {
                            throw new RuntimeException("id, title and/or date is null: id=" + id + ", title=" + title + ", date=" + date);
                        }
                        items.add(new AtomFeedItem(id, title, getCharacterData(eventReader), date));
                        id = null;
                        title = null;
                        date = null;
                        break;
                }
            }
        }
        return items;
    } finally {
        eventReader.close();
    }
}
Also used : ArrayList(java.util.ArrayList) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) Date(java.util.Date) XMLInputFactory(javax.xml.stream.XMLInputFactory) XMLStreamException(javax.xml.stream.XMLStreamException)

Aggregations

XMLEventReader (javax.xml.stream.XMLEventReader)71 XMLInputFactory (javax.xml.stream.XMLInputFactory)36 XMLEvent (javax.xml.stream.events.XMLEvent)34 XMLStreamException (javax.xml.stream.XMLStreamException)23 StringReader (java.io.StringReader)18 Test (org.junit.Test)17 InputStream (java.io.InputStream)15 StAXSource (javax.xml.transform.stax.StAXSource)13 StartElement (javax.xml.stream.events.StartElement)12 IOException (java.io.IOException)10 Unmarshaller (javax.xml.bind.Unmarshaller)9 Attribute (javax.xml.stream.events.Attribute)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 StringWriter (java.io.StringWriter)6 ArrayList (java.util.ArrayList)6 QName (javax.xml.namespace.QName)6 Document (org.w3c.dom.Document)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 InputStreamReader (java.io.InputStreamReader)5 JAXBContext (javax.xml.bind.JAXBContext)5