Search in sources :

Example 56 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project teiid by teiid.

the class XMLSystemFunctions method serialize.

public static Object serialize(XMLSerialize xs, XMLType value) throws TransformationException {
    Type type = value.getType();
    final Charset encoding;
    if (xs.getEncoding() != null) {
        encoding = Charset.forName(xs.getEncoding());
    } else {
        encoding = UTF_8;
    }
    if (Boolean.TRUE.equals(xs.getDeclaration())) {
        // need to replace existing/default declaration
        if (type == Type.ELEMENT || type == Type.DOCUMENT) {
            XMLEventFactory xmlEventFactory = threadLocalEventtFactory.get();
            xmlEventFactory.setLocation(dummyLocation);
            XMLEvent start = null;
            if (xs.getVersion() != null) {
                start = xmlEventFactory.createStartDocument(encoding.name(), xs.getVersion());
            } else {
                // use the encoding regardless as different stax impls have different default
                // behavior
                start = xmlEventFactory.createStartDocument(encoding.name());
            }
            StAXSourceProvider sourceProvider = new DeclarationStaxSourceProvider(start, value);
            value = new XMLType(new StAXSQLXML(sourceProvider, encoding));
            value.setType(type);
        }
    // else just ignore, since the result is likely invalid
    } else if (type == Type.DOCUMENT && Boolean.FALSE.equals(xs.getDeclaration())) {
        final XMLType v = value;
        StAXSourceProvider sourceProvider = new StAXSourceProvider() {

            @Override
            public StAXSource getStaxSource() throws SQLException {
                try {
                    XMLEventReader eventReader = getXMLEventReader(v.getSource(StAXSource.class));
                    eventReader = XMLType.getXmlInputFactory().createFilteredReader(eventReader, declarationOmittingFilter);
                    return new StAXSource(eventReader);
                } catch (XMLStreamException e) {
                    throw new SQLException(e);
                }
            }
        };
        value = new XMLType(new StAXSQLXML(sourceProvider, encoding));
        value.setType(Type.DOCUMENT);
    }
    if (xs.getType() == DataTypeManager.DefaultDataClasses.STRING) {
        return DataTypeManager.transformValue(value, xs.getType());
    }
    if (xs.getType() == DataTypeManager.DefaultDataClasses.CLOB) {
        InputStreamFactory isf = Evaluator.getInputStreamFactory(value);
        return new ClobType(new ClobImpl(isf, -1));
    }
    if (xs.getType() == DataTypeManager.DefaultDataClasses.VARBINARY) {
        try {
            InputStream is = null;
            if (!Charset.forName(value.getEncoding()).equals(encoding)) {
                is = new ReaderInputStream(value.getCharacterStream(), encoding);
            } else {
                is = value.getBinaryStream();
            }
            byte[] bytes = ObjectConverterUtil.convertToByteArray(is, DataTypeManager.MAX_VARBINARY_BYTES);
            return new BinaryType(bytes);
        } catch (SQLException e) {
            // $NON-NLS-1$ //$NON-NLS-2$
            throw new TransformationException(CorePlugin.Event.TEIID10080, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10080, "XML", "VARBINARY"));
        } catch (IOException e) {
            // $NON-NLS-1$ //$NON-NLS-2$
            throw new TransformationException(CorePlugin.Event.TEIID10080, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10080, "XML", "VARBINARY"));
        }
    }
    InputStreamFactory isf = null;
    if (!Charset.forName(value.getEncoding()).equals(encoding)) {
        // create a wrapper for the input stream
        isf = new InputStreamFactory.SQLXMLInputStreamFactory(value) {

            public InputStream getInputStream() throws IOException {
                try {
                    return new ReaderInputStream(sqlxml.getCharacterStream(), encoding);
                } catch (SQLException e) {
                    throw new IOException(e);
                }
            }
        };
    } else {
        isf = Evaluator.getInputStreamFactory(value);
    }
    return new BlobType(new BlobImpl(isf));
}
Also used : SQLException(java.sql.SQLException) XMLEventFactory(javax.xml.stream.XMLEventFactory) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) XMLEventReader(javax.xml.stream.XMLEventReader) StAXSQLXML(org.teiid.util.StAXSQLXML) StAXSourceProvider(org.teiid.util.StAXSQLXML.StAXSourceProvider) ReaderInputStream(org.teiid.core.util.ReaderInputStream) Charset(java.nio.charset.Charset) StAXSource(javax.xml.transform.stax.StAXSource) Type(org.teiid.core.types.XMLType.Type) ReaderInputStream(org.teiid.core.util.ReaderInputStream) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent)

Example 57 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project teiid by teiid.

the class TestXMLSystemFunctions method testRepairingNamespaces.

@Test
public void testRepairingNamespaces() throws Exception {
    XMLOutputFactory factory = XMLSystemFunctions.getOutputFactory(true);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLEventWriter writer = factory.createXMLEventWriter(baos);
    XMLEventReader reader = XMLType.getXmlInputFactory().createXMLEventReader(new StringReader("<a xmlns:x=\"http://foo\"><b x:y=\"1\"/></a>"));
    reader.nextTag();
    reader = XMLType.getXmlInputFactory().createFilteredReader(reader, new EventFilter() {

        @Override
        public boolean accept(XMLEvent arg0) {
            if (arg0.isStartDocument() || arg0.isEndDocument()) {
                return false;
            }
            if (arg0.isEndElement() && ((EndElement) arg0).getName().getLocalPart().equals("a")) {
                return false;
            }
            return true;
        }
    });
    writer.add(reader);
    writer.close();
    assertEquals("<b xmlns=\"\" xmlns:x=\"http://foo\" x:y=\"1\"></b>", new String(baos.toByteArray(), "UTF-8"));
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XMLEventWriter(javax.xml.stream.XMLEventWriter) StringReader(java.io.StringReader) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) EventFilter(javax.xml.stream.EventFilter) Test(org.junit.Test)

Example 58 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project bw-calendar-engine by Bedework.

the class DmozStructure method processInFile.

boolean processInFile(File f) throws Throwable {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    Reader fileReader = new FileReader(f);
    XMLEventReader reader = factory.createXMLEventReader(fileReader);
    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        if (skippingElement != null) {
            if (event.isEndElement()) {
                QName elname = event.asEndElement().getName();
                if (elname.equals(skippingElement)) {
                    skippingElement = null;
                }
            }
            continue;
        }
        if (event.isStartElement()) {
            processStartElement(reader, event.asStartElement());
            continue;
        }
        if (event.isEndElement()) {
            processEndElement(reader, event.asEndElement());
            continue;
        }
        if (event.isCharacters()) {
            String s = event.asCharacters().getData();
            if (curText != null) {
                curText.append(s);
                continue;
            }
            if (s.trim().length() == 0) {
                continue;
            }
            info("Text: " + s);
            continue;
        }
    }
    return true;
}
Also used : QName(javax.xml.namespace.QName) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) LineNumberReader(java.io.LineNumberReader) Reader(java.io.Reader) FileReader(java.io.FileReader) XMLEventReader(javax.xml.stream.XMLEventReader) FileReader(java.io.FileReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 59 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project tutorials by eugenp.

the class StaxParser method getAllTutorial.

public List<Tutorial> getAllTutorial() {
    boolean bTitle = false;
    boolean bDescription = false;
    boolean bDate = false;
    boolean bAuthor = false;
    List<Tutorial> tutorials = new ArrayList<Tutorial>();
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(this.getFile()));
        Tutorial current = null;
        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();
            switch(event.getEventType()) {
                case XMLStreamConstants.START_ELEMENT:
                    StartElement startElement = event.asStartElement();
                    String qName = startElement.getName().getLocalPart();
                    if (qName.equalsIgnoreCase("tutorial")) {
                        current = new Tutorial();
                        Iterator<Attribute> attributes = startElement.getAttributes();
                        while (attributes.hasNext()) {
                            Attribute currentAt = attributes.next();
                            if (currentAt.getName().toString().equalsIgnoreCase("tutId")) {
                                current.setTutId(currentAt.getValue());
                            } else if (currentAt.getName().toString().equalsIgnoreCase("type")) {
                                current.setType(currentAt.getValue());
                            }
                        }
                    } else if (qName.equalsIgnoreCase("title")) {
                        bTitle = true;
                    } else if (qName.equalsIgnoreCase("description")) {
                        bDescription = true;
                    } else if (qName.equalsIgnoreCase("date")) {
                        bDate = true;
                    } else if (qName.equalsIgnoreCase("author")) {
                        bAuthor = true;
                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    Characters characters = event.asCharacters();
                    if (bTitle) {
                        if (current != null) {
                            current.setTitle(characters.getData());
                        }
                        bTitle = false;
                    }
                    if (bDescription) {
                        if (current != null) {
                            current.setDescription(characters.getData());
                        }
                        bDescription = false;
                    }
                    if (bDate) {
                        if (current != null) {
                            current.setDate(characters.getData());
                        }
                        bDate = false;
                    }
                    if (bAuthor) {
                        if (current != null) {
                            current.setAuthor(characters.getData());
                        }
                        bAuthor = false;
                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    EndElement endElement = event.asEndElement();
                    if (endElement.getName().getLocalPart().equalsIgnoreCase("tutorial")) {
                        if (current != null) {
                            tutorials.add(current);
                        }
                    }
                    break;
            }
        }
    } catch (FileNotFoundException | XMLStreamException e) {
        e.printStackTrace();
    }
    return tutorials;
}
Also used : Tutorial(com.baeldung.xml.binding.Tutorial) Attribute(javax.xml.stream.events.Attribute) EndElement(javax.xml.stream.events.EndElement) Characters(javax.xml.stream.events.Characters) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) StartElement(javax.xml.stream.events.StartElement) XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) FileReader(java.io.FileReader) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 60 with XMLEvent

use of javax.xml.stream.events.XMLEvent in project teiid by teiid.

the class XMLReader method read.

@Override
public int read() throws IOException {
    while (pos >= builder.length()) {
        if (!reader.hasNext()) {
            return -1;
        }
        if (builder.length() > BUFFER_SIZE) {
            builder.setLength(0);
            pos = 0;
        }
        try {
            XMLEvent event = reader.nextEvent();
            writer.add(event);
            writer.flush();
        } catch (XMLStreamException e) {
            throw new IOException(e);
        }
    }
    return builder.charAt(pos++);
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLEvent(javax.xml.stream.events.XMLEvent) IOException(java.io.IOException)

Aggregations

XMLEvent (javax.xml.stream.events.XMLEvent)242 XMLEventReader (javax.xml.stream.XMLEventReader)111 XMLStreamException (javax.xml.stream.XMLStreamException)91 StartElement (javax.xml.stream.events.StartElement)86 XMLInputFactory (javax.xml.stream.XMLInputFactory)63 QName (javax.xml.namespace.QName)47 IOException (java.io.IOException)40 Attribute (javax.xml.stream.events.Attribute)40 EndElement (javax.xml.stream.events.EndElement)33 ArrayList (java.util.ArrayList)31 InputStream (java.io.InputStream)28 XMLEventWriter (javax.xml.stream.XMLEventWriter)23 Characters (javax.xml.stream.events.Characters)21 Test (org.junit.Test)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 StringWriter (java.io.StringWriter)16 StringReader (java.io.StringReader)14 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)14 HashMap (java.util.HashMap)13 XMLEventFactory (javax.xml.stream.XMLEventFactory)13