Example usage for org.dom4j Element attribute

List of usage examples for org.dom4j Element attribute

Introduction

In this page you can find the example usage for org.dom4j Element attribute.

Prototype

Attribute attribute(QName qName);

Source Link

Document

DOCUMENT ME!

Usage

From source file:ca.coder2000.recipes.RecipeFile.java

License:Mozilla Public License

/**
 * Checks the version of the recipe file.
 * @throws Exception If the version of the recipe file is wrong.
 *///from  w  w  w  .  ja  v a2 s. c o m
public void checkVersion() throws Exception {
    Element root = doc.getRootElement();
    Attribute version = root.attribute("version");

    if (Double.valueOf(version.getStringValue()).doubleValue() != 0.1) {
        valid = false;
        throw new Exception(
                java.util.ResourceBundle.getBundle("recipe").getString("Invalid_Recipe_File_Version"));
    }

    valid = true;
}

From source file:cc.warlock.build.FeatureVersionReplacer.java

License:Open Source License

protected void replaceVersions() throws BuildException {
    SAXReader reader = new SAXReader();

    try {//  w  ww  .j  a  v a2s .c o  m
        File featureFile = new File(new File(new File(buildDirectory, "features"), feature), "feature.xml");
        Document featureDoc = reader.read(featureFile);
        Element featureEl = featureDoc.getRootElement();
        featureEl.attribute("version").setValue(version);

        //          Properties versions = new Properties();
        //          File versionsFile = new File(buildDirectory, "finalPluginsVersions.properties");
        //          FileInputStream stream = new FileInputStream(versionsFile);
        //          versions.load(stream);
        //          stream.close();
        //          
        //          for (Element pluginEl : (List<Element>) featureEl.elements("plugin"))
        //          {
        //             if (pluginEl.attributeValue("version").equals("0.0.0"))
        //             {
        //                if (versions.containsKey(pluginEl.attributeValue("id")))
        //                   pluginEl.attribute("version").setValue(versions.getProperty(pluginEl.attributeValue("id")));
        //             }
        //          }

        OutputFormat format = OutputFormat.createPrettyPrint();

        FileOutputStream outStream = new FileOutputStream(featureFile);
        XMLWriter writer = new XMLWriter(outStream, format);
        writer.write(featureDoc);
        outStream.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:cc.warlock.rcp.application.WarlockPerspectiveLayout.java

License:Open Source License

public void parseElement(Element element) {
    if (element.attribute("x") != null) {
        bounds.x = Integer.parseInt(element.attributeValue("x"));
        bounds.y = Integer.parseInt(element.attributeValue("y"));
        bounds.width = Integer.parseInt(element.attributeValue("width"));
        bounds.height = Integer.parseInt(element.attributeValue("height"));
    }//from   w  w w .ja  v  a2  s  . c  o m
}

From source file:ch.javasoft.metabolic.efm.config.Config.java

License:BSD License

/**
 * Derives the configuration from the xml file stored in the system 
 * properties.//from   ww  w  . ja v a2 s  .co m
 * @return                  the config
 * @throws XmlConfigException   if invalid configuration settings were 
 *                         detected
 */
public static Config getFromXmlConfig(XmlConfig xmlConfig) throws XmlConfigException {
    Element efmCompConfig = getConfigMetabolicCompression(xmlConfig);
    Element efmImplConfig = getConfigEfmImplConfig(xmlConfig);
    Element efmOutputConfig = getConfigEfmOutput(xmlConfig);
    Element efmNumConfig = XmlUtil.getRequiredSingleChildElement(efmImplConfig, XmlElement.numeric);
    Element efmDistConfig = XmlUtil.getOptionalSingleChildElement(efmImplConfig, XmlElement.distribute);
    Element elGenerator = XmlUtil.getRequiredSingleChildElement(efmImplConfig, XmlElement.generator);
    Element elReacsToSup = XmlUtil.getRequiredSingleChildElement(efmImplConfig,
            XmlElement.reactions_to_suppress);
    Element elReacsToEnf = XmlUtil.getRequiredSingleChildElement(efmImplConfig,
            XmlElement.reactions_to_enforce);
    Element elReacsNoSplit = XmlUtil.getRequiredSingleChildElement(efmImplConfig,
            XmlElement.reactions_no_split);
    Element elTmpDir = XmlUtil.getRequiredSingleChildElement(efmImplConfig, XmlElement.temp_dir);
    Element elProgress = XmlUtil.getRequiredSingleChildElement(efmImplConfig, XmlElement.progress);
    Element elFlag = XmlUtil.getOptionalSingleChildElement(efmImplConfig, XmlElement.flag);
    String rowOrdering = getAttributeValue(efmImplConfig, XmlElement.row_ordering, XmlAttribute.value);
    String adjMethod = getAttributeValue(efmImplConfig, XmlElement.adjacency_method, XmlAttribute.value);
    int maxThreads = Integer
            .parseInt(getAttributeValue(efmImplConfig, XmlElement.maxthreads, XmlAttribute.value));
    Element elArithmetic = XmlUtil.getRequiredSingleChildElement(efmNumConfig, XmlElement.arithmetic);
    Element elPrecision = XmlUtil.getRequiredSingleChildElement(efmNumConfig, XmlElement.precision);
    Element elZero = XmlUtil.getRequiredSingleChildElement(efmNumConfig, XmlElement.zero);
    int precision = XmlElement.parsePrecision(elPrecision.attribute(XmlAttribute.value.getXmlName()));
    double dblZero = XmlElement.parseZero(elZero.attribute(XmlAttribute.value.getXmlName()));
    Arithmetic arithmetic = Arithmetic.parse(elArithmetic.attribute(XmlAttribute.value.getXmlName()));
    Normalize normalize = Normalize.parse(efmOutputConfig.attribute(XmlAttribute.normalize.getXmlName()));
    Generator generator = Generator.parse(elGenerator.attribute(XmlAttribute.value.getXmlName()));
    String reacsToSuppress = XmlUtil.getRequiredAttributeValue(elReacsToSup, XmlAttribute.value);
    String reacsToEnforce = XmlUtil.getRequiredAttributeValue(elReacsToEnf, XmlAttribute.value);
    String reacsNoSplit = XmlUtil.getRequiredAttributeValue(elReacsNoSplit, XmlAttribute.value);
    Zero zero = Double.isNaN(dblZero) ? arithmetic.getDefaultZero() : new Zero(dblZero);
    File tmpDir = new File(XmlUtil.getRequiredAttributeValue(elTmpDir, XmlAttribute.name));
    String strProgType = XmlUtil.getRequiredAttributeValue(elProgress, XmlAttribute.type);
    String strProgPartition = XmlUtil.getRequiredAttributeValue(elProgress, XmlAttribute.partition);
    String flag = elFlag == null ? null : XmlUtil.getOptionalAttributeValue(elFlag, XmlAttribute.value, null);
    final int progPartition;
    try {
        progPartition = Integer.parseInt(strProgPartition);
    } catch (Exception ex) {
        throw new XmlConfigException(
                "invalid value for progress partition, expected integer value, but found: " + strProgPartition,
                elProgress);
    }
    final ProgressType progType = ProgressType.parse(strProgType);

    MetabolicCompressionConfig cmpCfg = new MetabolicCompressionConfig(efmCompConfig);
    final boolean preprocessDuplicateGenes;
    final CompressionMethod[] cmpMethods;
    try {
        preprocessDuplicateGenes = cmpCfg.getPreprocessDuplicateGenes();
        cmpMethods = cmpCfg.getCompressionMethods();
    } catch (ch.javasoft.metabolic.compress.config.XmlConfigException ex) {
        throw new XmlConfigException(ex.getMessage(), ex.getNode());
    }

    if (flag == null || (flag = flag.trim()).length() == 0)
        flag = null;
    final DistributedConfig distConfig = new DistributedConfig(efmDistConfig);

    boolean selfTest = Boolean
            .parseBoolean(getAttributeValue(efmImplConfig, XmlElement.self_test, XmlAttribute.value));
    boolean parseOnly = Boolean
            .parseBoolean(getAttributeValue(efmImplConfig, XmlElement.parse_only, XmlAttribute.value));
    return new Config(xmlConfig, zero, adjMethod, rowOrdering, cmpMethods, preprocessDuplicateGenes, selfTest,
            parseOnly, maxThreads, arithmetic, precision, generator, normalize, reacsToSuppress, reacsToEnforce,
            reacsNoSplit, tmpDir, progPartition, progType, flag, distConfig);
}

From source file:ch.javasoft.xml.config.ConstConfigParser.java

License:BSD License

private static <T> T parseConstant(Element constElement, Class<T> expectedType, boolean allowNull)
        throws XmlConfigException {
    final String sValue;
    if (constElement.attributeCount() == 1) {
        sValue = XmlUtil.getRequiredAttributeValue(constElement, new Attribute(constElement.attribute(0)));
    } else {//from  w  ww .j  a v  a  2  s  .  c  om
        sValue = XmlUtil.getRequiredAttributeValue(constElement, XmlAttribute.value);
    }
    if (allowNull && sValue.equals("null")) {
        return null;
    }
    try {
        return expectedType.getConstructor(METHOD_SIGNATURE).newInstance(new Object[] { sValue });
    } catch (Exception ex) {
        throw new XmlConfigException(
                "cannot create " + expectedType.getSimpleName() + " const value '" + sValue + "', e=" + ex,
                constElement, ex);
    }
}

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);//w ww . ja v a 2  s.c  o  m
    } 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;
}

From source file:ch.javasoft.xml.config.XmlConfig.java

License:BSD License

/**
 * Returns the logging properties read from the <tt>logging</tt> element, or
 * <code>null</code> if no logging properties found in the xml config.
 * <p>//from www.j  a v  a 2  s .c om
 * The logging element is expected to look like this:
 * <pre>
   <logging>
  <prop name=".level"    value="INFO"/>   
  <prop name="handlers"    value="ch.javasoft.util.logging.StandardOutHandler,ch.javasoft.util.logging.StandardErrHandler"/>
  ...
    </logging>
 * </pre>
 * 
 * @param config            the config element to use
 * @throws XmlConfigException   if an xml configuration error occurs
 */
@SuppressWarnings("unchecked")
private Properties getLoggingProperties(Element config) throws XmlConfigException {
    final Element logging = XmlUtil.getOptionalSingleChildElement(config, XmlElement.logging);
    final List<Element> props = (List<Element>) logging.elements(XmlElement.property.getXmlName());

    if (props != null) {
        final Properties loggingProps = new Properties();
        for (final Element property : props) {
            String path = XmlUtil.getElementPath(property, true /*recurseParents*/);

            final Attribute attRef = property.attribute(XmlAttribute.ref.getXmlName());
            final Iterable<Element> resolved;
            if (attRef != null) {
                resolved = resolve(property, path);
            } else {
                resolved = Collections.singleton(property);
            }
            for (Element prop : resolved) {
                final Attribute attName = prop.attribute(XmlAttribute.name.getXmlName());
                final Attribute attValue = prop.attribute(XmlAttribute.value.getXmlName());
                if (attName == null) {
                    throw new XmlConfigException("name attribute missing", path);
                }
                resolveAttributeValue(attName, path);
                final String name = XmlUtil.getRequiredAttributeValue(prop, XmlAttribute.name);
                path += "[name=" + name + "]";
                resolveAttributeValue(attValue, path);
                final String value = XmlUtil.getRequiredAttributeValue(prop, XmlAttribute.value);
                loggingProps.put(name, value);
            }
        }
        return loggingProps;
    }
    return null;
}

From source file:ch.javasoft.xml.config.XmlUtil.java

License:BSD License

/**
 * Returns the desired attribute value, or throws an exception no such
 * attribute exists.//from  w w w . j  ava  2  s .com
 * 
 * @param element            the element to get the attribute value from
 * @param attribute            specifies the desired attribute name
 * @return                  the attribute value
 * @throws XmlConfigException   if no such attribute value is defined
 */
public static String getRequiredAttributeValue(Element element, XmlNode attribute) throws XmlConfigException {
    Attribute att = element.attribute(attribute.getXmlName());
    if (att == null) {
        throw new XmlConfigException(
                "missing " + attribute.getXmlName() + " attribute for " + element.getName() + " element",
                element);
    }
    return att.getValue();
}

From source file:ch.javasoft.xml.config.XmlUtil.java

License:BSD License

/**
 * Returns the desired attribute value, or the given default value if no 
 * such attribute exists.//w  w w .  ja  v  a2 s .c  o  m
 * 
 * @param element            the element to get the attribute value from
 * @param attribute            specifies the desired attribute name
 * @param defaultValue         the default value to use if no such 
 *                         attribute exists
 * @return                  the attribute value, or the default if no
 *                         such attribute exists
 */
public static String getOptionalAttributeValue(Element element, XmlNode attribute, String defaultValue)
        throws XmlConfigException {
    Attribute att = element.attribute(attribute.getXmlName());
    if (att == null) {
        return defaultValue;
    }
    return att.getValue();
}

From source file:client.ManageNets.java

License:Open Source License

@SuppressWarnings("unchecked")
private void parseDoc() {
    //seznam uzivatelu
    List<Element> u = serverroot.elements();
    //seznam siti
    List<Element> n;
    //seznam verzi
    List<Element> v;
    //n.size();/*from  w  w w.j a  v  a2s  . c o  m*/

    names = new String[u.size()];
    nets = new String[u.size()][];
    versions = new String[u.size()][][];
    descriptions = new String[u.size()][][];

    int i = 0;
    int j = 0;
    int k = 0;
    for (Element e : u) {

        names[i] = e.attribute("login").getValue();

        n = e.elements();

        nets[i] = new String[n.size()];
        versions[i] = new String[n.size()][];
        descriptions[i] = new String[n.size()][];

        j = 0;
        for (Element f : n) {

            nets[i][j] = f.attribute("name").getText();

            v = f.elements();

            versions[i][j] = new String[v.size()];
            descriptions[i][j] = new String[v.size()];

            //s verzema jdu odzadu aby byly nejnovejsi nejdrive
            k = v.size() - 1;
            for (Element g : v) {

                versions[i][j][k] = convVerToPrint(g.attribute("time").getText());
                descriptions[i][j][k] = g.attribute("description").getText();

                k--;
            }
            j++;
        }
        i++;
    }
}