Search in sources :

Example 16 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project lucene-solr by apache.

the class AddBlockUpdateTest method testXML.

//This is the same as testSolrJXML above but uses the XMLLoader
// to illustrate the structure of the XML documents
@Test
public void testXML() throws IOException, XMLStreamException {
    UpdateRequest req = new UpdateRequest();
    List<SolrInputDocument> docs = new ArrayList<>();
    String xml_doc1 = "<doc >" + "  <field name=\"id\">1</field>" + "  <field name=\"parent_s\">X</field>" + "<doc>  " + "  <field name=\"id\" >2</field>" + "  <field name=\"child_s\">y</field>" + "</doc>" + "<doc>  " + "  <field name=\"id\" >3</field>" + "  <field name=\"child_s\">z</field>" + "</doc>" + "</doc>";
    String xml_doc2 = "<doc >" + "  <field name=\"id\">4</field>" + "  <field name=\"parent_s\">A</field>" + "<doc>  " + "  <field name=\"id\" >5</field>" + "  <field name=\"child_s\">b</field>" + "</doc>" + "<doc>  " + "  <field name=\"id\" >6</field>" + "  <field name=\"child_s\">c</field>" + "</doc>" + "</doc>";
    XMLStreamReader parser = inputFactory.createXMLStreamReader(new StringReader(xml_doc1));
    // read the START document...
    parser.next();
    //null for the processor is all right here
    XMLLoader loader = new XMLLoader();
    SolrInputDocument document1 = loader.readDoc(parser);
    XMLStreamReader parser2 = inputFactory.createXMLStreamReader(new StringReader(xml_doc2));
    // read the START document...
    parser2.next();
    //null for the processor is all right here
    //XMLLoader loader = new XMLLoader();
    SolrInputDocument document2 = loader.readDoc(parser2);
    docs.add(document1);
    docs.add(document2);
    Collections.shuffle(docs, random());
    req.add(docs);
    RequestWriter requestWriter = new RequestWriter();
    OutputStream os = new ByteArrayOutputStream();
    requestWriter.write(req, os);
    assertBlockU(os.toString());
    assertU(commit());
    final SolrIndexSearcher searcher = getSearcher();
    assertSingleParentOf(searcher, one("yz"), "X");
    assertSingleParentOf(searcher, one("bc"), "A");
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) XMLStreamReader(javax.xml.stream.XMLStreamReader) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) OutputStream(java.io.OutputStream) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) RequestWriter(org.apache.solr.client.solrj.request.RequestWriter) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) XMLLoader(org.apache.solr.handler.loader.XMLLoader) Test(org.junit.Test)

Example 17 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project karaf by apache.

the class MavenConfigService method getLocalRepositoryFromSettings.

private static String getLocalRepositoryFromSettings(File file) {
    XMLStreamReader reader = null;
    try (InputStream fin = new FileInputStream(file)) {
        reader = XMLInputFactory.newFactory().createXMLStreamReader(fin);
        int event;
        String elementName = null;
        while ((event = reader.next()) != XMLStreamConstants.END_DOCUMENT) {
            if (event == XMLStreamConstants.START_ELEMENT) {
                elementName = reader.getLocalName();
            } else if (event == XMLStreamConstants.END_ELEMENT) {
                elementName = null;
            } else if (event == XMLStreamConstants.CHARACTERS && "localRepository".equals(elementName)) {
                return reader.getText().trim();
            }
        }
    } catch (Exception e) {
        logger.error("Error retrieving maven configuration", e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (XMLStreamException e) {
            // Ignore
            }
        }
    }
    return null;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 18 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project wildfly by wildfly.

the class JSFManagedBeanProcessor method processXmlManagedBeans.

/**
     * Parse the faces config files looking for managed bean classes. The parser is quite
     * simplistic as the only information we need is the managed-bean-class element
     */
private void processXmlManagedBeans(final DeploymentUnit deploymentUnit, final Set<String> managedBeanClasses) {
    for (final VirtualFile facesConfig : getConfigurationFiles(deploymentUnit)) {
        InputStream is = null;
        try {
            is = facesConfig.openStream();
            final XMLInputFactory inputFactory = XMLInputFactory.newInstance();
            inputFactory.setXMLResolver(NoopXMLResolver.create());
            XMLStreamReader parser = inputFactory.createXMLStreamReader(is);
            StringBuilder className = null;
            int indent = 0;
            boolean managedBean = false;
            boolean managedBeanClass = false;
            while (true) {
                int event = parser.next();
                if (event == XMLStreamConstants.END_DOCUMENT) {
                    parser.close();
                    break;
                }
                if (event == XMLStreamConstants.START_ELEMENT) {
                    indent++;
                    if (indent == 2) {
                        if (parser.getLocalName().equals(MANAGED_BEAN)) {
                            managedBean = true;
                        }
                    } else if (indent == 3 && managedBean) {
                        if (parser.getLocalName().equals(MANAGED_BEAN_CLASS)) {
                            managedBeanClass = true;
                            className = new StringBuilder();
                        }
                    }
                } else if (event == XMLStreamConstants.END_ELEMENT) {
                    indent--;
                    managedBeanClass = false;
                    if (indent == 1) {
                        managedBean = false;
                    }
                    if (className != null) {
                        managedBeanClasses.add(className.toString().trim());
                        className = null;
                    }
                } else if (managedBeanClass && event == XMLStreamConstants.CHARACTERS) {
                    className.append(parser.getText());
                }
            }
        } catch (Exception e) {
            JSFLogger.ROOT_LOGGER.managedBeansConfigParseFailed(facesConfig);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (IOException e) {
            // Ignore
            }
        }
    }
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) XMLInputFactory(javax.xml.stream.XMLInputFactory) DeploymentUnitProcessingException(org.jboss.as.server.deployment.DeploymentUnitProcessingException) IOException(java.io.IOException)

Example 19 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project spring-framework by spring-projects.

the class Jaxb2CollectionHttpMessageConverter method read.

@Override
@SuppressWarnings("unchecked")
public T read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    ParameterizedType parameterizedType = (ParameterizedType) type;
    T result = createCollection((Class<?>) parameterizedType.getRawType());
    Class<?> elementClass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
    try {
        Unmarshaller unmarshaller = createUnmarshaller(elementClass);
        XMLStreamReader streamReader = this.inputFactory.createXMLStreamReader(inputMessage.getBody());
        int event = moveToFirstChildOfRootElement(streamReader);
        while (event != XMLStreamReader.END_DOCUMENT) {
            if (elementClass.isAnnotationPresent(XmlRootElement.class)) {
                result.add(unmarshaller.unmarshal(streamReader));
            } else if (elementClass.isAnnotationPresent(XmlType.class)) {
                result.add(unmarshaller.unmarshal(streamReader, elementClass).getValue());
            } else {
                // should not happen, since we check in canRead(Type)
                throw new HttpMessageConversionException("Could not unmarshal to [" + elementClass + "]");
            }
            event = moveToNextElement(streamReader);
        }
        return result;
    } catch (UnmarshalException ex) {
        throw new HttpMessageNotReadableException("Could not unmarshal to [" + elementClass + "]: " + ex.getMessage(), ex);
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
    } catch (XMLStreamException ex) {
        throw new HttpMessageConversionException(ex.getMessage(), ex);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBException(javax.xml.bind.JAXBException) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) XMLStreamException(javax.xml.stream.XMLStreamException) UnmarshalException(javax.xml.bind.UnmarshalException) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 20 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project buck by facebook.

the class XmlUtils method getRootTagName.

/**
     * Returns the name of the root element tag stored in the given file, or null if it can't be
     * determined.
     */
@Nullable
public static String getRootTagName(@NonNull File xmlFile) {
    try (InputStream stream = new BufferedInputStream(new FileInputStream(xmlFile))) {
        XMLInputFactory factory = XMLInputFactory.newFactory();
        XMLStreamReader xmlStreamReader = factory.createXMLStreamReader(stream);
        while (xmlStreamReader.hasNext()) {
            int event = xmlStreamReader.next();
            if (event == XMLStreamReader.START_ELEMENT) {
                return xmlStreamReader.getLocalName();
            }
        }
    } catch (XMLStreamException | IOException ignored) {
    // Ignored.
    }
    return null;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) XMLInputFactory(javax.xml.stream.XMLInputFactory) Nullable(com.android.annotations.Nullable)

Aggregations

XMLStreamReader (javax.xml.stream.XMLStreamReader)1074 Test (org.junit.Test)486 InputStream (java.io.InputStream)451 ByteArrayInputStream (java.io.ByteArrayInputStream)379 ByteArrayOutputStream (java.io.ByteArrayOutputStream)334 Document (org.w3c.dom.Document)311 XMLStreamException (javax.xml.stream.XMLStreamException)288 ArrayList (java.util.ArrayList)270 XMLSecurityProperties (org.apache.xml.security.stax.ext.XMLSecurityProperties)242 XMLInputFactory (javax.xml.stream.XMLInputFactory)211 QName (javax.xml.namespace.QName)208 DOMSource (javax.xml.transform.dom.DOMSource)206 StringReader (java.io.StringReader)196 SecretKey (javax.crypto.SecretKey)188 StreamResult (javax.xml.transform.stream.StreamResult)183 DocumentBuilder (javax.xml.parsers.DocumentBuilder)178 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)160 InboundXMLSec (org.apache.xml.security.stax.ext.InboundXMLSec)155 IOException (java.io.IOException)144 Key (java.security.Key)103