List of usage examples for javax.xml.stream XMLStreamConstants START_ELEMENT
int START_ELEMENT
To view the source code for javax.xml.stream XMLStreamConstants START_ELEMENT.
Click Source Link
From source file:com.sun.socialsite.pojos.PropDefinition.java
protected void init(PropDefinition profileDef, InputStream input) throws SocialSiteException { try {//from w w w . ja va 2 s . c o m XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader parser = factory.createXMLStreamReader(input); String ns = null; // TODO: namespace for ProfileDef // hold the current things we're working on Map<String, DisplaySectionDefinition> sdefs = new LinkedHashMap<String, DisplaySectionDefinition>(); Stack<PropertyDefinitionHolder> propertyHolderStack = new Stack<PropertyDefinitionHolder>(); List<AllowedValue> allowedValues = null; PropertyDefinition pdef = null; for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) { switch (event) { case XMLStreamConstants.START_ELEMENT: log.debug("START ELEMENT -- " + parser.getLocalName()); if ("display-section".equals(parser.getLocalName())) { propertyHolderStack.push(new DisplaySectionDefinition(parser.getAttributeValue(ns, "name"), parser.getAttributeValue(ns, "namekey"))); } else if ("property".equals(parser.getLocalName())) { PropertyDefinitionHolder holder = propertyHolderStack.peek(); pdef = new PropertyDefinition(holder.getBasePath(), parser.getAttributeValue(ns, "name"), parser.getAttributeValue(ns, "namekey"), parser.getAttributeValue(ns, "type")); } else if ("object".equals(parser.getLocalName())) { PropertyDefinitionHolder holder = propertyHolderStack.peek(); propertyHolderStack.push(new PropertyObjectDefinition(holder.getBasePath(), parser.getAttributeValue(ns, "name"), parser.getAttributeValue(ns, "namekey"))); } else if ("collection".equals(parser.getLocalName())) { PropertyDefinitionHolder holder = propertyHolderStack.peek(); propertyHolderStack.push(new PropertyObjectCollectionDefinition(holder.getBasePath(), parser.getAttributeValue(ns, "name"), parser.getAttributeValue(ns, "namekey"))); } else if ("allowed-values".equals(parser.getLocalName())) { allowedValues = new ArrayList<AllowedValue>(); } else if ("value".equals(parser.getLocalName())) { AllowedValue allowedValue = new AllowedValue(parser.getAttributeValue(ns, "name"), parser.getAttributeValue(ns, "namekey")); allowedValues.add(allowedValue); } else if ("default-value".equals(parser.getLocalName())) { pdef.setDefaultValue(parser.getText()); } break; case XMLStreamConstants.END_ELEMENT: log.debug("END ELEMENT -- " + parser.getLocalName()); if ("display-section".equals(parser.getLocalName())) { DisplaySectionDefinition sdef = (DisplaySectionDefinition) propertyHolderStack.pop(); sdefs.put(sdef.getName(), sdef); } else if ("property".equals(parser.getLocalName())) { PropertyDefinitionHolder holder = propertyHolderStack.peek(); holder.getPropertyDefinitions().add(pdef); propertyDefs.put(pdef.getName(), pdef); pdef = null; } else if ("object".equals(parser.getLocalName())) { PropertyObjectDefinition odef = (PropertyObjectDefinition) propertyHolderStack.pop(); PropertyDefinitionHolder holder = propertyHolderStack.peek(); holder.getPropertyObjectDefinitions().add(odef); // add to list of all property object defs propertyObjectDefs.put(odef.getName(), odef); odef = null; } else if ("collection".equals(parser.getLocalName())) { PropertyObjectCollectionDefinition cdef = (PropertyObjectCollectionDefinition) propertyHolderStack .pop(); PropertyDefinitionHolder holder = propertyHolderStack.peek(); holder.getPropertyObjectCollectionDefinitions().add(cdef); // add to list of all property object defs propertyObjectCollectionDefs.put(cdef.getName(), cdef); cdef = null; } else if ("allowed-values".equals(parser.getLocalName())) { pdef.setAllowedValues(allowedValues); allowedValues = null; } break; case XMLStreamConstants.CHARACTERS: break; case XMLStreamConstants.CDATA: break; } // end switch } // end while parser.close(); profileDef.sectionDefs = sdefs; } catch (Exception ex) { throw new SocialSiteException("ERROR parsing profile definitions", ex); } }
From source file:com.microsoft.alm.plugin.context.soap.CatalogServiceImpl.java
/** * Advances the {@link XMLStreamReader} until it has read the end of the * current element. Useful when an element is encountered while reading a * stream, and it should be skipped.// w ww . j a v a 2s . c om * * @param reader the stream reader to read from (not null). */ private static void readUntilElementEnd(final XMLStreamReader reader) throws XMLStreamException { int event = reader.getEventType(); /* * Start element depth at 1, increment when an element is started (not * including the element that we start with), decrement when an element * is ended, and when it goes to 0 we've read the end of the original * reader's element. */ int elementDepth = 1; boolean firstTime = true; while (true) { switch (event) { case XMLStreamConstants.START_ELEMENT: /* * Don't increment depth the first time through, because the * caller opened the element. */ if (firstTime) { firstTime = false; } else { elementDepth++; } break; case XMLStreamConstants.END_ELEMENT: elementDepth--; if (elementDepth < 1) { /* * We just read the end element for the original * element. */ return; } break; default: /* * Things like characters, comments, attributes, etc. Ignore * them all. */ } event = reader.next(); } }
From source file:de.uzk.hki.da.sb.SIPFactoryTest.java
/** * @param premis/* ww w. j a v a 2 s. com*/ * @param publicRights */ private boolean checkPremisFilePubStartDate(File premis, ContractRights rights) { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader streamReader; try { streamReader = inputFactory.createXMLStreamReader(new FileInputStream(premis)); while (streamReader.hasNext()) { int event = streamReader.next(); switch (event) { case XMLStreamConstants.START_ELEMENT: if (streamReader.getLocalName().equals("startDate")) { String startDate = streamReader.getElementText().substring(0, 10); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String publicDate = sdf.format(rights.getPublicRights().getStartDate()); if (startDate.trim().equals(publicDate.trim())) { return true; } } default: break; } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (XMLStreamException e) { e.printStackTrace(); } return false; }
From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.ddi.DDIFileReader.java
private void processDataDscr(XMLStreamReader xmlr, SDIOMetadata smd) throws XMLStreamException { int variableCounter = 0; for (int event = xmlr.next(); event != XMLStreamConstants.END_DOCUMENT; event = xmlr.next()) { if (event == XMLStreamConstants.START_ELEMENT) { if (xmlr.getLocalName().equals("var")) { processVar(xmlr, smd, variableCounter); variableCounter++;/* w ww . j a v a2 s . c om*/ } else { throw new XMLStreamException( "Unsupported element in the DDI: codeBook/dataDscr/" + xmlr.getLocalName()); } } else if (event == XMLStreamConstants.END_ELEMENT) { if (xmlr.getLocalName().equals("dataDscr")) { dbgLog.info("processed dataDscr section"); return; } else { throw new XMLStreamException( "Mismatched DDI Formatting: </dataDscr> expected, found " + xmlr.getLocalName()); } } } }
From source file:com.clustercontrol.agent.winevent.WinEventMonitor.java
/** * XMLStAX???EventLogRecord????/*from ww w.j a v a2 s. com*/ * @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; }
From source file:com.liferay.portal.util.LocalizationImpl.java
private void _copyNonExempt(XMLStreamReader xmlStreamReader, XMLStreamWriter xmlStreamWriter, String exemptLanguageId, String defaultLanguageId, boolean cdata) throws XMLStreamException { while (xmlStreamReader.hasNext()) { int event = xmlStreamReader.next(); if (event == XMLStreamConstants.START_ELEMENT) { String languageId = xmlStreamReader.getAttributeValue(null, _LANGUAGE_ID); if (Validator.isNull(languageId)) { languageId = defaultLanguageId; }//from ww w. j a va2 s. c o m if (!languageId.equals(exemptLanguageId)) { xmlStreamWriter.writeStartElement(xmlStreamReader.getLocalName()); xmlStreamWriter.writeAttribute(_LANGUAGE_ID, languageId); while (xmlStreamReader.hasNext()) { event = xmlStreamReader.next(); if ((event == XMLStreamConstants.CHARACTERS) || (event == XMLStreamConstants.CDATA)) { String text = xmlStreamReader.getText(); if (cdata) { xmlStreamWriter.writeCData(text); } else { xmlStreamWriter.writeCharacters(xmlStreamReader.getText()); } break; } else if (event == XMLStreamConstants.END_ELEMENT) { break; } } xmlStreamWriter.writeEndElement(); } } else if (event == XMLStreamConstants.END_DOCUMENT) { break; } } }
From source file:ddf.catalog.source.opensearch.OpenSearchSource.java
private InputTransformer getInputTransformer(InputStream inputStream) { XMLStreamReader xmlStreamReader = null; try {/*from w ww. j av a 2 s. co m*/ xmlStreamReader = xmlInputFactory.createXMLStreamReader(inputStream); while (xmlStreamReader.hasNext()) { int next = xmlStreamReader.next(); if (next == XMLStreamConstants.START_ELEMENT) { String namespaceUri = xmlStreamReader.getNamespaceURI(); InputTransformer transformerReference = lookupTransformerReference(namespaceUri); if (transformerReference != null) { return transformerReference; } } } } catch (XMLStreamException | InvalidSyntaxException e) { LOGGER.error("Failed to parse transformer namespace", e); } finally { try { if (xmlStreamReader != null) { xmlStreamReader.close(); } } catch (XMLStreamException e) { LOGGER.error("failed to close namespace reader", e); } } return null; }
From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.ddi.DDIFileReader.java
private void processVar(XMLStreamReader xmlr, SDIOMetadata smd, int variableNumber) throws XMLStreamException { // Attributes: // ID -- can be ignored (will be reassigned); // name:/*ww w .j a v a2 s. co m*/ String variableName = xmlr.getAttributeValue(null, "name"); if (variableName == null || variableName.equals("")) { throw new XMLStreamException("NULL or empty variable name attribute."); } variableNameList.add(variableName); // interval type: String varIntervalType = xmlr.getAttributeValue(null, "intrvl"); // OK if not specified; defaults to discrete: varIntervalType = (varIntervalType == null ? VAR_INTERVAL_DISCRETE : varIntervalType); // Number of decimal points: String dcmlAttr = xmlr.getAttributeValue(null, "dcml"); Long dcmlPoints = null; if (dcmlAttr != null && !dcmlAttr.equals("")) { try { dcmlPoints = new Long(dcmlAttr); } catch (NumberFormatException nfe) { throw new XMLStreamException("Invalid variable dcml attribute: " + dcmlAttr); } } // weighed variables not supported yet -- L.A. //dv.setWeighted( VAR_WEIGHTED.equals( xmlr.getAttributeValue(null, "wgt") ) ); // default is not-wgtd, so null sets weighted to false Map<String, String> valueLabelPairs = new LinkedHashMap<String, String>(); List<String> missingValues = new ArrayList<String>(); for (int event = xmlr.next(); event != XMLStreamConstants.END_DOCUMENT; event = xmlr.next()) { if (event == XMLStreamConstants.START_ELEMENT) { if (xmlr.getLocalName().equals("location")) { processLocation(xmlr, smd); } else if (xmlr.getLocalName().equals("labl")) { String _labl = processLabl(xmlr, LEVEL_VARIABLE); if (_labl != null && !_labl.equals("")) { variableLabelMap.put(variableName, _labl); } } else if (xmlr.getLocalName().equals("invalrng")) { processInvalrng(xmlr, smd, variableName); } else if (xmlr.getLocalName().equals("varFormat")) { int simpleVariableFormat = processVarFormat(xmlr, smd, variableName); variableTypeList.add(simpleVariableFormat); } else if (xmlr.getLocalName().equals("catgry")) { processCatgry(xmlr, valueLabelPairs, missingValues, variableName); } else if (xmlr.getLocalName().equals("universe")) { // Should not occur in TAB+DDI ingest (?) // ignore. } else if (xmlr.getLocalName().equals("concept")) { // Same deal. } else if (xmlr.getLocalName().equals("notes")) { // Same. } else if (xmlr.getLocalName().equals("sumStat")) { throw new XMLStreamException( "sumStat (Summary Statistics) section found in a variable section; not supported!"); } // the "todos" below are from DDIServiceBean -- L.A. // todo: qstnTxt: wait to handle until we know more of how we will use it // todo: wgt-var : waitng to see example } else if (event == XMLStreamConstants.END_ELEMENT) { if (xmlr.getLocalName().equals("var")) { // Before returning, set rich variable type metadata: if (variableTypeList.get(variableNumber) == 0) { // this is a numeric variable; // It could be discrete or continuous, with the number // of decimal points explicitely specified. unfVariableTypes.put(variableName, 0); // default printFormatList.add(5); formatCategoryTable.put(variableName, "other"); if ((dcmlPoints != null && dcmlPoints > new Long(0)) || (!varIntervalType.equals(VAR_INTERVAL_DISCRETE))) { unfVariableTypes.put(variableName, 1); decimalVariableSet.add(variableNumber); if (dcmlPoints == null || dcmlPoints == new Long(0)) { dcmlPoints = new Long(7); } printFormatNameTable.put(variableName, "F8." + dcmlPoints); } else { unfVariableTypes.put(variableName, 0); } } else { // this is a string variable. // (or Date; whih is stored as a string, for most purposes, // but requires some special treatment. unfVariableTypes.put(variableName, -1); printFormatList.add(1); if (!"date".equals(formatCategoryTable.get(variableName)) && !"time".equals(formatCategoryTable.get(variableName))) { formatCategoryTable.put(variableName, "other"); } } // Variable Label: // (if it's not supplied, we create an empty placeholder) if (variableLabelMap.get(variableName) == null || variableLabelMap.get(variableName).equals("")) { variableLabelMap.put(variableName, ""); } // Value Labels: if (!valueLabelPairs.isEmpty()) { valueLabelTable.put(variableName, valueLabelPairs); dbgLog.warning("valueLabelTable = " + valueLabelTable.toString()); dbgLog.warning("valueLabelTable = " + valueLabelTable); valueVariableMappingTable.put(variableName, variableName); } // Missing Values: if (!missingValues.isEmpty()) { missingValueTable.put(variableName, missingValues); } dbgLog.info("processed variable number " + variableNumber); return; } } } }
From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java
public static StartElement getPropertyStartTag(final XMLEventReader propReader, final String[] path) throws Exception { int pos = 0;//from www .j a va 2s.c om StartElement property = null; while (propReader.hasNext() && pos < path.length) { final XMLEvent event = propReader.nextEvent(); if (event.getEventType() == XMLStreamConstants.START_ELEMENT && (ATOM_PROPERTY_PREFIX + path[pos].trim()) .equals(event.asStartElement().getName().getLocalPart())) { pos++; if (path.length == pos) { property = event.asStartElement(); } } } if (property == null) { throw new NotFoundException(); } return property; }
From source file:com.hp.alm.ali.rest.client.AliRestClient.java
private List<String> getAttributeValues(InputStream xml, String elemName, String attrName) { try {/*from ww w. ja v a 2 s . c o m*/ XMLInputFactory factory = XmlUtils.createBasicInputFactory(); XMLStreamReader parser; parser = factory.createXMLStreamReader(xml); List<String> result = new LinkedList<String>(); while (true) { int event = parser.next(); if (event == XMLStreamConstants.END_DOCUMENT) { parser.close(); break; } if (event == XMLStreamConstants.START_ELEMENT && elemName.equals(parser.getLocalName())) { for (int i = 0; i < parser.getAttributeCount(); i++) { String localName = parser.getAttributeLocalName(i); if (attrName.equals(localName)) { result.add(parser.getAttributeValue(i)); break; } } } } return result; } catch (XMLStreamException e) { throw new RuntimeException(e); } finally { try { xml.close(); } catch (IOException e) { // ignore } } }