List of usage examples for javax.xml.stream XMLStreamReader getText
public String getText();
From source file:Main.java
public static String parse(XMLStreamReader reader) { StringBuffer result = new StringBuffer(); if (reader != null) { try {/*from ww w . j a v a 2s .com*/ while (reader.hasNext()) { switch (reader.getEventType()) { case XMLStreamConstants.START_ELEMENT: result.append("<"); printName(reader, result); printNamespaces(reader, result); printAttributes(reader, result); result.append(">"); break; case XMLStreamConstants.END_ELEMENT: result.append("</"); printName(reader, result); result.append(">"); break; case XMLStreamConstants.SPACE: case XMLStreamConstants.CHARACTERS: int start = reader.getTextStart(); int length = reader.getTextLength(); result.append(new String(reader.getTextCharacters(), start, length)); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: result.append("<?"); if (reader.hasText()) result.append(reader.getText()); result.append("?>"); break; case XMLStreamConstants.CDATA: result.append("<![CDATA["); start = reader.getTextStart(); length = reader.getTextLength(); result.append(new String(reader.getTextCharacters(), start, length)); result.append("]]>"); break; case XMLStreamConstants.COMMENT: result.append("<!--"); if (reader.hasText()) result.append(reader.getText()); result.append("-->"); break; case XMLStreamConstants.ENTITY_REFERENCE: result.append(reader.getLocalName()).append("="); if (reader.hasText()) result.append("[").append(reader.getText()).append("]"); break; case XMLStreamConstants.START_DOCUMENT: result.append("<?xml"); result.append(" version='").append(reader.getVersion()).append("'"); result.append(" encoding='").append(reader.getCharacterEncodingScheme()).append("'"); if (reader.isStandalone()) result.append(" standalone='yes'"); else result.append(" standalone='no'"); result.append("?>"); break; } reader.next(); } // end while } catch (XMLStreamException e) { throw new RuntimeException(e); } finally { try { reader.close(); } catch (XMLStreamException e) { } } } return result.toString(); }
From source file:hudson.model.ExternalRun.java
/** * Instead of performing a build, accept the log and the return code from a * remote machine./*from w w w. j a va 2 s. c o m*/ * * <p> The format of the XML is: * * <pre><xmp> * <run> * <log>...console output...</log> * <result>exit code</result> * </run> * </xmp></pre> */ @SuppressWarnings({ "Since15" }) public void acceptRemoteSubmission(final Reader in) throws IOException { final long[] duration = new long[1]; run(new Runner() { private String elementText(XMLStreamReader r) throws XMLStreamException { StringBuilder buf = new StringBuilder(); while (true) { int type = r.next(); if (type == CHARACTERS || type == CDATA) { buf.append(r.getTextCharacters(), r.getTextStart(), r.getTextLength()); } else { return buf.toString(); } } } public Result run(BuildListener listener) throws Exception { PrintStream logger = null; try { logger = new PrintStream(new DecodingStream(listener.getLogger())); XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader p = xif.createXMLStreamReader(in); p.nextTag(); // get to the <run> p.nextTag(); // get to the <log> charset = p.getAttributeValue(null, "content-encoding"); while (p.next() != END_ELEMENT) { int type = p.getEventType(); if (type == CHARACTERS || type == CDATA) { logger.print(p.getText()); } } p.nextTag(); // get to <result> Result r = Integer.parseInt(elementText(p)) == 0 ? Result.SUCCESS : Result.FAILURE; p.nextTag(); // get to <duration> (optional) if (p.getEventType() == START_ELEMENT && p.getLocalName().equals("duration")) { duration[0] = Long.parseLong(elementText(p)); } return r; } finally { IOUtils.closeQuietly(logger); } } public void post(BuildListener listener) { // do nothing } public void cleanUp(BuildListener listener) { // do nothing } }); if (duration[0] != 0) { super.duration = duration[0]; // save the updated duration save(); } }
From source file:davmail.exchange.dav.ExchangeDavMethod.java
protected String getTagContent(XMLStreamReader reader) throws XMLStreamException { String value = null;/*from ww w .j a va 2 s .co m*/ String tagLocalName = reader.getLocalName(); while (reader.hasNext() && !((reader.getEventType() == XMLStreamConstants.END_ELEMENT) && tagLocalName.equals(reader.getLocalName()))) { reader.next(); if (reader.getEventType() == XMLStreamConstants.CHARACTERS) { value = reader.getText(); } } // empty tag if (!reader.hasNext()) { throw new XMLStreamException("End element for " + tagLocalName + " not found"); } return value; }
From source file:com.predic8.membrane.core.http.xml.Response.java
@Override protected void parseChildren(XMLStreamReader token, String child) throws Exception { if (Headers.ELEMENT_NAME.equals(child)) { headers = (Headers) new Headers().parse(token); } else if ("status".equals(child)) { statusCode = Integer//w w w .j av a2 s. c o m .parseInt(StringUtils.defaultIfBlank(token.getAttributeValue("", "status-code"), "0")); statusMessage = ""; while (token.hasNext()) { token.next(); if (token.isStartElement()) { parseChildren(token, token.getName().getLocalPart()); } else if (token.isCharacters()) { statusMessage += token.getText(); } else if (token.isEndElement()) { break; } } } }
From source file:hudson.plugins.report.jck.parsers.JtregReportParser.java
private String captureCharacters(XMLStreamReader in, String element) throws Exception { while (in.hasNext()) { int event = in.next(); if (event == END_ELEMENT && element.equals(in.getLocalName())) { break; }/*w w w .ja v a2 s . c o m*/ if (event == CHARACTERS) { return in.getText(); } } return ""; }
From source file:Main.java
private static void parseRestOfDocument(XMLStreamReader reader) throws XMLStreamException { while (reader.hasNext()) { int type = reader.next(); switch (type) { case XMLStreamConstants.START_ELEMENT: System.out.println(reader.getLocalName()); if (reader.getNamespaceURI() != null) { String prefix = reader.getPrefix(); if (prefix == null) { prefix = "[None]"; }//w ww .j a v a 2 s. co m System.out.println("prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'"); } if (reader.getAttributeCount() > 0) { for (int i = 0; i < reader.getAttributeCount(); i++) { System.out.println("Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '" + reader.getAttributeValue(i) + "')"); String attURI = reader.getAttributeNamespace(i); if (attURI != null) { String attPrefix = reader.getAttributePrefix(i); if (attPrefix == null || attPrefix.equals("")) { attPrefix = "[None]"; } System.out.println("prefix=" + attPrefix + ",URI=" + attURI); } } } break; case XMLStreamConstants.END_ELEMENT: System.out.println("XMLStreamConstants.END_ELEMENT"); break; case XMLStreamConstants.CHARACTERS: if (!reader.isWhiteSpace()) { System.out.println("CD:" + reader.getText()); } break; case XMLStreamConstants.DTD: System.out.println("DTD:" + reader.getText()); break; case XMLStreamConstants.SPACE: System.out.println(" "); break; case XMLStreamConstants.COMMENT: System.out.println(reader.getText()); break; default: System.out.println(type); } } }
From source file:com.microsoft.windowsazure.storage.table.TableParser.java
/** * Reserved for internal use. Reads the properties of an entity from the stream into a map of property names to * typed values. Reads the entity data as an AtomPub Entry Resource from the specified {@link XMLStreamReader} into * a map of <code>String</code> property names to {@link EntityProperty} data typed values. * // www . j a v a2 s .c o m * @param xmlr * The <code>XMLStreamReader</code> to read the data from. * @param opContext * An {@link OperationContext} object used to track the execution of the operation. * * @return * A <code>java.util.HashMap</code> containing a map of <code>String</code> property names to * {@link EntityProperty} data typed values found in the entity data. * @throws XMLStreamException * if an error occurs accessing the stream. * @throws ParseException * if an error occurs converting the input to a particular data type. */ private static HashMap<String, EntityProperty> readAtomProperties(final XMLStreamReader xmlr, final OperationContext opContext) throws XMLStreamException, ParseException { int eventType = xmlr.getEventType(); xmlr.require(XMLStreamConstants.START_ELEMENT, null, ODataConstants.PROPERTIES); final HashMap<String, EntityProperty> properties = new HashMap<String, EntityProperty>(); while (xmlr.hasNext()) { eventType = xmlr.next(); if (eventType == XMLStreamConstants.CHARACTERS) { xmlr.getText(); continue; } if (eventType == XMLStreamConstants.START_ELEMENT && xmlr.getNamespaceURI().equals(ODataConstants.DATA_SERVICES_NS)) { final String key = xmlr.getLocalName(); String val = Constants.EMPTY_STRING; String edmType = null; if (xmlr.getAttributeCount() > 0) { edmType = xmlr.getAttributeValue(ODataConstants.DATA_SERVICES_METADATA_NS, ODataConstants.TYPE); } // move to chars eventType = xmlr.next(); if (eventType == XMLStreamConstants.CHARACTERS) { val = xmlr.getText(); // end element eventType = xmlr.next(); } xmlr.require(XMLStreamConstants.END_ELEMENT, null, key); final EntityProperty newProp = new EntityProperty(val, EdmType.parse(edmType)); properties.put(key, newProp); } else if (eventType == XMLStreamConstants.END_ELEMENT && xmlr.getName().toString() .equals(ODataConstants.BRACKETED_DATA_SERVICES_METADATA_NS + ODataConstants.PROPERTIES)) { // End read properties break; } } xmlr.require(XMLStreamConstants.END_ELEMENT, null, ODataConstants.PROPERTIES); return properties; }
From source file:com.microsoft.windowsazure.storage.table.TableParser.java
/** * Reserved for internal use. Parses the operation response as a collection of entities. Reads entity data from the * specified input stream using the specified class type and optionally projects each entity result with the * specified resolver into an {@link ODataPayload} containing a collection of {@link TableResult} objects. * /* w w w. j a v a2 s. c om*/ * @param inStream * The <code>InputStream</code> to read the data to parse from. * @param clazzType * The class type <code>T</code> implementing {@link TableEntity} for the entities returned. Set to * <code>null</code> to ignore the returned entities and copy only response properties into the * {@link TableResult} objects. * @param resolver * An {@link EntityResolver} instance to project the entities into instances of type <code>R</code>. Set * to <code>null</code> to return the entities as instances of the class type <code>T</code>. * @param opContext * An {@link OperationContext} object used to track the execution of the operation. * @return * An {@link ODataPayload} containing a collection of {@link TableResult} objects with the parsed operation * response. * * @throws XMLStreamException * if an error occurs while accessing the stream. * @throws ParseException * if an error occurs while parsing the stream. * @throws InstantiationException * if an error occurs while constructing the result. * @throws IllegalAccessException * if an error occurs in reflection while parsing the result. * @throws StorageException * if a storage service error occurs. */ @SuppressWarnings("unchecked") private static <T extends TableEntity, R> ODataPayload<?> parseAtomQueryResponse(final InputStream inStream, final Class<T> clazzType, final EntityResolver<R> resolver, final OperationContext opContext) throws XMLStreamException, ParseException, InstantiationException, IllegalAccessException, StorageException { ODataPayload<T> corePayload = null; ODataPayload<R> resolvedPayload = null; ODataPayload<?> commonPayload = null; if (resolver != null) { resolvedPayload = new ODataPayload<R>(); commonPayload = resolvedPayload; } else { corePayload = new ODataPayload<T>(); commonPayload = corePayload; } final XMLStreamReader xmlr = Utility.createXMLStreamReaderFromStream(inStream); int eventType = xmlr.getEventType(); xmlr.require(XMLStreamConstants.START_DOCUMENT, null, null); eventType = xmlr.next(); xmlr.require(XMLStreamConstants.START_ELEMENT, null, ODataConstants.FEED); // skip feed chars eventType = xmlr.next(); while (xmlr.hasNext()) { eventType = xmlr.next(); if (eventType == XMLStreamConstants.CHARACTERS) { xmlr.getText(); continue; } final String name = xmlr.getName().toString(); if (eventType == XMLStreamConstants.START_ELEMENT) { if (name.equals(ODataConstants.BRACKETED_ATOM_NS + ODataConstants.ENTRY)) { final TableResult res = parseAtomEntity(xmlr, clazzType, resolver, opContext); if (corePayload != null) { corePayload.tableResults.add(res); } if (resolver != null) { resolvedPayload.results.add((R) res.getResult()); } else { corePayload.results.add((T) res.getResult()); } } } else if (eventType == XMLStreamConstants.END_ELEMENT && name.equals(ODataConstants.BRACKETED_ATOM_NS + ODataConstants.FEED)) { break; } } xmlr.require(XMLStreamConstants.END_ELEMENT, null, ODataConstants.FEED); return commonPayload; }
From source file:com.norconex.collector.http.sitemap.impl.StandardSitemapResolver.java
private void parseLocation(InputStream is, HttpClient httpClient, SitemapURLAdder sitemapURLAdder, Set<String> resolvedLocations, String location) throws XMLStreamException { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true); XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(is); ParseState parseState = new ParseState(); String locationDir = StringUtils.substringBeforeLast(location, "/"); int event = xmlReader.getEventType(); while (true) { switch (event) { case XMLStreamConstants.START_ELEMENT: String tag = xmlReader.getLocalName(); parseStartElement(parseState, tag); break; case XMLStreamConstants.CHARACTERS: String value = xmlReader.getText(); if (parseState.sitemapIndex && parseState.loc) { resolveLocation(value, httpClient, sitemapURLAdder, resolvedLocations); parseState.loc = false;//from w w w. j a v a2 s . c o m } else if (parseState.baseURL != null) { parseCharacters(parseState, value); } break; case XMLStreamConstants.END_ELEMENT: tag = xmlReader.getLocalName(); parseEndElement(sitemapURLAdder, parseState, locationDir, tag); break; } if (!xmlReader.hasNext()) { break; } event = xmlReader.next(); } }
From source file:com.norconex.collector.http.sitemap.impl.DefaultSitemapResolver.java
private void parseLocation(InputStream is, DefaultHttpClient httpClient, SitemapURLStore sitemapURLStore, Set<String> resolvedLocations, String location) throws XMLStreamException { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true); XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(is); ParseState parseState = new ParseState(); String locationDir = StringUtils.substringBeforeLast(location, "/"); int event = xmlReader.getEventType(); while (true) { switch (event) { case XMLStreamConstants.START_ELEMENT: String tag = xmlReader.getLocalName(); parseStartElement(parseState, tag); break; case XMLStreamConstants.CHARACTERS: String value = xmlReader.getText(); if (parseState.sitemapIndex && parseState.loc) { resolveLocation(value, httpClient, sitemapURLStore, resolvedLocations); parseState.loc = false;//w ww . jav a2s. c o m } else if (parseState.baseURL != null) { parseCharacters(parseState, value); } break; case XMLStreamConstants.END_ELEMENT: tag = xmlReader.getLocalName(); parseEndElement(sitemapURLStore, parseState, locationDir, tag); break; } if (!xmlReader.hasNext()) { break; } event = xmlReader.next(); } }