Example usage for org.jdom2 Element getAttributeValue

List of usage examples for org.jdom2 Element getAttributeValue

Introduction

In this page you can find the example usage for org.jdom2 Element getAttributeValue.

Prototype

public String getAttributeValue(final String attname) 

Source Link

Document

This returns the attribute value for the attribute with the given name and within no namespace, null if there is no such attribute, and the empty string if the attribute value is empty.

Usage

From source file:com.init.octo.schema.XSDSchema.java

License:Open Source License

private boolean privateBuild(String schemaFileName, Document document, boolean topLevel) throws Exception {
    int schemaFileNamesIdx = 0;

    String attributeValue;/*from w ww. j  a v a2  s  .c  om*/
    XSDElement thisDocRoot;

    // get the root element of the XML - this should be <schema>, or we wont process the document 

    Element xsdRoot = document.getRootElement();
    String elementName = xsdRoot.getName();

    if (elementName.equals(SCHEMA) == false) {
        throw new Exception("The root element of the XML document is not <" + SCHEMA + ">");
    }

    id = xsdRoot.getAttributeValue(XSDSchema.ID_ATT);
    targetNamespace = xsdRoot.getAttributeValue(XSDSchema.TARGNAMSP_ATT);

    try {
        cache.pushNamespaces(schemaFileName);
    } catch (Exception ex) {
        log.fatal("Exception caching namespaces: " + ex.toString());
        return (false);
    }

    /** Go through the document and find any include or import elements... **/

    for (Element element : xsdRoot.getChildren()) {
        elementName = element.getName();

        if (elementName.equals(IMPORT) || elementName.equals(INCLUDE)) {
            attributeValue = element.getAttributeValue(SCHEMALOC_ATT);
            if (cache.schemaCached(attributeValue) == true) {
                log.debug("Schema already cached... ignoring");
                continue;
            }

            log.debug(elementName + " schema [" + attributeValue + "]");

            String importSchemaName = null;

            /* If the schema is relative to the current schema then work out the absolute name... */

            if (attributeValue.startsWith("..") == true) {

                String[] currentSchemaPath = ((String) schemaFileNames.get(schemaFileNamesIdx))
                        .split("[/\\\\]");
                String[] importSchemaFile = attributeValue.split("[/\\\\]");
                int idx = 0;
                int n = 0;
                StringBuffer realImportName = new StringBuffer();

                while (importSchemaFile[n].equals("..")) {
                    idx++;
                    n++;
                }

                for (n = 0; n < (currentSchemaPath.length - 1) - idx; n++) {
                    realImportName.append(currentSchemaPath[n]);
                    realImportName.append(File.separator);
                }

                for (n = idx; n < importSchemaFile.length; n++) {
                    realImportName.append(importSchemaFile[n]);
                    if (n < importSchemaFile.length - 1) {
                        realImportName.append(File.separator);
                    }
                }

                importSchemaName = realImportName.toString();
            } else { /* schema in current directory... */

                File sf = new File((String) schemaFileNames.get(schemaFileNamesIdx));
                String dirName = sf.getParent();
                importSchemaName = dirName + File.separator + attributeValue;
            }

            Document doc = builder.build(new FileReader(importSchemaName));

            schemaFileNames.add(importSchemaName);
            schemaFileNamesIdx++;

            cache.putSchema(attributeValue, document);
            privateBuild(importSchemaName, doc, false);

            schemaFileNames.remove(schemaFileNamesIdx);
            schemaFileNamesIdx--;
        }

    }

    // do it twice... so that all predefined types are picked up, no matter what order they are defined in...
    // @todo I know it is clunky, and when I get time I will make it scan for types (and imports etc!) first then build the definition.....

    for (int x = 0; x < 2; x++) {

        /** iterate all of the child elements of schema **/

        for (Element element : xsdRoot.getChildren()) {
            elementName = element.getName();
            log.debug("" + indent + ": " + "Element <" + elementName + ">");

            if (elementName.equals(ELEMENT)) {
                thisDocRoot = new XSDElement(indent);
                if (thisDocRoot.build(element, cache, "") != true) {
                    log.fatal("Error processing the schema");
                    return (false);
                }

                if (topLevel == true) {
                    topLevelElements.put(elementName, thisDocRoot);
                }
            } else if (elementName.equals(SIMPLETYPE)) {
            } else if (elementName.equals(COMPLEXTYPE)) {
                XSDElementTypeComplex complexType = new XSDElementTypeComplex(indent);
                if (complexType.build(element, cache, "") != true) {
                    log.fatal("Error building a complex type");
                    throw new Exception("Error building a complex type");
                }
                log.debug("Adding type <" + complexType.getName() + "> to type cache");
                cache.putType(complexType.getName(), complexType);

                if (topLevel == true) {
                    topLevelTypes.put(complexType.getName(), complexType);
                }

            } else if (elementName.equals(GROUP)) {
                XSDGroupType group = new XSDElementGroup(indent);
                if (group.build(element, cache, "") != true) {
                    log.fatal("Error building a group");
                    return (false);
                }
            } else if (elementName.equals(ATTRIBUTEGROUP)) {
                XSDAttributeGroup attGroup = new XSDAttributeGroup();
                if (attGroup.build(element, cache, "") != true) {
                    log.warn("Error building an attribute group");
                    continue;
                }
            } else if (elementName.equals(ATTRIBUTE)) {
                XSDAttribute attribute = new XSDAttribute();
                if (attribute.build(element, cache, "") != true) {
                    log.warn("Error building an attribute object");
                    continue;
                }
            } else if (elementName.equals(IMPORT) || elementName.equals(INCLUDE)) {
                // do nothing - these have already been processed...
            } else {
                log.debug("" + indent + ": " + "Unexpected element <" + elementName + "> found and ignored");
            }
        } // end for all child elements of the schema root

    }

    cache.popNamespaces();

    return (true);

}

From source file:com.init.octo.schema.XSDSequence.java

License:Open Source License

/**
 * This method builds a sequence element
 *
 * @param    root - the sequence element that defines this element
 * @param    cache - a list of pre-defined XML types
 *//*from w  w  w  .j ava 2  s  .  c  o  m*/
public boolean build(Element root, XSDCache cache, String parentName) {

    log.debug("" + indent + ": " + "Build representation of a sequence");

    id = root.getAttributeValue(XSDSchema.ID_ATT);
    maxOccurs = root.getAttributeValue(XSDSchema.MAXOCCURS_ATT);
    minOccurs = root.getAttributeValue(XSDSchema.MINOCCURS_ATT);

    group = new LinkedList<XMLType>();

    Element element;
    String elementName;

    for (Iterator<?> i = (root.getChildren()).iterator(); i.hasNext();) {

        element = (Element) i.next();
        elementName = element.getName();

        log.debug("" + indent + ": " + "Child element <" + elementName + "> found");

        /** process the child elements that define this element...   **/

        if (elementName.equals(XSDSchema.ANNOTATION)) {
            annotation = element.getTextTrim();

        } else if (elementName.equals(XSDSchema.ELEMENT)) {
            log.debug("" + indent + ": " + "Adding element to the list of child elements");
            XSDElement e = new XSDElement(indent + 1);
            if (e.build(element, cache, parentName) != true) {
                log.debug("Error building the element");
                return (false);
            }
            group.add(e);
        } else if (elementName.equals(XSDSchema.GROUP)) {
            log.debug("" + indent + ": " + "Adding group to the list of child elements");
            XSDGroupType g = new XSDElementGroup(indent); // child elements will be at the same level

            if (g.build(element, cache, parentName) != true) {
                log.error("Error building a group");
                return (false);
            }

            group.add(g);
        } else if (elementName.equals(XSDSchema.CHOICE)) {

            log.debug("" + indent + ": " + "Adding choice to the list of child elements");

            XSDChoice c = new XSDChoice(indent); // child elements will be at the same level

            if (c.build(element, cache, parentName) != true) {
                log.error("Error building a choice");
                return (false);
            }

            group.add(c);
        } else if (elementName.equals(XSDSchema.SEQUENCE)) {

            log.debug("" + indent + ": " + "Adding sequence to the list of child elements");

            XSDSequence s = new XSDSequence(indent); // child elements will be at the same level

            if (s.build(element, cache, parentName) != true) {
                log.error("Error building a sequence");
                return (false);
            }

            group.add(s);
        } else {
            log.warn("" + indent + ": " + "Unexpected element <" + elementName + "> found and ignored");
        }

    } // end for all child elements of this <sequence> tag

    log.debug("" + indent + ": " + "Sequence built");

    return (true);

}

From source file:com.init.octo.util.FindXML.java

License:Open Source License

/**
 * This method locates an XML tag or attribute based on an input string.
 * The input locator string must be in the format root.element.element or root.element.[attribute]
 *
 * @param   locator - the definition of the element you want to locate
 * @param   root - the root element of the XML structure
 *
 * @returns   String - the string we have found, or null of it wasn't found
 *//*ww  w.  ja v a  2s .com*/

static public String findXML(String locator, Element root) {

    Element element = null;
    String retStr = null;
    StringTokenizer tokens = new StringTokenizer(locator, ".");
    String str = tokens.nextToken();

    if (tokens.countTokens() == 0) {
        locator = "root." + locator;
        tokens = new StringTokenizer(locator, ".");
        str = tokens.nextToken();
    }

    // follow the locator text element name definition down...

    element = root;

    while (tokens.hasMoreTokens()) {
        str = tokens.nextToken();

        if (str.startsWith("[")) { // an attribute has been specified
            String attName = str.substring(1, str.indexOf("]"));
            retStr = element.getAttributeValue(attName);
            element = null;
            break;
        } else {
            String[] spec = str.split(":");

            if (spec.length == 1) {
                element = element.getChild(str);
            } else {
                /** A specific member of a repeating group has been specified... **/
                Iterator<?> it = element.getChildren(spec[0]).iterator();
                int idx = 1;
                int num = 0;

                try {
                    num = Integer.parseInt(spec[1]);
                } catch (Exception x) {
                    log.warn("Bad element index [" + spec[1] + "]");
                    num = 1;
                }

                while (it.hasNext()) {
                    element = (Element) it.next();
                    if (idx == num) {
                        break;
                    }
                }

                /** If we go past the end of the list we will return the last one... **/
                /** this way the call can detect no change in the output...          **/
            }
        }
        if (element == null) {
            return (null);
        }
    }

    if (element != null) { // wasn't specified as an attribute...
        retStr = element.getTextTrim();
    }

    return (retStr);

}

From source file:com.khodev.oradiff.io.XmlSchemaReader.java

License:Open Source License

private Trigger getTrigger(Element child) {
    return new Trigger(child.getAttributeValue("name"), child.getAttributeValue("type"),
            child.getAttributeValue("event"), child.getAttributeValue("table"), child.getChildText("when"),
            child.getAttributeValue("status"), child.getAttributeValue("description"),
            child.getChildText("body"));
}

From source file:com.khodev.oradiff.io.XmlSchemaReader.java

License:Open Source License

private Sequence getSequence(Element element) {
    return new Sequence(element.getAttributeValue("name"), element.getAttributeValue("minValue"),
            element.getAttributeValue("maxValue"), element.getAttributeValue("incrementBy"),
            Boolean.parseBoolean(element.getAttributeValue("cycleFlag")),
            Boolean.parseBoolean(element.getAttributeValue("orderFlag")),
            Integer.parseInt(element.getAttributeValue("cacheSize")), element.getAttributeValue("lastNumber"));
}

From source file:com.khodev.oradiff.io.XmlSchemaReader.java

License:Open Source License

private View getView(Element element) {
    View view = new View(element.getAttributeValue("name"), element.getChildText("source"));
    List<Element> children = getChildren(element, "columns");
    for (Element child : children)
        try {//  www  .j a va  2s .  co m
            view.getColumns().add(child.getAttributeValue("name"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    return view;
}

From source file:com.khodev.oradiff.io.XmlSchemaReader.java

License:Open Source License

private Procedure getProcedure(Element element) {
    Procedure procedure = new Procedure(element.getAttributeValue("name"));
    procedure.getBody().clear();//from w  w w .j  av a  2s.c  o  m
    String bodyStr = element.getChild("body").getText();
    String[] lines = bodyStr.split("\n");
    for (String line : lines)
        procedure.getBody().add(line + "\n");
    return procedure;
}

From source file:com.khodev.oradiff.io.XmlSchemaReader.java

License:Open Source License

private Function getFunction(Element element) {
    Function function = new Function(element.getAttributeValue("name"));
    function.getBody().clear();//w w  w  . java2 s.  com
    String bodyStr = element.getChild("body").getText();
    String[] lines = bodyStr.split("\n");
    for (String line : lines)
        function.getBody().add(line + "\n");
    return function;
}

From source file:com.khodev.oradiff.io.XmlSchemaReader.java

License:Open Source License

private DBPackage getPackage(Element element) {
    DBPackage pkg = new DBPackage(element.getAttributeValue("name"));
    pkg.getDeclaration().clear();/* w w w.  j a  v a2 s  .  c o  m*/
    String declarationStr = element.getChild("declaration").getText();
    String[] lines = declarationStr.split("\n");
    for (String line : lines)
        pkg.getDeclaration().add(line + "\n");
    pkg.getBody().clear();
    String bodyStr = element.getChild("body").getText();
    lines = bodyStr.split("\n");
    for (String line : lines)
        pkg.getBody().add(line + "\n");
    return pkg;
}

From source file:com.khodev.oradiff.io.XmlSchemaReader.java

License:Open Source License

private Table getTable(Element element) {
    Table table = new Table("", element.getAttributeValue("name"),
            ReplaceManager.getManager("tablespaces").getSubstitute(element.getAttributeValue("tablespace")),
            element.getAttributeValue("comments"));

    table.getColumns().clear();//from ww w  .  j  a  v a  2  s .  c om
    List<Element> children = getChildren(element, "columns");
    for (Element child : children) {
        table.getColumns().add(getColumn(table, child));
    }

    table.getIndexes().clear();
    children = getChildren(element, "indexes");
    for (Element child : children) {
        table.getIndexes().add(getIndex(table, child));
    }

    table.getConstraints().clear();
    children = getChildren(element, "constraints");
    for (Element child : children) {
        table.getConstraints().add(getConstraint(table, child));
    }

    table.getGrants().clear();
    children = getChildren(element, "grants");
    for (Element child : children) {
        table.getGrants().add(getGrant(table, child));
    }

    table.getPublicSynonyms().clear();
    children = getChildren(element, "publicSynonyms");
    for (Element child : children) {
        table.getPublicSynonyms().add(getPublicSynonym(table, child));
    }

    return table;
}