/** * Required element. * * @param tag required tag * @param xmlElement handler * @throws Exception exception */ public void required(@Nonnull final String tag, @Nonnull final XmlProcessor xmlElement) throws Exception { if (isEndTag()) { throw new Exception("found end tag but required tag [" + tag + "]"); } if (!matchesTag(tag)) { throw new Exception("Expected tag [" + tag + "] but was [" + xpp.getName() + "]"); } processElement(xmlElement); nextTag(); }
/** * One or more element. * * @param tag element tag * @param xmlElement handler * @throws Exception exception */ public void oneOrMore(@Nonnull final String tag, @Nonnull final XmlProcessor xmlElement) throws Exception { if (isEndTag()) { throw new Exception("End tag reached but was expecting [" + tag + "]"); } if (!matchesTag(tag)) { throw new Exception("Expected tag [" + tag + "] but was [" + xpp.getName() + "]"); } processElement(xmlElement); nextTag(); zeroOrMore(tag, xmlElement); }
@Nonnull public XmlType loadXml(@Nonnull final XmlParser parser) throws Exception { if (type == null) { throw new Exception("The type is null, something is wrong."); } Type t = getType(type); XmlProcessorType xmlType = t.createXmlProcessor(this); parser.nextTag(); if (root == null) { throw new Exception("Root element is not set."); } parser.required(root, xmlType); XmlType result = xmlType.getXmlType(); if (result == null) { throw new Exception("Failed to resolve XML data to a proper XML type."); } return result; }
/** * Optional element. * * @param tag required tag * @param xmlElement handler * @throws Exception exception */ public void optional(@Nonnull final String tag, @Nonnull final XmlProcessor xmlElement) throws Exception { if (isEndTag()) { return; } if (!matchesTag(tag)) { return; } processElement(xmlElement); nextTag(); }
@Override public void process(@Nonnull final XmlParser xmlParser, @Nonnull final Attributes attributes) throws Exception { SubstitutionGroup substitutionGroup = new SubstitutionGroup(); parent.addSubstitutionGroup(substitutionGroup); xmlParser.nextTag(); xmlParser.oneOrMore("element", new TypeProcessorElement(substitutionGroup.getHelperType())); } }
/** * Zero or more element. * * @param tag element tag * @param xmlElement handler * @throws Exception exception */ public void zeroOrMore(@Nonnull final String tag, @Nonnull final XmlProcessor xmlElement) throws Exception { if (isEndTag()) { return; } if (!matchesTag(tag)) { return; } processElement(xmlElement); nextTag(); zeroOrMore(tag, xmlElement); }
/** * SubstitionGroup Support. * * @param substGroup SubstitutionGroup * @throws Exception exception */ public void zeroOrMore(@Nonnull final SubstitutionGroup substGroup) throws Exception { if (isEndTag()) { return; } XmlProcessor element = substGroup.matches(xpp.getName()); if (element == null) { return; } processElement(element); nextTag(); zeroOrMore(substGroup); }
@Override public void process( @Nonnull final XmlParser xmlParser, @Nonnull final Attributes attributes) throws Exception { String filename = attributes.get("filename"); if (filename == null) { return; } Schema niftyXmlSchema = new Schema(parserFactory, resourceLoader); XmlParser parser = new XmlParser(parserFactory.newPullParser()); InputStream stream = resourceLoader.getResourceAsStream(filename); if (stream != null) { try { parser.read(stream); parser.nextTag(); parser.required("nxs", niftyXmlSchema); types.putAll(niftyXmlSchema.getTypes()); xmlParser.nextTag(); } finally { try { stream.close(); } catch (IOException ignored) {} } } } }
@Override public void process(@Nonnull final XmlParser xmlParser, @Nonnull final Attributes attributes) throws Exception { String name = attributes.get("name"); if (name == null) { throw new Exception("[name] attribute is a required attribute"); } String type = attributes.get("type"); if (type == null) { throw new Exception("[type] attribute is a required attribute"); } OccursEnum occures = OccursEnum.optional; if (attributes.get("occurs") != null) { occures = OccursEnum.valueOf(attributes.get("occurs")); } Element element = new Element(name, type, occures); parent.addElement(element); xmlParser.nextTag(); } }
public void registerSchema( @Nonnull final String schemaId, @Nonnull @WillClose final InputStream inputStreamSchema) throws Exception { try { Schema niftyXmlSchema = new Schema(parserFactory, nifty.getResourceLoader()); XmlParser parser = new XmlParser(parserFactory.newPullParser()); parser.read(inputStreamSchema); parser.nextTag(); parser.required("nxs", niftyXmlSchema); schemes.put(schemaId, niftyXmlSchema); } finally { closeSilently(inputStreamSchema); } }
@Override public void process(@Nonnull final XmlParser xmlParser, @Nonnull final Attributes attributes) throws Exception { packageString = attributes.get("package"); if (packageString == null) { throw new Exception("[package] attribute is a required attribute"); } root = attributes.get("root"); if (root == null) { throw new Exception("[root] attribute is a required attribute"); } type = attributes.get("type"); if (type == null) { throw new Exception("[type] attribute is a required attribute"); } xmlParser.nextTag(); xmlParser.zeroOrMore( new de.lessvoid.xml.xpp3.SubstitutionGroup() .add("include", new IncludeProcessor(parserFactory, resourceLoader, types)) .add("type", new TypeProcessor(this))); }
@Override public void process(@Nonnull final XmlParser xmlParser, @Nonnull final Attributes attributes) throws Exception { xmlType = ClassHelper.getInstance(fullClassName, XmlType.class); if (xmlType == null) { log.log(Level.SEVERE, "Failed to process XML. Requested class " + fullClassName + " failed to locate."); } else { xmlType.applyAttributes(attributes); if (xmlTypeParentSingle != null) { invoke(xmlType, xmlTypeParentSingle, "set"); } else if (xmlTypeParentMultiple != null) { invoke(xmlType, xmlTypeParentMultiple, "add"); } xmlParser.nextTag(); for (XmlProcessorElement child : elements) { child.process(xmlParser, xmlType); } for (XmlProcessorSubstituitionGroup subst : substGroups) { xmlParser.zeroOrMore(subst.getSubstGroup(xmlType)); } } }
@Override public void process(@Nonnull final XmlParser xmlParser, @Nonnull final Attributes attributes) throws Exception { String name = getNameAttribute(attributes); Type type = new Type(name, getExtendsAttribute(attributes)); niftyXmlSchema.addType(name, type); SubstitutionGroup substGroup = new SubstitutionGroup(); substGroup.add("element", new TypeProcessorElement(type)); substGroup.add("group", new TypeProcessorSubstitutionGroup(type)); xmlParser.nextTag(); xmlParser.zeroOrMore(substGroup); }