private static void processAlias( XMLStreamReader2 streamReader, AliasDefinitionsData data, MemoizedFile aliasDefinitionsFile, AssetLocation assetLocation) throws XMLStreamException, NamespaceException, RequirePathException, AliasException { String aliasName = streamReader.getAttributeValue(null, "name"); String aliasClass = streamReader.getAttributeValue(null, "defaultClass"); String aliasInterface = streamReader.getAttributeValue(null, "interface"); if (aliasName.equals(aliasClass)) { throw new AliasNameIsTheSameAsTheClassException(aliasDefinitionsFile, aliasName); } assetLocation.assertIdentifierCorrectlyNamespaced(aliasName); data.aliasDefinitions.add(new AliasDefinition(aliasName, aliasClass, aliasInterface)); XmlStreamCursor cursor = new XmlStreamCursor(streamReader); while (cursor.isWithinInitialNode()) { if (streamReader.getEventType() == XMLStreamReader.START_ELEMENT) { switch (streamReader.getLocalName()) { case "scenario": processScenario(aliasName, streamReader, data); break; } } cursor.nextTag(); } }
private static void processScenario( String aliasName, XMLStreamReader2 streamReader, AliasDefinitionsData data) { String scenarioName = streamReader.getAttributeValue(null, "name"); String aliasClass = streamReader.getAttributeValue(null, "class"); AliasOverride scenarioAlias = new AliasOverride(aliasName, aliasClass); data.getScenarioAliases(aliasName).put(scenarioName, scenarioAlias); }
private void processAliases(XMLStreamReader2 streamReader) { aliasesData.scenario = streamReader.getAttributeValue(null, "useScenario"); String useGroups = streamReader.getAttributeValue(null, "useGroups"); if (useGroups != null) { aliasesData.groupNames = Arrays.asList(useGroups.split(" ")); } }
private static void processGroupAlias( String groupName, XMLStreamReader2 streamReader, AliasDefinitionsData data) { String aliasName = streamReader.getAttributeValue(null, "name"); String aliasClass = streamReader.getAttributeValue(null, "class"); AliasOverride groupAlias = new AliasOverride(aliasName, aliasClass); data.getGroupAliases(groupName).add(groupAlias); }
private void processAlias(XMLStreamReader2 streamReader) throws IncompleteAliasException { String aliasName = streamReader.getAttributeValue(null, "name"); String aliasClass = streamReader.getAttributeValue(null, "class"); if ((aliasClass == null) || (aliasClass.equals(""))) { throw new IncompleteAliasException(aliasesFile, aliasName); } aliasesData.aliasOverrides.add(new AliasOverride(aliasName, aliasClass)); }
String getAttributes(XMLStreamReader2 xmlr) { List<String> attributes = new ArrayList<String>(); for (int i = 0; i < xmlr.getAttributeCount(); i++) { attributes.add(xmlr.getAttributeLocalName(i) + ":" + xmlr.getAttributeValue(i)); } return StringUtils.join(attributes, ", "); }
public MetadataRecord fromXml(String recordContents) throws XMLStreamException { String recordString = createCompleteRecordString(recordContents); try { Reader reader = new StringReader(recordString); XMLStreamReader2 input = (XMLStreamReader2) inputFactory.createXMLStreamReader(reader); GroovyNode rootNode = null, node = null; StringBuilder value = new StringBuilder(); while (true) { switch (input.getEventType()) { case XMLEvent.START_DOCUMENT: break; case XMLEvent.START_ELEMENT: if (node == null) { rootNode = node = new GroovyNode(null, "input"); } else { node = new GroovyNode( node, input.getNamespaceURI(), input.getLocalName(), input.getPrefix()); } if (input.getAttributeCount() > 0) { for (int walk = 0; walk < input.getAttributeCount(); walk++) { QName attributeName = input.getAttributeName(walk); node.attributes() .put(attributeName.getLocalPart(), input.getAttributeValue(walk)); } } value.setLength(0); break; case XMLEvent.CHARACTERS: case XMLEvent.CDATA: value.append(input.getText()); break; case XMLEvent.END_ELEMENT: if (node == null) { throw new RuntimeException("Node cannot be null"); } String valueString = sanitize(value.toString()); value.setLength(0); if (valueString.length() > 0) { node.setValue(valueString); } node = node.parent(); break; case XMLEvent.END_DOCUMENT: { break; } } if (!input.hasNext()) { break; } input.next(); } return new MetadataRecord(rootNode, -1, -1); } catch (WstxParsingException e) { throw new XMLStreamException("Problem parsing record:\n" + recordString, e); } }
public void testAttributeDefault() throws Exception { XMLValidationSchema schema = parseW3CSchema(SCHEMA_WITH_DEFAULTS); XMLStreamReader2 sr = getReader("<price currency='FIM'>129</price>"); sr.validateAgainst(schema); // first: if explicitly defined, should show that value assertTokenType(START_ELEMENT, sr.next()); assertEquals("price", sr.getLocalName()); assertEquals(1, sr.getAttributeCount()); assertEquals("FIM", sr.getAttributeValue(null, "currency")); sr.close(); // then, if missing, default to given default sr = getReader("<price>129</price>"); sr.validateAgainst(schema); // first: if explicitly defined, should show that value assertTokenType(START_ELEMENT, sr.next()); assertEquals("price", sr.getLocalName()); assertEquals(1, sr.getAttributeCount()); assertEquals("USD", sr.getAttributeValue(null, "currency")); sr.close(); }
private static void processGroup( XMLStreamReader2 streamReader, AliasDefinitionsData data, AssetLocation assetLocation) throws XMLStreamException, NamespaceException, RequirePathException { String groupName = streamReader.getAttributeValue(null, "name"); assetLocation.assertIdentifierCorrectlyNamespaced(groupName); XmlStreamCursor cursor = new XmlStreamCursor(streamReader); while (cursor.isWithinInitialNode()) { if (streamReader.getEventType() == XMLStreamReader.START_ELEMENT) { switch (streamReader.getLocalName()) { case "alias": processGroupAlias(groupName, streamReader, data); break; } } cursor.nextTag(); } }
public MetadataRecord metadataRecordFrom(String recordContents) throws XMLStreamException { try { Reader reader = new StringReader(recordContents); XMLStreamReader2 input = (XMLStreamReader2) inputFactory.createXMLStreamReader(reader); GroovyNode rootNode = null, node = null; StringBuilder value = new StringBuilder(); while (true) { switch (input.getEventType()) { case XMLEvent.START_DOCUMENT: break; case XMLEvent.START_ELEMENT: node = new GroovyNode( node, input.getNamespaceURI(), input.getLocalName(), input.getPrefix()); if (rootNode == null) { rootNode = node; } if (input.getAttributeCount() > 0) { for (int walk = 0; walk < input.getAttributeCount(); walk++) { QName attributeName = input.getAttributeName(walk); if (attributeName.getPrefix() == null || attributeName.getPrefix().isEmpty()) { node.attributes() .put(attributeName.getLocalPart(), input.getAttributeValue(walk)); } else { node.attributes() .put( String.format( "%s:%s", attributeName.getPrefix(), attributeName.getLocalPart()), input.getAttributeValue(walk)); } } } value.setLength(0); break; case XMLEvent.CHARACTERS: value.append(input.getText()); break; case XMLEvent.CDATA: value.append(String.format("<![CDATA[%s]]>", input.getText())); break; case XMLEvent.END_ELEMENT: if (node == null) throw new RuntimeException("Node cannot be null"); String valueString = value.toString().trim(); value.setLength(0); if (valueString.length() > 0) node.setNodeValue(valueString); node = node.parent(); break; case XMLEvent.END_DOCUMENT: { break; } } if (!input.hasNext()) { break; } input.next(); } return MetadataRecord.create(rootNode, -1, -1); } catch (WstxParsingException e) { throw new XMLStreamException("Problem parsing record:\n" + recordContents, e); } }