List of usage examples for javax.xml.stream XMLStreamConstants SPACE
int SPACE
To view the source code for javax.xml.stream XMLStreamConstants SPACE.
Click Source Link
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 2 s . c o m*/ 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:Main.java
/** * Get the string representation of a given StAX event type. The returned * value is the name of the constant in {@link XMLStreamConstants} * corresponding to the event type.// ww w .jav a2s.c o m * * @param event * the event type as returned by * {@link javax.xml.stream.events.XMLEvent#getEventType()}, * {@link javax.xml.stream.XMLStreamReader#getEventType()} or * {@link javax.xml.stream.XMLStreamReader#next()} * @return a string representation of the event type */ public static String getEventTypeString(int event) { String state = null; switch (event) { case XMLStreamConstants.START_ELEMENT: state = "START_ELEMENT"; break; case XMLStreamConstants.START_DOCUMENT: state = "START_DOCUMENT"; break; case XMLStreamConstants.CHARACTERS: state = "CHARACTERS"; break; case XMLStreamConstants.CDATA: state = "CDATA"; break; case XMLStreamConstants.END_ELEMENT: state = "END_ELEMENT"; break; case XMLStreamConstants.END_DOCUMENT: state = "END_DOCUMENT"; break; case XMLStreamConstants.SPACE: state = "SPACE"; break; case XMLStreamConstants.COMMENT: state = "COMMENT"; break; case XMLStreamConstants.DTD: state = "DTD"; break; case XMLStreamConstants.PROCESSING_INSTRUCTION: state = "PROCESSING_INSTRUCTION"; break; case XMLStreamConstants.ENTITY_REFERENCE: state = "ENTITY_REFERENCE"; break; default: state = "UNKNOWN_STATE: " + event; } return state; }
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]"; }//from w w w .j ava 2 s .com 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.widowcrawler.exo.parse.Parser.java
private static String getEventTypeString(int eventType) { switch (eventType) { case XMLStreamConstants.START_ELEMENT: return "START_ELEMENT"; case XMLStreamConstants.END_ELEMENT: return "END_ELEMENT"; case XMLStreamConstants.PROCESSING_INSTRUCTION: return "PROCESSING_INSTRUCTION"; case XMLStreamConstants.CHARACTERS: return "CHARACTERS"; case XMLStreamConstants.COMMENT: return "COMMENT"; case XMLStreamConstants.START_DOCUMENT: return "START_DOCUMENT"; case XMLStreamConstants.END_DOCUMENT: return "END_DOCUMENT"; case XMLStreamConstants.ENTITY_REFERENCE: return "ENTITY_REFERENCE"; case XMLStreamConstants.ATTRIBUTE: return "ATTRIBUTE"; case XMLStreamConstants.DTD: return "DTD"; case XMLStreamConstants.CDATA: return "CDATA"; case XMLStreamConstants.SPACE: return "SPACE"; case XMLStreamConstants.ENTITY_DECLARATION: return "ENTITY_DECLARATION"; case XMLStreamConstants.NAMESPACE: return "NAMESPACE"; case XMLStreamConstants.NOTATION_DECLARATION: return "NOTATION_DECLARATION"; }// ww w. ja v a 2s .c o m return "UNKNOWN_EVENT_TYPE: " + eventType; }
From source file:StAXStreamTreeViewer.java
private void parseRestOfDocument(XMLStreamReader reader, DefaultMutableTreeNode current) throws XMLStreamException { while (reader.hasNext()) { int type = reader.next(); switch (type) { case XMLStreamConstants.START_ELEMENT: DefaultMutableTreeNode element = new DefaultMutableTreeNode(reader.getLocalName()); current.add(element);//from ww w. j av a 2 s.c o m current = element; if (reader.getNamespaceURI() != null) { String prefix = reader.getPrefix(); if (prefix == null) { prefix = "[None]"; } DefaultMutableTreeNode namespace = new DefaultMutableTreeNode( "prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'"); current.add(namespace); } if (reader.getAttributeCount() > 0) { for (int i = 0; i < reader.getAttributeCount(); i++) { DefaultMutableTreeNode attribute = new DefaultMutableTreeNode( "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]"; } DefaultMutableTreeNode attNamespace = new DefaultMutableTreeNode( "prefix=" + attPrefix + ",URI=" + attURI); attribute.add(attNamespace); } current.add(attribute); } } break; case XMLStreamConstants.END_ELEMENT: current = (DefaultMutableTreeNode) current.getParent(); break; case XMLStreamConstants.CHARACTERS: if (!reader.isWhiteSpace()) { DefaultMutableTreeNode data = new DefaultMutableTreeNode("CD:" + reader.getText()); current.add(data); } break; case XMLStreamConstants.DTD: DefaultMutableTreeNode dtd = new DefaultMutableTreeNode("DTD:" + reader.getText()); current.add(dtd); break; case XMLStreamConstants.SPACE: break; case XMLStreamConstants.COMMENT: DefaultMutableTreeNode comment = new DefaultMutableTreeNode(reader.getText()); current.add(comment); break; default: System.out.println(type); } } }
From source file:lux.search.highlight.XmlHighlighter.java
@Override public void handleEvent(XMLStreamReader reader, int eventType) throws XMLStreamException { switch (eventType) { case XMLStreamConstants.START_DOCUMENT: xmlStreamReader = reader; // cache the reader so we can pull events super.handleEvent(reader, eventType); break;/*from ww w . jav a 2 s . c o m*/ case XMLStreamConstants.START_ELEMENT: super.handleEvent(reader, eventType); xmlStreamTokens .pushElement(new QName(reader.getNamespaceURI(), reader.getLocalName(), reader.getPrefix())); break; case XMLStreamConstants.END_ELEMENT: super.handleEvent(reader, eventType); xmlStreamTokens.popElement(); break; case XMLStreamConstants.COMMENT: case XMLStreamConstants.PROCESSING_INSTRUCTION: super.handleEvent(reader, eventType); break; case XMLStreamConstants.CDATA: throw new XMLStreamException("unexpected CDATA event"); case XMLStreamConstants.SPACE: super.handleEvent(reader, eventType); break; case XMLStreamConstants.CHARACTERS: textReader.text(); try { highlightTextNode(); } catch (IOException e) { throw new XMLStreamException(e); } break; case XMLStreamConstants.ENTITY_REFERENCE: throw new XMLStreamException("unexpected entity reference event"); default: super.handleEvent(reader, eventType); } }
From source file:org.javelin.sws.ext.bind.internal.model.ComplexTypePattern.java
@Override public T consumeValue(XMLEventReader eventReader, UnmarshallingContext context) throws XMLStreamException { // first create an object to be filled (using PropertyAccessors - direct or bean) according to the content model T object = BeanUtils.instantiate(this.getJavaType()); // the order is dictated by incoming events, not by the mode // TODO: create a property to enable strict unmarshalling - dictated by content model // only this (ContentModel) pattern iterates over XML Events XMLEvent event = null;/*from www.j a va2 s. c om*/ PropertyMetadataValue<T, ?> pmv = null; // this loop will only handle first level of start elements and only single end element // deeper levels will be handled by nested patterns while (true) { boolean end = false; event = eventReader.peek(); pmv = null; switch (event.getEventType()) { case XMLStreamConstants.ATTRIBUTE: pmv = this.consumeNestedAttribute(eventReader, context); break; case XMLStreamConstants.CDATA: case XMLStreamConstants.CHARACTERS: // TODO: XMLEvent.ENTITY_REFERENCE? if (this.simpleContent != null) { pmv = this.consumeSimpleContent(eventReader, context); break; } case XMLStreamConstants.COMMENT: case XMLStreamConstants.DTD: case XMLStreamConstants.SPACE: case XMLStreamConstants.ENTITY_DECLARATION: case XMLStreamConstants.NOTATION_DECLARATION: case XMLStreamConstants.PROCESSING_INSTRUCTION: eventReader.nextEvent(); break; case XMLStreamConstants.ENTITY_REFERENCE: // TODO: XMLEvent.ENTITY_REFERENCE? eventReader.nextEvent(); break; case XMLStreamConstants.START_DOCUMENT: // strange break; case XMLStreamConstants.START_ELEMENT: pmv = this.consumeNestedElement(eventReader, context); break; case XMLStreamConstants.END_ELEMENT: // TODO: in mixed content there will be more than one end element it this content model's level case XMLStreamConstants.END_DOCUMENT: end = true; break; } if (end) break; if (pmv != null) pmv.getMetadata().setValue(object, pmv.getValue()); } return (T) object; }
From source file:edu.utah.further.core.api.xml.XmlUtil.java
/** * Basic StAX element printout. For more sophisticated functionality, use * {@link XmlStreamPrinter}.//from w w w .j a v a 2s . c o m * * @param reader * reader * @return reader's next element textual representation */ public static String getEventSimpleString(final XMLStreamReader reader) { switch (reader.getEventType()) { case XMLStreamConstants.START_ELEMENT: return "START_ELEMENT:\t\"" + reader.getLocalName() + "\""; case XMLStreamConstants.END_ELEMENT: return "END_ELEMENT:\t\"" + reader.getLocalName() + "\""; case XMLStreamConstants.START_DOCUMENT: return "START_DOCUMENT"; case XMLStreamConstants.END_DOCUMENT: return "END_DOCUMENT"; case XMLStreamConstants.CHARACTERS: return "CHARACTERS:\t\"" + reader.getText() + "\"" + " blank? " + StringUtils.isWhitespace(reader.getText()); case XMLStreamConstants.SPACE: return "SPACE:\t\"" + reader.getText() + "\""; default: return "EVENT:\t" + reader.getEventType(); } }
From source file:com.marklogic.contentpump.AggregateXMLReader.java
@Override public boolean nextKeyValue() throws IOException, InterruptedException { if (xmlSR == null) { hasNext = false;// w ww . jav a 2 s . c o m return false; } try { while (xmlSR.hasNext()) { int eventType; //getCharacterOffset() returns int; //int will overflows if file is larger than 2GB if (!overflow && xmlSR.getLocation().getCharacterOffset() < -1) { overflow = true; LOG.info("In progress..."); } //do not update pos if offset overflows if (!overflow) { pos = xmlSR.getLocation().getCharacterOffset(); } eventType = xmlSR.next(); switch (eventType) { case XMLStreamConstants.START_ELEMENT: if (startOfRecord) { // this is the start of the root, only copy // namespaces copyNameSpaceDecl(); startOfRecord = false; continue; } processStartElement(); break; case XMLStreamConstants.CHARACTERS: write(StringEscapeUtils.escapeXml(xmlSR.getText())); break; case XMLStreamConstants.CDATA: write("<![CDATA["); write(xmlSR.getText()); write("]]>"); break; case XMLStreamConstants.SPACE: write(xmlSR.getText()); break; case XMLStreamConstants.ENTITY_REFERENCE: write("&"); write(xmlSR.getLocalName()); write(";"); break; case XMLStreamConstants.DTD: write("<!DOCTYPE"); write(xmlSR.getText()); write(">"); break; case XMLStreamConstants.PROCESSING_INSTRUCTION: write("<?"); write(xmlSR.getPIData()); write("?>"); break; case XMLStreamConstants.COMMENT: write("<!--"); write(xmlSR.getText()); write("-->"); break; case XMLStreamConstants.END_ELEMENT: keepGoing = processEndElement(); if (!keepGoing) { keepGoing = true; return true; } break; case XMLStreamConstants.START_DOCUMENT: throw new XMLStreamException("unexpected start of document within record!\n" + "recordName = " + recordName + ", recordNamespace = " + recordNamespace + " at " + xmlSR.getLocation()); case XMLStreamConstants.END_DOCUMENT: if (currentId != null) { throw new XMLStreamException( "end of document before end of current record!\n" + "recordName = " + recordName + ", recordNamespace = " + recordNamespace + " at " + xmlSR.getLocation()); } else { if (compressed) { //this doc is done, refer to the zip for next doc hasNext = false; return false; } else { //get next file from FileIterator if (iterator != null && iterator.hasNext()) { close(); initStreamReader(iterator.next()); continue; } else { hasNext = false; return false; } } } default: throw new XMLStreamException("UNIMPLEMENTED: " + eventType); } } } catch (XMLStreamException e) { LOG.error("Parsing error", e); throw new IOException("Parsing error", e); } return false; }
From source file:com.clustercontrol.agent.winevent.WinEventMonitor.java
/** * XMLStAX???EventLogRecord????//from www . j a va 2s.c o m * @param eventXmlStream * @return EventLogRecord? */ private ArrayList<EventLogRecord> parseEventXML(InputStream eventXmlStream) { ArrayList<EventLogRecord> eventlogs = new ArrayList<EventLogRecord>(); try { XMLInputFactory xmlif = XMLInputFactory.newInstance(); /** * OpenJDK7/OracleJDK7??"]"?2????????????????????????????? * ?XML?????????OpenJDK7/OracleJDK7???????/?????????? * URL??????????????? * * URL * http://docs.oracle.com/javase/jp/6/api/javax/xml/stream/XMLStreamReader.html#next() */ String xmlCoalescingKey = "javax.xml.stream.isCoalescing";// TODO JRE??????????????????? if (m_log.isDebugEnabled()) { m_log.debug(xmlCoalescingKey + " = true"); } xmlif.setProperty(xmlCoalescingKey, true); XMLStreamReader xmlr = xmlif.createXMLStreamReader(eventXmlStream); while (xmlr.hasNext()) { switch (xmlr.getEventType()) { case XMLStreamConstants.START_ELEMENT: m_log.trace("EventType : XMLStreamConstants.START_ELEMENT"); String localName = xmlr.getLocalName(); m_log.trace("local name : " + localName); if ("Event".equals(localName)) { EventLogRecord eventlog = new EventLogRecord(); eventlogs.add(eventlog); m_log.debug("create new EventLogRecord"); } else { String attrLocalName = null; String attrValue = null; if (xmlr.getAttributeCount() != 0) { attrLocalName = xmlr.getAttributeLocalName(0); attrValue = xmlr.getAttributeValue(0); m_log.trace("attribute local name : " + attrLocalName); m_log.trace("attribute local value : " + attrValue); } if ("Provider".equals(localName)) { if ("Name".equals(attrLocalName)) { m_log.trace("target value : " + attrValue); EventLogRecord eventlog = eventlogs.get(eventlogs.size() - 1); eventlog.setProviderName(attrValue); m_log.debug("set ProviderName : " + eventlog.getProviderName()); } } // Get-WinEvent/wevtutil.exe else if ("TimeCreated".equals(localName) && "SystemTime".equals(attrLocalName)) { m_log.trace("target value : " + attrValue); // "yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'"???S???????????? String formatedDateString = attrValue.replaceAll("\\..*Z", ""); m_log.trace("formatted target value : " + formatedDateString); DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("UTC")); EventLogRecord eventlog = eventlogs.get(eventlogs.size() - 1); ; try { eventlog.setTimeCreated(sdf.parse(formatedDateString)); } catch (ParseException e) { // do nothing m_log.error("set TimeCreated Error", e); } m_log.debug("set TimeCreated : " + eventlog.getTimeCreated()); } // Get-EventLog if ("TimeGenerated".equals(localName) && "SystemTime".equals(attrLocalName)) { m_log.trace("target value : " + attrValue); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss'Z'"); sdf.setTimeZone(HinemosTime.getTimeZone()); EventLogRecord eventlog = eventlogs.get(eventlogs.size() - 1); ; try { eventlog.setTimeCreated(sdf.parse(attrValue)); } catch (ParseException e) { // do nothing m_log.error("set TimeCreated Error", e); } m_log.debug("set TimeCreated : " + eventlog.getTimeCreated()); } else { targetProperty = localName; m_log.trace("target property : " + targetProperty); } } break; case XMLStreamConstants.SPACE: case XMLStreamConstants.CHARACTERS: m_log.trace("EventType : XMLStreamConstants.CHARACTERS, length=" + xmlr.getTextLength()); if (targetProperty != null) { try { EventLogRecord eventlog = eventlogs.get(eventlogs.size() - 1); ; if ("EventID".equals(targetProperty)) { eventlog.setId(Integer.parseInt(new String(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength()))); m_log.debug("set EventID : " + eventlog.getId()); } // Get-WinEvent/wevtutil.exe else if ("Level".equals(targetProperty)) { if (eventlog.getLevel() == WinEventConstant.UNDEFINED) { eventlog.setLevel(Integer.parseInt(new String(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength()))); m_log.debug("set Level : " + eventlog.getLevel()); } } else if ("Task".equals(targetProperty)) { if (eventlog.getTask() == WinEventConstant.UNDEFINED) { eventlog.setTask(Integer.parseInt(new String(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength()))); m_log.debug("set Task : " + eventlog.getTask()); } } else if ("Keywords".equals(targetProperty)) { // TODO ????????0x8080000000000000 //eventlog.setKeywords(Long.decode(new String(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength()))); //m_log.debug("set Keywords : " + eventlog.getKeywords()); } else if ("EventRecordId".equals(targetProperty)) { eventlog.setRecordId(Long.parseLong(new String(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength()))); m_log.debug("set RecordId : " + eventlog.getRecordId()); } else if ("Channel".equals(targetProperty)) { eventlog.setLogName(new String(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength())); m_log.debug("set LogName : " + eventlog.getLogName()); } else if ("Computer".equals(targetProperty)) { eventlog.setMachineName(new String(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength())); m_log.debug("set MachineName : " + eventlog.getMachineName()); } else if ("Message".equals(targetProperty)) { String message = new String(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength()); message = message.replaceAll(tmpReturnCode, "\r\n"); message = message.replaceAll(tmpLtCode, "<"); message = message.replaceAll(tmpGtCode, ">"); eventlog.setMessage(message); m_log.debug("set Message : " + eventlog.getMessage()); } else if ("Data".equals(targetProperty)) { String data = new String(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength()); eventlog.getData().add(data); m_log.debug("set Data : " + data); } else { m_log.debug("unknown target property : " + targetProperty); } } catch (NumberFormatException e) { m_log.debug("number parse error", e); } } targetProperty = null; break; default: // break; } xmlr.next(); } xmlr.close(); } catch (XMLStreamException e) { m_log.warn("parseEvent() xmlstream error", e); } return eventlogs; }