List of usage examples for org.dom4j Attribute getValue
String getValue();
From source file:StreamFlusher.java
License:Apache License
private Fst xml2fst(String filepath) throws Exception { // read an XML file representing a network, return the network final Fst fst = lib.EmptyLanguageFst(); final HashSet<Integer> sigma = fst.getSigma(); SAXReader reader = new SAXReader(); // SAXReader from dom4j // each SAXReader handler must define onStart() and onEnd() methods // when the kleeneFst element is first found reader.addHandler("/kleeneFst", new ElementHandler() { public void onStart(ElementPath path) { Element current = path.getCurrent(); // semiring is an attribute on the kleeneFst node String semiring = current.attribute("semiring").getValue(); }/* ww w . java 2s . c o m*/ public void onEnd(ElementPath path) { } }); reader.addHandler("/kleeneFst/sigma", new ElementHandler() { public void onStart(ElementPath path) { if (path.getCurrent().attribute("containsOther").getValue().equals("true")) { fst.setContainsOther(true); } else { fst.setContainsOther(false); } ; } public void onEnd(ElementPath path) { } }); reader.addHandler("/kleeneFst/sigma/sym", new ElementHandler() { public void onStart(ElementPath path) { } public void onEnd(ElementPath path) { Element sym = path.getCurrent(); sigma.add(symmap.putsym(sym.getText())); sym.detach(); } }); // when the arcs element is first found reader.addHandler("/kleeneFst/arcs", new ElementHandler() { public void onStart(ElementPath path) { // grab the two attrs and convert to int int startState = Integer.parseInt(path.getCurrent().attribute("start").getValue()); int numStates = Integer.parseInt(path.getCurrent().attribute("numStates").getValue()); lib.AddStates(fst, numStates); // native function, add this many // states to the new Fst lib.SetStart(fst, startState); // set the start state } public void onEnd(ElementPath path) { } }); // handle each whole arc element reader.addHandler("/kleeneFst/arcs/arc", new ElementHandler() { // in an ElementHandler, need to supply .onStart(), // called when the start tag is found, and // .onEnd(), which is called when the end tag is found. public void onStart(ElementPath path) { } public void onEnd(ElementPath path) { // retrieve the entire arc element Element arc = path.getCurrent(); // these two are always present int src_id = Integer.parseInt(arc.attribute("s").getValue()); int dest_id = Integer.parseInt(arc.attribute("d").getValue()); // there will be either one io attr xor separate i and o attrs // (keep the io option to facilitate hand-written XML files) String input; String output; Attribute io = arc.attribute("io"); if (io != null) { input = io.getValue(); output = io.getValue(); } else { input = arc.attribute("i").getValue(); output = arc.attribute("o").getValue(); } if (!symmap.containsKey(input)) { // symbol name in XML file not in the // current internal symtab symmap.putsym(input); } if (!symmap.containsKey(output)) { symmap.putsym(output); } // the w attr is optional in the arc elmt Attribute w = arc.attribute("w"); if (w != null) { // call AddArc to add an arc to the fst // being built from the XML file description // semiring generalization point lib.AddArc(fst, src_id, symmap.getint(input), symmap.getint(output), Float.parseFloat(w.getValue()), dest_id); } else { // semiring generalization point lib.AddArcNeutralWeight(fst, src_id, symmap.getint(input), symmap.getint(output), dest_id); } arc.detach(); } }); // for each full final element reader.addHandler("/kleeneFst/arcs/final", new ElementHandler() { public void onStart(ElementPath path) { } public void onEnd(ElementPath path) { Element arc = path.getCurrent(); // s attr is always present int src_id = Integer.parseInt(arc.attribute("s").getValue()); // the w attr is optional Attribute w = arc.attribute("w"); if (w != null) { lib.SetFinal(fst, src_id, Float.parseFloat(w.getValue())); } else { lib.SetFinalNeutralWeight(fst, src_id); } arc.detach(); } }); Document document = null; // the actual XML reading/parsing is done here try { // XmlReader detects the encoding of the XML document and // handles BOMs, including the UTF-8 BOMs that Java usually // chokes on document = reader.read(new XmlReader(new FileInputStream(filepath))); // Old, pre-XmlReader code //if (encoding.equals("UTF-8")) { // // then need to work around SUN's irresponsible decision not to // // handle the optional UTF-8 BOM correctly // document = reader.read(new InputStreamReader( // new UTF8BOMStripperInputStream(new FileInputStream(filepath)), // "UTF-8") // ) ; //} else { // document = reader.read(new InputStreamReader( // new FileInputStream(filepath), // encoding) // ) ; //} } catch (DocumentException de) { // dom4j DocumentException extends Exception de.printStackTrace(); throw de; } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(); throw fnfe; } catch (Exception e) { e.printStackTrace(); throw e; } correctSigmaOther(fst); return fst; }
From source file:architecture.common.xml.XmlWriter.java
License:Apache License
/** * Writes the attributes of the given element * *//* w w w . j a v a 2s. co m*/ protected void writeAttributes(Element element) throws IOException { // I do not yet handle the case where the same prefix maps to // two different URIs. For attributes on the same element // this is illegal; but as yet we don't throw an exception // if someone tries to do this for (int i = 0, size = element.attributeCount(); i < size; i++) { Attribute attribute = element.attribute(i); Namespace ns = attribute.getNamespace(); if (ns != null && ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) { String prefix = ns.getPrefix(); String uri = namespaceStack.getURI(prefix); if (!ns.getURI().equals(uri)) { // output a new namespace // declaration writeNamespace(ns); namespaceStack.push(ns); } } // If the attribute is a namespace declaration, check if we have // already // written that declaration elsewhere (if that's the case, it must // be // in the namespace stack String attName = attribute.getName(); if (attName.startsWith("xmlns:")) { String prefix = attName.substring(6); if (namespaceStack.getNamespaceForPrefix(prefix) == null) { String uri = attribute.getValue(); namespaceStack.push(prefix, uri); writeNamespace(prefix, uri); } } else if (attName.equals("xmlns")) { if (namespaceStack.getDefaultNamespace() == null) { String uri = attribute.getValue(); namespaceStack.push(null, uri); writeNamespace(null, uri); } } else { char quote = format.getAttributeQuoteCharacter(); writer.write(" "); writer.write(attribute.getQualifiedName()); writer.write("="); writer.write(quote); writeEscapeAttributeEntities(attribute.getValue()); writer.write(quote); } } }
From source file:architecture.common.xml.XmlWriter.java
License:Apache License
protected void writeAttribute(Attribute attribute) throws IOException { writer.write(" "); writer.write(attribute.getQualifiedName()); writer.write("="); char quote = format.getAttributeQuoteCharacter(); writer.write(quote);//from www . jav a 2 s. c o m writeEscapeAttributeEntities(attribute.getValue()); writer.write(quote); lastOutputNodeType = Node.ATTRIBUTE_NODE; }
From source file:architecture.ee.util.xml.XmlWriter.java
License:Apache License
/** Writes the attributes of the given element *//w w w. j ava 2 s . com */ protected void writeAttributes(Element element) throws IOException { // I do not yet handle the case where the same prefix maps to // two different URIs. For attributes on the same element // this is illegal; but as yet we don't throw an exception // if someone tries to do this for (int i = 0, size = element.attributeCount(); i < size; i++) { Attribute attribute = element.attribute(i); Namespace ns = attribute.getNamespace(); if (ns != null && ns != Namespace.NO_NAMESPACE && ns != Namespace.XML_NAMESPACE) { String prefix = ns.getPrefix(); String uri = namespaceStack.getURI(prefix); if (!ns.getURI().equals(uri)) { // output a new namespace declaration writeNamespace(ns); namespaceStack.push(ns); } } // If the attribute is a namespace declaration, check if we have already // written that declaration elsewhere (if that's the case, it must be // in the namespace stack String attName = attribute.getName(); if (attName.startsWith("xmlns:")) { String prefix = attName.substring(6); if (namespaceStack.getNamespaceForPrefix(prefix) == null) { String uri = attribute.getValue(); namespaceStack.push(prefix, uri); writeNamespace(prefix, uri); } } else if (attName.equals("xmlns")) { if (namespaceStack.getDefaultNamespace() == null) { String uri = attribute.getValue(); namespaceStack.push(null, uri); writeNamespace(null, uri); } } else { char quote = format.getAttributeQuoteCharacter(); writer.write(" "); writer.write(attribute.getQualifiedName()); writer.write("="); writer.write(quote); writeEscapeAttributeEntities(attribute.getValue()); writer.write(quote); } } }
From source file:ch.javasoft.metabolic.efm.config.Arithmetic.java
License:BSD License
public static Arithmetic parse(Attribute attribute) throws XmlConfigException { try {/*from w w w. j av a 2 s.com*/ return parse(attribute.getValue()); } catch (IllegalArgumentException ex) { throw new XmlConfigException(ex.getLocalizedMessage(), attribute); } }
From source file:ch.javasoft.metabolic.efm.config.Generator.java
License:BSD License
public static Generator parse(Attribute attribute) throws XmlConfigException { final String str = attribute.getValue(); try {// w w w .j a v a 2 s .c o m return valueOf(StringUtil.toTitleCase(str)); } catch (IllegalArgumentException ex) { throw new XmlConfigException( "invalid value for generator, expected " + Arrays.toString(values()) + ", but found: " + str, attribute); } }
From source file:ch.javasoft.metabolic.efm.config.Normalize.java
License:BSD License
/** * Parse a norm string and return the appropriate constant, or throw * an exception if not recognized as such. *//*from www. j av a 2s . com*/ public static Normalize parse(Attribute attribute) throws XmlConfigException { final String str = attribute.getValue(); try { return valueOf(str.toLowerCase()); } catch (IllegalArgumentException ex) { throw new XmlConfigException( "invalid value for norm, expected " + Arrays.toString(values()) + ", but found: " + str, attribute); } }
From source file:ch.javasoft.metabolic.efm.config.XmlElement.java
License:BSD License
public static int parsePrecision(Attribute attribute) throws XmlConfigException { final String str = attribute.getValue(); try {/*from w w w . j a v a2 s . c om*/ return Integer.parseInt(str); } catch (NumberFormatException ex) { throw new XmlConfigException("invalid value for precision: " + str, attribute); } }
From source file:ch.javasoft.metabolic.efm.config.XmlElement.java
License:BSD License
public static double parseZero(Attribute attribute) throws XmlConfigException { final String str = attribute.getValue(); try {/* w ww . ja va 2 s . co m*/ return Double.parseDouble(str); } catch (NumberFormatException ex) { throw new XmlConfigException("invalid value for precision: " + str, attribute); } }
From source file:ch.javasoft.xml.config.XmlConfig.java
License:BSD License
@SuppressWarnings("unchecked") protected List<Element> resolve(Element element, String path) throws XmlConfigException { final List<Element> resolved = new ArrayList<Element>(); Attribute refAtt = element.attribute(XmlAttribute.ref.getXmlName()); if (refAtt == null) { resolved.add(element);//ww w. ja v a 2 s. c om } else { resolveAttributeValue(refAtt, path); List<Element> refElCont = getReferredElementContent(refAtt.getValue(), path); for (final Element el : refElCont) { final Element newEl = el.createCopy(); element.getParent().add(newEl); resolved.addAll(resolve(newEl, XmlUtil.getElementPath(el, true/*recurseParents*/))); } if (!element.getParent().remove(element)) { throw new RuntimeException("internal error: should have been removed"); } } for (Element elem : resolved) { Iterator<Attribute> itA = elem.attributeIterator(); while (itA.hasNext()) { Attribute att = itA.next(); resolveAttributeValue(att, path); } // resolve(elem.elementIterator(), path); Iterator<Element> itE = elem.elementIterator(); while (itE.hasNext()) { Element child = itE.next(); resolve(child, path + "/" + XmlUtil.getElementPath(child, false /*recurseParents*/)); } if (elem.attribute(XmlAttribute.ref.getXmlName()) != null) { throw new RuntimeException("internal error: should have been resolved"); } } return resolved; }