List of usage examples for javax.xml.stream XMLEventReader nextEvent
public XMLEvent nextEvent() throws XMLStreamException;
From source file:edu.jhu.hlt.concrete.ingesters.bolt.BoltForumPostIngester.java
private int handleImg(final XMLEventReader rdr) throws XMLStreamException { XMLEvent n = rdr.nextEvent(); return n.asEndElement().getLocation().getCharacterOffset(); }
From source file:fr.dutra.confluence2wordpress.core.sync.DefaultAttachmentsSynchronizer.java
private Set<Attachment> parseForAttachments(ContentEntityObject page) throws SynchronizationException { Set<Attachment> attachments = new HashSet<Attachment>(); try {//from w w w . j a v a 2 s . c o m XMLEventReader r = StaxUtils.getReader(page); String fileName = null; String pageTitle = null; String spaceKey = null; try { while (r.hasNext()) { XMLEvent e = r.nextEvent(); if (e.isStartElement()) { StartElement startElement = e.asStartElement(); QName name = startElement.getName(); if (name.equals(ATTACHMENT_QNAME)) { Attribute att = startElement.getAttributeByName(FILENAME_QNAME); if (att != null) { fileName = att.getValue(); } } else if (name.equals(PAGE_QNAME)) { Attribute title = startElement.getAttributeByName(TITLE_QNAME); if (title != null) { pageTitle = title.getValue(); } Attribute space = startElement.getAttributeByName(SPACE_QNAME); if (space != null) { spaceKey = space.getValue(); } } } else if (e.isEndElement()) { EndElement endElement = e.asEndElement(); if (endElement.getName().equals(ATTACHMENT_QNAME)) { ContentEntityObject attachmentPage; if (pageTitle == null) { attachmentPage = page; } else { attachmentPage = pageManager.getPage(spaceKey, pageTitle); } Attachment attachment = attachmentManager.getAttachment(attachmentPage, fileName); attachments.add(attachment); fileName = null; pageTitle = null; spaceKey = null; } } } } finally { r.close(); } } catch (XMLStreamException e) { throw new SynchronizationException("Cannot read page: " + page.getTitle(), e); } return attachments; }
From source file:org.callimachusproject.rdfa.test.RDFaGenerationTest.java
void testPI(XMLEventReader xml) throws Exception { while (xml.hasNext()) { XMLEvent e = xml.nextEvent(); if (e.isProcessingInstruction()) { if (e.toString().contains("repository clear") || e.toString().contains("clear repository")) { source.clear();/*from w w w .j a v a 2 s. c o m*/ } } } }
From source file:com.adobe.acs.commons.it.build.ScrMetadataIT.java
private Descriptor parseScr(InputStream is, String name, boolean checkNs) throws Exception { Descriptor result = new Descriptor(); XMLEventReader reader = xmlInputFactory.createXMLEventReader(is); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { StartElement start = event.asStartElement(); String elementName = start.getName().getLocalPart(); if (elementName.equals("component")) { result.name = start.getAttributeByName(new QName("name")).getValue(); if (checkNs) { String scrUri = start.getName().getNamespaceURI(); if (!ALLOWED_SCR_NS_URIS.contains(scrUri)) { throw new Exception( String.format("Banned Namespace URI %s found for %s", scrUri, name)); }/* w ww.j ava2 s . com*/ } } else if (elementName.equals("property")) { String propName = start.getAttributeByName(new QName("name")).getValue(); Attribute value = start.getAttributeByName(new QName("value")); Attribute typeAttr = start.getAttributeByName(new QName("type")); String type = typeAttr == null ? "String" : typeAttr.getValue(); if (value != null) { result.properties.add(new Property(propName, value.getValue(), type)); } else { result.properties.add(new Property(propName, cleanText(reader.getElementText()), type)); } } } } return result; }
From source file:edu.jhu.hlt.concrete.ingesters.bolt.BoltForumPostIngester.java
/** * Moves the rdr "iterator" past any img tags or quote tags. * * @param rdr/* www .j a v a 2s . co m*/ * @throws XMLStreamException */ private int handleNonPostStartElement(final XMLEventReader rdr) throws XMLStreamException { // Next is a start element. Throw if not. StartElement se = rdr.nextEvent().asStartElement(); QName seqn = se.getName(); String part = seqn.getLocalPart(); if (part.equals(QUOTE_LOCAL_NAME)) { return this.handleQuote(rdr); } else if (part.equals(IMG_LOCAL_NAME)) { return this.handleImg(rdr); } else if (part.equals(LINK_LOCAL_NAME)) { return this.handleLink(rdr); } else throw new IllegalArgumentException("Unhandled tag: " + part); }
From source file:com.adobe.acs.commons.it.build.ScrMetadataIT.java
private Descriptor parseMetatype(InputStream is, String name) throws Exception { Descriptor result = new Descriptor(); XMLEventReader reader = xmlInputFactory.createXMLEventReader(is); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (event.isStartElement()) { StartElement start = event.asStartElement(); String elementName = start.getName().getLocalPart(); if (elementName.equals("Designate")) { Attribute pidAttribute = start.getAttributeByName(new QName("pid")); if (pidAttribute != null) { result.name = pidAttribute.getValue(); } else { pidAttribute = start.getAttributeByName(new QName("factoryPid")); if (pidAttribute != null) { result.name = pidAttribute.getValue(); }/*from ww w . j ava 2 s . co m*/ result.factory = true; } } else if (elementName.equals("AD")) { String propName = start.getAttributeByName(new QName("id")).getValue(); Attribute value = start.getAttributeByName(new QName("default")); Attribute typeAttr = start.getAttributeByName(new QName("type")); String type = typeAttr == null ? "String" : typeAttr.getValue(); if (value == null) { result.properties.add(new Property(propName, "", type)); } else { result.properties.add(new Property(propName, value.getValue(), type)); } } } } if (result.name == null) { throw new Exception("Could not identify pid for " + name); } return result; }
From source file:fr.openwide.talendalfresco.rest.client.RestAuthenticationTest.java
public void testRestLogin() { // create client and configure it HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout); // instantiating a new method and configuring it GetMethod method = new GetMethod(restCommandUrlPrefix + "login"); method.setFollowRedirects(true); // ? // Provide custom retry handler is necessary (?) method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); NameValuePair[] params = new NameValuePair[] { new NameValuePair("username", "admin"), new NameValuePair("password", "admin") }; method.setQueryString(params);// w w w. j a va2 s . c o m try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } // Read the response body. byte[] responseBody = method.getResponseBody(); System.out.println(new String(responseBody)); // TODO rm HashSet<String> defaultElementSet = new HashSet<String>( Arrays.asList(new String[] { RestConstants.TAG_COMMAND, RestConstants.TAG_CODE, RestConstants.TAG_CONTENT, RestConstants.TAG_ERROR, RestConstants.TAG_MESSAGE })); HashMap<String, String> elementValueMap = new HashMap<String, String>(6); try { XMLEventReader xmlReader = XmlHelper.getXMLInputFactory() .createXMLEventReader(new ByteArrayInputStream(responseBody)); StringBuffer singleLevelTextBuf = null; while (xmlReader.hasNext()) { XMLEvent event = xmlReader.nextEvent(); switch (event.getEventType()) { case XMLEvent.CHARACTERS: case XMLEvent.CDATA: if (singleLevelTextBuf != null) { singleLevelTextBuf.append(event.asCharacters().getData()); } // else element not meaningful break; case XMLEvent.START_ELEMENT: StartElement startElement = event.asStartElement(); String elementName = startElement.getName().getLocalPart(); if (defaultElementSet.contains(elementName) // TODO another command specific level || "ticket".equals(elementName)) { // reinit buffer at start of meaningful elements singleLevelTextBuf = new StringBuffer(); } else { singleLevelTextBuf = null; // not useful } break; case XMLEvent.END_ELEMENT: if (singleLevelTextBuf == null) { break; // element not meaningful } // TODO or merely put it in the map since the element has been tested at start EndElement endElement = event.asEndElement(); elementName = endElement.getName().getLocalPart(); if (defaultElementSet.contains(elementName)) { String value = singleLevelTextBuf.toString(); elementValueMap.put(elementName, value); // TODO test if it is code and it is not OK, break to error handling } // TODO another command specific level else if ("ticket".equals(elementName)) { ticket = singleLevelTextBuf.toString(); } // singleLevelTextBuf = new StringBuffer(); // no ! in start break; } } } catch (XMLStreamException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Throwable t) { // TODO Auto-generated catch block t.printStackTrace(); //throw t; } String code = elementValueMap.get(RestConstants.TAG_CODE); assertTrue(RestConstants.CODE_OK.equals(code)); System.out.println("got ticket " + ticket); } catch (HttpException e) { // TODO e.printStackTrace(); } catch (IOException e) { // TODO e.printStackTrace(); } finally { // Release the connection. method.releaseConnection(); } }
From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java
public static Map.Entry<Integer, XmlElement> getAtomElement(final XMLEventReader reader, final XMLEventWriter discarded, final String name, final Collection<Map.Entry<String, String>> filterAttrs, final int initialDepth, final int minDepth, final int maxDepth, final boolean filterInOr) throws Exception { int depth = initialDepth; StartElement start = null;/*from www . ja v a 2 s . c om*/ while (reader.hasNext() && start == null) { final XMLEvent event = reader.nextEvent(); if (event.getEventType() == XMLStreamConstants.START_ELEMENT) { depth++; if ((StringUtils.isBlank(name) || name.trim().equals(event.asStartElement().getName().getLocalPart())) && (minDepth < 0 || minDepth <= depth) && (maxDepth < 0 || maxDepth >= depth)) { boolean match = filterAttrs == null || filterAttrs.isEmpty() || !filterInOr; for (Map.Entry<String, String> filterAttr : filterAttrs == null ? Collections.<Map.Entry<String, String>>emptySet() : filterAttrs) { final Attribute attr = event.asStartElement() .getAttributeByName(new QName(filterAttr.getKey().trim())); if (attr == null || !filterAttr.getValue().trim().equals(attr.getValue())) { match = filterInOr ? match : false; } else { match = filterInOr ? true : match; } } if (match) { start = event.asStartElement(); } } } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) { depth--; } if (start == null) { if (discarded != null) { discarded.add(event); } } } if (start == null) { throw new Exception(String.format("Could not find an element named '%s'", name)); } return new SimpleEntry<Integer, XmlElement>(Integer.valueOf(depth - 1), getAtomElement(start, reader)); }
From source file:edu.jhu.hlt.concrete.ingesters.bolt.BoltForumPostIngester.java
private int handleQuote(final XMLEventReader rdr) throws XMLStreamException { // For quotes, there will be character contents - skip for now... XMLEvent quoteContent = rdr.nextEvent(); if (!quoteContent.isCharacters()) throw new RuntimeException("Characters did not follow quote."); // Skip end of quote. XMLEvent next = rdr.nextEvent(); // Exit loop only when next end quote is hit. boolean hitEndQuoteElement = false; while (!next.isEndElement() && !hitEndQuoteElement) { // Move to next element. next = rdr.nextEvent();//from www . ja v a 2 s. c o m // If next element is an end element, // see if it's an end quote. // If so, exit the loop. if (next.isEndElement()) hitEndQuoteElement = next.asEndElement().getName().getLocalPart().equals("quote"); } return next.getLocation().getCharacterOffset(); }