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");
}
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;
}
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
}
}
}
}
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);
}
}
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;
}
Aggregations