List of usage examples for javax.xml.stream XMLStreamReader getAttributeCount
public int getAttributeCount();
From source file:net.xy.jcms.controller.configurations.parser.TranslationParser.java
/** * checks for parameters <parameter name="contentgroup" group="1" * convert="de.jamba.ContentGroupConverter"/> * /* w ww .ja v a 2s . c o m*/ * @param parser * @return value * @throws XMLStreamException * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException */ private static List<RuleParameter> parseParameter(final XMLStreamReader parser, final ClassLoader loader) throws XMLStreamException, ClassNotFoundException { final List<RuleParameter> params = new ArrayList<RuleParameter>(); while (parser.nextTag() == XMLStreamConstants.START_ELEMENT) { String parameterName = null, converter = null; Integer aplicatesToGroup = null; if (parser.getAttributeCount() != 3 && parser.getAttributeCount() != 2) { throw new IllegalArgumentException("There are to much or few attributes specified for parameter."); } for (int i = 0; i < parser.getAttributeCount(); i++) { if (parser.getAttributeLocalName(i).equals("name")) { parameterName = parser.getAttributeValue(i); } else if (parser.getAttributeLocalName(i).equals("convert")) { converter = parser.getAttributeValue(i); } else if (parser.getAttributeLocalName(i).equals("group")) { aplicatesToGroup = new Integer(parser.getAttributeValue(i)); } } boolean goEnd = true; Map<String, String> mappings = null; if (parser.next() == XMLStreamConstants.CHARACTERS) { final String mappingStr = parser.getText(); // get integrated mapping body if (StringUtils.isNotBlank(mappingStr)) { mappings = new HashMap<String, String>(); final String[] lines = mappingStr.split("\n"); for (String line : lines) { line = line.trim(); if (StringUtils.isBlank(line) || line.startsWith("#")) { continue; } final String[] pair = line.split("=", 2); mappings.put(pair[0].trim(), pair[1].trim()); } } } else { goEnd = false; // allready on end } if (StringUtils.isBlank(parameterName)) { throw new IllegalArgumentException("Parameter name has to be set"); } else if (StringUtils.isBlank(converter)) { throw new IllegalArgumentException("Parameter Converter has to be set"); } else if (aplicatesToGroup == null) { throw new IllegalArgumentException("Applicates to regex group has to be set"); } else { if (mappings != null) { // get special mapping converter params.add(new RuleParameter(parameterName, aplicatesToGroup, ((InitializableController<?>) ConverterPool.get(converter, loader)) .initialize(mappings))); } else { // get normal converter params.add(new RuleParameter(parameterName, aplicatesToGroup, ConverterPool.get(converter, loader))); } } if (goEnd) { parser.nextTag(); // gets endtag } } return params; }
From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java
/** * parse the usecase subsections// w w w . j a v a 2 s. c o m * * @param parser * @return * @throws XMLStreamException * @throws ClassNotFoundException */ private static Usecase parseUsecase(final XMLStreamReader parser, final ClassLoader loader) throws XMLStreamException, ClassNotFoundException { if (parser.getAttributeCount() != 1) { throw new IllegalArgumentException( "There are to much or few attributes specified for usecase. [" + parser.getLocation() + "]"); } final String id = parser.getAttributeValue(0); String description = null; Parameter[] parameterList = {}; Controller[] controllerList = {}; Configuration<?>[] configurationList = {}; while (parser.nextTag() == XMLStreamConstants.START_ELEMENT) { if (parser.getLocalName().equals("description")) { description = parseDescription(parser); } else if (parser.getLocalName().equals("parameter")) { parameterList = parseParameters(parser); } else if (parser.getLocalName().equals("controller")) { controllerList = parseControllers(parser, loader); } else if (parser.getLocalName().equals("configurations")) { configurationList = parseConfigurations(parser, loader); } else { throw new IllegalArgumentException( "Syntax error nothing allowed between Usecase sections. [" + parser.getLocation() + "]"); } } if (StringUtils.isBlank(description) || description.length() < 32) { throw new IllegalArgumentException( "Description is empty or insufficient please add more details. [" + parser.getLocation() + "]"); } return new Usecase(id, description, parameterList, controllerList, configurationList); }
From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java
/** * parses an parameter deffinition/*from w w w . j av a2 s .c o m*/ * * @param parser * @return * @throws XMLStreamException */ private static Parameter parseParameter(final XMLStreamReader parser) throws XMLStreamException { if (parser.getAttributeCount() != 2) { throw new IllegalArgumentException( "There are to much or few attributes specified for param. [" + parser.getLocation() + "]"); } String key = null, valueType = null; for (int i = 0; i < 2; i++) { if (parser.getAttributeLocalName(i).equals("key")) { key = parser.getAttributeValue(i); } else if (parser.getAttributeLocalName(i).equals("valueType")) { valueType = parser.getAttributeValue(i); } } if (StringUtils.isBlank(key)) { throw new IllegalArgumentException("Param key name is missing [" + parser.getLocation() + "]"); } else if (StringUtils.isBlank(valueType)) { throw new IllegalArgumentException("Param value type is missing [" + parser.getLocation() + "]"); } parser.nextTag(); return new Parameter(key, valueType); }
From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java
/** * parses the controller itself/*from www . j a v a 2s . c o m*/ * * @param parser * @return * @throws XMLStreamException * @throws ClassNotFoundException */ private static Controller parseController(final XMLStreamReader parser, final ClassLoader loader) throws XMLStreamException, ClassNotFoundException { if (parser.getAttributeCount() != 2 && parser.getAttributeCount() != 1) { throw new IllegalArgumentException( "There are to much or few attributes specified for class. [" + parser.getLocation() + "]"); } String path = null; final EnumSet<ConfigurationType> obmitedConfigurations = EnumSet.noneOf(ConfigurationType.class); for (int i = 0; i < parser.getAttributeCount(); i++) { if (parser.getAttributeLocalName(i).equals("path")) { path = parser.getAttributeValue(i); } else if (parser.getAttributeLocalName(i).equals("obmitConfig")) { final String[] configs = parser.getAttributeValue(i).split(","); for (final String config : configs) { obmitedConfigurations.add(ConfigurationType.valueOf(config.trim())); } } } if (StringUtils.isBlank(path)) { throw new IllegalArgumentException("Classpath must be set [" + parser.getLocation() + "]"); } parser.nextTag(); return new Controller(ControllerPool.get(path, loader), obmitedConfigurations); }
From source file:net.xy.jcms.controller.configurations.parser.UsecaseParser.java
/** * parses the configuration itself/*from w w w. j a v a2 s . co m*/ * * @param parser * @return * @throws XMLStreamException */ private static Configuration<?> parseConfiguration(final XMLStreamReader parser, final ClassLoader loader) throws XMLStreamException { Configuration<?> config = null; if (parser.getAttributeCount() != 1 && parser.getAttributeCount() != 2) { throw new IllegalArgumentException("There are to much or few attributes specified for configuration. [" + parser.getLocation() + "]"); } ConfigurationType type = null; String include = null; for (int i = 0; i < parser.getAttributeCount(); i++) { if (parser.getAttributeLocalName(i).equals("type")) { type = ConfigurationType.valueOf(parser.getAttributeValue(i).trim()); } else if (parser.getAttributeLocalName(i).equals("include")) { include = parser.getAttributeValue(i); } } if (type == null) { throw new IllegalArgumentException("Configuration type must be set [" + parser.getLocation() + "]"); } if (include != null) { config = Configuration.initConfigurationByInclude(type, include, loader); } else { config = getConfigurationByBody(type, parser, loader); } if (config == null) { throw new IllegalArgumentException( "Configuration could not be retrieved [" + parser.getLocation() + "]"); } parser.nextTag(); return config; }
From source file:nl.knaw.huygens.tei.xpath.XPathUtil.java
public static Map<String, String> getNamespaceInfo(String xml) { Map<String, String> namespaces = Maps.newIdentityHashMap(); XMLInputFactory inputFactory = XMLInputFactory.newInstance(); try {/* www . j a v a 2 s . c o m*/ XMLStreamReader xreader = inputFactory.createXMLStreamReader(IOUtils.toInputStream(xml, "UTF-8")); while (xreader.hasNext()) { if (xreader.next() == XMLStreamConstants.START_ELEMENT) { QName qName = xreader.getName(); if (qName != null) { addNamespace(namespaces, qName.getPrefix(), qName.getNamespaceURI()); for (int i = 0; i < xreader.getAttributeCount(); i++) { addNamespace(namespaces, xreader.getAttributePrefix(i), xreader.getAttributeNamespace(i)); } } } } } catch (XMLStreamException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return namespaces; }
From source file:org.activiti.bpmn.converter.parser.DefinitionsParser.java
@SuppressWarnings("unchecked") public void parse(XMLStreamReader xtr, BpmnModel model) throws Exception { model.setTargetNamespace(xtr.getAttributeValue(null, TARGET_NAMESPACE_ATTRIBUTE)); for (int i = 0; i < xtr.getNamespaceCount(); i++) { String prefix = xtr.getNamespacePrefix(i); if (StringUtils.isNotEmpty(prefix)) { model.addNamespace(prefix, xtr.getNamespaceURI(i)); }//from ww w. j a v a 2 s .co m } for (int i = 0; i < xtr.getAttributeCount(); i++) { ExtensionAttribute extensionAttribute = new ExtensionAttribute(); extensionAttribute.setName(xtr.getAttributeLocalName(i)); extensionAttribute.setValue(xtr.getAttributeValue(i)); if (StringUtils.isNotEmpty(xtr.getAttributeNamespace(i))) { extensionAttribute.setNamespace(xtr.getAttributeNamespace(i)); } if (StringUtils.isNotEmpty(xtr.getAttributePrefix(i))) { extensionAttribute.setNamespacePrefix(xtr.getAttributePrefix(i)); } if (!BpmnXMLUtil.isBlacklisted(extensionAttribute, defaultAttributes)) { model.addDefinitionsAttribute(extensionAttribute); } } }
From source file:org.activiti.dmn.converter.util.DmnXMLUtil.java
public static DmnExtensionElement parseExtensionElement(XMLStreamReader xtr) throws Exception { DmnExtensionElement extensionElement = new DmnExtensionElement(); extensionElement.setName(xtr.getLocalName()); if (StringUtils.isNotEmpty(xtr.getNamespaceURI())) { extensionElement.setNamespace(xtr.getNamespaceURI()); }/*from w w w. j a v a 2 s . co m*/ if (StringUtils.isNotEmpty(xtr.getPrefix())) { extensionElement.setNamespacePrefix(xtr.getPrefix()); } for (int i = 0; i < xtr.getAttributeCount(); i++) { DmnExtensionAttribute extensionAttribute = new DmnExtensionAttribute(); extensionAttribute.setName(xtr.getAttributeLocalName(i)); extensionAttribute.setValue(xtr.getAttributeValue(i)); if (StringUtils.isNotEmpty(xtr.getAttributeNamespace(i))) { extensionAttribute.setNamespace(xtr.getAttributeNamespace(i)); } if (StringUtils.isNotEmpty(xtr.getAttributePrefix(i))) { extensionAttribute.setNamespacePrefix(xtr.getAttributePrefix(i)); } extensionElement.addAttribute(extensionAttribute); } boolean readyWithExtensionElement = false; while (readyWithExtensionElement == false && xtr.hasNext()) { xtr.next(); if (xtr.isCharacters() || XMLStreamReader.CDATA == xtr.getEventType()) { if (StringUtils.isNotEmpty(xtr.getText().trim())) { extensionElement.setElementText(xtr.getText().trim()); } } else if (xtr.isStartElement()) { DmnExtensionElement childExtensionElement = parseExtensionElement(xtr); extensionElement.addChildElement(childExtensionElement); } else if (xtr.isEndElement() && extensionElement.getName().equalsIgnoreCase(xtr.getLocalName())) { readyWithExtensionElement = true; } } return extensionElement; }
From source file:org.apache.axiom.om.impl.serialize.StreamingOMSerializer.java
/** * @param reader/*from ww w . j a v a 2s . co m*/ * @param writer * @throws XMLStreamException */ protected void serializeElement(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { // Note: To serialize the start tag, we must follow the order dictated by the JSR-173 (StAX) specification. // Please keep this code in sync with the code in OMSerializerUtil.serializeStartpart // The algorithm is: // ... generate writeStartElement // // ... generate setPrefix/setDefaultNamespace for each namespace declaration if the prefix is unassociated. // ... generate setPrefix/setDefaultNamespace if the prefix of the element is unassociated // ... generate setPrefix/setDefaultNamespace for each unassociated prefix of the attributes. // // ... generate writeNamespace/writerDefaultNamespace for the new namespace declarations determine during the "set" processing // ... generate writeAttribute for each attribute ArrayList writePrefixList = null; ArrayList writeNSList = null; // Get the prefix and namespace of the element. "" and null are identical. String ePrefix = reader.getPrefix(); ePrefix = (ePrefix != null && ePrefix.length() == 0) ? null : ePrefix; String eNamespace = reader.getNamespaceURI(); eNamespace = (eNamespace != null && eNamespace.length() == 0) ? null : eNamespace; // Write the startElement if required if (eNamespace != null) { if (ePrefix == null) { if (!OMSerializerUtil.isAssociated("", eNamespace, writer)) { if (writePrefixList == null) { writePrefixList = new ArrayList(); writeNSList = new ArrayList(); } writePrefixList.add(""); writeNSList.add(eNamespace); } writer.writeStartElement("", reader.getLocalName(), eNamespace); } else { if (!OMSerializerUtil.isAssociated(ePrefix, eNamespace, writer)) { if (writePrefixList == null) { writePrefixList = new ArrayList(); writeNSList = new ArrayList(); } writePrefixList.add(ePrefix); writeNSList.add(eNamespace); } writer.writeStartElement(ePrefix, reader.getLocalName(), eNamespace); } } else { writer.writeStartElement(reader.getLocalName()); } // Generate setPrefix for the namespace declarations int count = reader.getNamespaceCount(); for (int i = 0; i < count; i++) { String prefix = reader.getNamespacePrefix(i); prefix = (prefix != null && prefix.length() == 0) ? null : prefix; String namespace = reader.getNamespaceURI(i); namespace = (namespace != null && namespace.length() == 0) ? null : namespace; String newPrefix = OMSerializerUtil.generateSetPrefix(prefix, namespace, writer, false); // If this is a new association, remember it so that it can written out later if (newPrefix != null) { if (writePrefixList == null) { writePrefixList = new ArrayList(); writeNSList = new ArrayList(); } if (!writePrefixList.contains(newPrefix)) { writePrefixList.add(newPrefix); writeNSList.add(namespace); } } } // Generate setPrefix for the element // If the prefix is not associated with a namespace yet, remember it so that we can // write out a namespace declaration String newPrefix = OMSerializerUtil.generateSetPrefix(ePrefix, eNamespace, writer, false); // If this is a new association, remember it so that it can written out later if (newPrefix != null) { if (writePrefixList == null) { writePrefixList = new ArrayList(); writeNSList = new ArrayList(); } if (!writePrefixList.contains(newPrefix)) { writePrefixList.add(newPrefix); writeNSList.add(eNamespace); } } // Now Generate setPrefix for each attribute count = reader.getAttributeCount(); for (int i = 0; i < count; i++) { String prefix = reader.getAttributePrefix(i); prefix = (prefix != null && prefix.length() == 0) ? null : prefix; String namespace = reader.getAttributeNamespace(i); namespace = (namespace != null && namespace.length() == 0) ? null : namespace; // Default prefix referencing is not allowed on an attribute if (prefix == null && namespace != null) { String writerPrefix = writer.getPrefix(namespace); writerPrefix = (writerPrefix != null && writerPrefix.length() == 0) ? null : writerPrefix; prefix = (writerPrefix != null) ? writerPrefix : generateUniquePrefix(writer.getNamespaceContext()); } newPrefix = OMSerializerUtil.generateSetPrefix(prefix, namespace, writer, true); // If the prefix is not associated with a namespace yet, remember it so that we can // write out a namespace declaration if (newPrefix != null) { if (writePrefixList == null) { writePrefixList = new ArrayList(); writeNSList = new ArrayList(); } if (!writePrefixList.contains(newPrefix)) { writePrefixList.add(newPrefix); writeNSList.add(namespace); } } } // Now Generate setPrefix for each prefix referenced in an xsi:type // For example xsi:type="p:dataType" // The following code will make sure that setPrefix is called for "p". count = reader.getAttributeCount(); for (int i = 0; i < count; i++) { String prefix = reader.getAttributePrefix(i); prefix = (prefix != null && prefix.length() == 0) ? null : prefix; String namespace = reader.getAttributeNamespace(i); namespace = (namespace != null && namespace.length() == 0) ? null : namespace; String localName = reader.getAttributeLocalName(i); if (XSI_URI.equals(namespace) && XSI_LOCAL_NAME.equals(localName)) { String value = reader.getAttributeValue(i); if (DEBUG_ENABLED) { log.debug("The value of xsi:type is " + value); } if (value != null) { value = value.trim(); if (value.indexOf(":") > 0) { String refPrefix = value.substring(0, value.indexOf(":")); String refNamespace = reader.getNamespaceURI(refPrefix); if (refNamespace != null && refNamespace.length() > 0) { newPrefix = OMSerializerUtil.generateSetPrefix(refPrefix, refNamespace, writer, true); // If the prefix is not associated with a namespace yet, remember it so that we can // write out a namespace declaration if (newPrefix != null) { if (DEBUG_ENABLED) { log.debug( "An xmlns:" + newPrefix + "=\"" + refNamespace + "\" will be written"); } if (writePrefixList == null) { writePrefixList = new ArrayList(); writeNSList = new ArrayList(); } if (!writePrefixList.contains(newPrefix)) { writePrefixList.add(newPrefix); writeNSList.add(refNamespace); } } } } } } } // Now write out the list of namespace declarations in this list that we constructed // while doing the "set" processing. if (writePrefixList != null) { for (int i = 0; i < writePrefixList.size(); i++) { String prefix = (String) writePrefixList.get(i); String namespace = (String) writeNSList.get(i); if (prefix != null) { if (namespace == null) { writer.writeNamespace(prefix, ""); } else { writer.writeNamespace(prefix, namespace); } } else { writer.writeDefaultNamespace(namespace); } } } // Now write the attributes count = reader.getAttributeCount(); for (int i = 0; i < count; i++) { String prefix = reader.getAttributePrefix(i); prefix = (prefix != null && prefix.length() == 0) ? null : prefix; String namespace = reader.getAttributeNamespace(i); namespace = (namespace != null && namespace.length() == 0) ? null : namespace; if (prefix == null && namespace != null) { // Default namespaces are not allowed on an attribute reference. // Earlier in this code, a unique prefix was added for this case...now obtain and use it prefix = writer.getPrefix(namespace); //XMLStreamWriter doesn't allow for getPrefix to know whether you're asking for the prefix //for an attribute or an element. So if the namespace matches the default namespace getPrefix will return //the empty string, as if it were an element, in all cases (even for attributes, and even if //there was a prefix specifically set up for this), which is not the desired behavior. //Since the interface is base java, we can't fix it where we need to (by adding an attr boolean to //XMLStreamWriter.getPrefix), so we hack it in here... if (prefix == null || "".equals(prefix)) { for (int j = 0; j < writePrefixList.size(); j++) { if (namespace.equals((String) writeNSList.get(j))) { prefix = (String) writePrefixList.get(j); } } } } else if (namespace != null && !prefix.equals("xml")) { // Use the writer's prefix if it is different, but if the writers // prefix is empty then do not replace because attributes do not // default to the default namespace like elements do. String writerPrefix = writer.getPrefix(namespace); if (!prefix.equals(writerPrefix) && !"".equals(writerPrefix)) { prefix = writerPrefix; } } if (namespace != null) { // Qualified attribute writer.writeAttribute(prefix, namespace, reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } else { // Unqualified attribute writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } } }
From source file:org.apache.axiom.om.impl.serialize.StreamingOMSerializer.java
/** * @param reader//from w ww . ja v a2s. c o m * @param writer * @throws XMLStreamException */ protected void serializeAttributes(XMLStreamReader reader, XMLStreamWriter writer) throws XMLStreamException { int count = reader.getAttributeCount(); String prefix = null; String namespaceName = null; String writerPrefix = null; for (int i = 0; i < count; i++) { prefix = reader.getAttributePrefix(i); namespaceName = reader.getAttributeNamespace(i); /* Some parser implementations return null for the unqualified namespace. But getPrefix(null) will throw an exception (according to the XMLStreamWriter javadoc. We guard against this by using "" for the unqualified namespace. */ namespaceName = (namespaceName == null) ? "" : namespaceName; // Using getNamespaceContext should be avoided when not necessary. // Some parser implementations construct a new NamespaceContext each time it is invoked. // writerPrefix = writer.getNamespaceContext().getPrefix(namespaceName); writerPrefix = writer.getPrefix(namespaceName); if (!"".equals(namespaceName)) { //prefix has already being declared but this particular attrib has a //no prefix attached. So use the prefix provided by the writer if (writerPrefix != null && (prefix == null || prefix.equals(""))) { writer.writeAttribute(writerPrefix, namespaceName, reader.getAttributeLocalName(i), reader.getAttributeValue(i)); //writer prefix is available but different from the current //prefix of the attrib. We should be decalring the new prefix //as a namespace declaration } else if (prefix != null && !"".equals(prefix) && !prefix.equals(writerPrefix)) { writer.writeNamespace(prefix, namespaceName); writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader.getAttributeValue(i)); //prefix is null (or empty), but the namespace name is valid! it has not //being written previously also. So we need to generate a prefix //here } else { prefix = generateUniquePrefix(writer.getNamespaceContext()); writer.writeNamespace(prefix, namespaceName); writer.writeAttribute(prefix, namespaceName, reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } } else { //empty namespace is equal to no namespace! writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i)); } } }