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); } }
/** * next start or end tag. * * @throws Exception exception */ public void nextTag() throws Exception { if (xpp.getEventType() == XmlPullParser.END_DOCUMENT) { return; } int eventType = xpp.next(); while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.END_TAG) { if (log.isLoggable(Level.FINE)) { indent(); log.fine(indent() + "END <" + xpp.getName() + ">"); } return; } else if (eventType == XmlPullParser.START_TAG) { if (log.isLoggable(Level.FINE)) { log.fine(indent() + "START <" + xpp.getName() + ">"); } return; } eventType = xpp.next(); } }
/** * 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); }
public void process( @Nonnull final XmlParser xmlParser, @Nonnull final XmlType xmlTypeParent) throws Exception { if (elementOccurs.equals(OccursEnum.required)) { elementProcessor.parentLinkSet(xmlTypeParent, elementName); xmlParser.required(elementName, elementProcessor); } else if (elementOccurs.equals(OccursEnum.oneOrMore)) { elementProcessor.parentLinkAdd(xmlTypeParent, elementName); xmlParser.oneOrMore(elementName, elementProcessor); } else if (elementOccurs.equals(OccursEnum.optional)) { elementProcessor.parentLinkSet(xmlTypeParent, elementName); xmlParser.optional(elementName, elementProcessor); } else if (elementOccurs.equals(OccursEnum.zeroOrMore)) { elementProcessor.parentLinkAdd(xmlTypeParent, elementName); xmlParser.zeroOrMore(elementName, elementProcessor); } }
@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)); } } }
public void loadStyleFile( @Nonnull final String schemaId, @Nonnull final String styleFilename, @Nonnull final NiftyType niftyType, @Nonnull final Nifty nifty) throws Exception { log.fine("loading new nifty style xml file [" + styleFilename + "] with schemaId [" + schemaId + "]"); XmlParser parser = new XmlParser(parserFactory.newPullParser()); InputStream stream = null; try { stream = nifty.getResourceAsStream(styleFilename); if (stream == null) { throw new IOException("Failed to load style. Resource \"" + styleFilename + "\" not found"); } parser.read(stream); NiftyStylesType niftyStylesType = (NiftyStylesType) getSchema(schemaId).loadXml(parser); niftyStylesType.loadStyles(this, niftyType, nifty, log); } finally { closeSilently(stream); } }
@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())); } }
@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; }
@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(); } }
@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))); }
public void loadControlFile( @Nonnull final String schemaId, @Nonnull final String controlFilename, @Nonnull final NiftyType niftyType) throws Exception { log.fine("loading new nifty controls xml file [" + controlFilename + "] with schemaId [" + schemaId + "]"); XmlParser parser = new XmlParser(parserFactory.newPullParser()); InputStream stream = null; try { stream = nifty.getResourceAsStream(controlFilename); if (stream == null) { throw new IOException("Failed to load control. Resource \"" + controlFilename + "\" not found"); } parser.read(stream); NiftyControlsType niftyControlsType = (NiftyControlsType) getSchema(schemaId).loadXml(parser); niftyControlsType.loadControls(this, niftyType); } finally { closeSilently(stream); } }
/** * 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 { 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 = 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); }
/** * Load a Nifty-GUI XML file and get the type that is defined in the file. The XML file needs to be a valid XML GUI * definition. * * @param schemaId the name of the schema that should be used to validate the XML file * @param inputStreamXml the input stream of the XML file, this stream <b>will be closed</b> by this function * @return the NiftyType that was load from the XML file * @throws Exception in case the loading fails at any point */ @Nonnull public NiftyType loadNiftyXml( @Nonnull final String schemaId, @Nonnull @WillClose final InputStream inputStreamXml) throws Exception { try { long start = timeProvider.getMsTime(); log.fine("loading new nifty xml file with schemaId [" + schemaId + "]"); XmlParser parser = new XmlParser(parserFactory.newPullParser()); parser.read(inputStreamXml); NiftyType niftyType = (NiftyType) getSchema(schemaId).loadXml(parser); niftyType.loadStyles(this, nifty); niftyType.loadControls(this); long end = timeProvider.getMsTime(); log.fine("loaded nifty xml file with schemaId [" + schemaId + "] took [" + (end - start) + " ms]"); return niftyType; } finally { closeSilently(inputStreamXml); } }
/** * 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); }
/** * 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); }