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:com.sofrecom.MybatisGenXmlHandler.java

private void modifyDriverPath(Document document) {
    final Element javaClientElement = (Element) document.getRootElement()
            .selectSingleNode("/generatorConfiguration/classPathEntry");
    final Attribute locationAttribute = javaClientElement.attribute("location");
    locationAttribute.setValue(Configuration.getProperty(Configuration.JDBCDriverPathField));
}

From source file:com.sofrecom.MybatisGenXmlHandler.java

private void modifyJavaModelElement(Document document) {
    Element javaModelGeneratorElement = (Element) document.getRootElement()
            .selectSingleNode("/generatorConfiguration/context/javaModelGenerator");
    final Attribute targetpackage = javaModelGeneratorElement.attribute("targetPackage");
    final Attribute targetProjectPath = javaModelGeneratorElement.attribute("targetProject");
    targetProjectPath.setValue(Configuration.getProperty(Configuration.TargetProjectPathField));
    targetpackage.setValue(Configuration.getProperty(Configuration.JavaModelTargetPathField));
}

From source file:com.sofrecom.MybatisGenXmlHandler.java

private void modifyJDBCElement(Document document) {
    Element javaModelGeneratorElement = (Element) document.getRootElement()
            .selectSingleNode("/generatorConfiguration/context/jdbcConnection");
    final Attribute driverClass = javaModelGeneratorElement.attribute("driverClass");
    final Attribute connectionURL = javaModelGeneratorElement.attribute("connectionURL");
    final Attribute userId = javaModelGeneratorElement.attribute("userId");
    final Attribute password = javaModelGeneratorElement.attribute("password");
    driverClass.setValue(Configuration.getProperty(Configuration.JdbcDriver));
    connectionURL.setValue(Configuration.getProperty(Configuration.DataBaseCnxUrl));
    userId.setValue(Configuration.getProperty(Configuration.DBUser));
    password.setValue(Configuration.getProperty(Configuration.DBPassword));

}

From source file:com.sofrecom.MybatisGenXmlHandler.java

private void modifyTableElement(Document document) {
    Element tableElement = (Element) document.getRootElement()
            .selectSingleNode("/generatorConfiguration/context/table");
    final Attribute tableNameAttribute = tableElement.attribute("tableName");
    tableNameAttribute.setValue(tableName);

}

From source file:com.stratumsoft.xmlgen.SchemaTypeXmlGenerator.java

License:Open Source License

/**
 * Add the element to the parent branch. The min number of times to add it is determined by the element's minOccurs
 * value and the max no. of times to add it is determined by the minimum of the max repeating elements option
 * and the element's maxOccurs value//from w  w  w  .  j  av  a 2 s  . c o  m
 * <p/>
 * Note: the element may again be added multiple times based on its container minOccurs and maxOccurs values
 *
 * @param branch
 * @param elemToAdd
 * @param schElemOfElemToAdd
 */

private void addElement(Branch branch, Element elemToAdd, XmlSchemaElement schElemOfElemToAdd) {

    long minCount = schElemOfElemToAdd.getMinOccurs();
    long maxCount = schElemOfElemToAdd.getMaxOccurs();

    //determine how many times to add this element to the parent
    if (branch != null) {

        if (maxCount > 0) {
            long maxEls = getMaxElementsToGenerate(minCount, maxCount);

            for (long i = 1; i <= maxEls; i++) {
                if (i > minCount) { //anything > the min count but < max is optional
                    if (options.isGenCommentsForParticles()) {
                        branch.add(factory.createComment("optional"));
                    }
                }
                logger.trace("Adding dom4j element: {} to branch: {}", elemToAdd.getName(), branch.getName());
                branch.add(elemToAdd);

                elemToAdd = elemToAdd.createCopy(); //cannot add same element again, so create a copy

                //if id attr is present need to set a new value for it in the copied el
                Attribute idAttr = elemToAdd.attribute("id"); //NON-NLS
                if (idAttr != null) {
                    idAttr.setValue(SampleValueProvider.get(Constants.XSD_ID));
                }
            }
        }
    }

}

From source file:com.stratumsoft.xmlgen.SchemaTypeXmlGenerator.java

License:Open Source License

private void addRestrictedAttributesToElement(Element elementToAddOn, XmlSchemaAttribute attribute) {
    logger.debug("Adding restricted attribute: {}", attribute.getName());
    QName attrQName = attribute.getQName();
    if (attrQName != null) {
        org.dom4j.QName dom4jQName = createDom4jQName(attrQName, attribute.getForm());
        Attribute attr = elementToAddOn.attribute(dom4jQName);
        if (attr != null) {
            //already exists, so remove it, so we can add the restricted version of it
            logger.debug("Removing existing attribute\\: {} to add the restricted attribute",
                    dom4jQName.getName());
            elementToAddOn.remove(attr);
        }//from   ww  w.jav a 2  s. co  m

        attr = factory.createAttribute(elementToAddOn, dom4jQName, "");
        elementToAddOn.add(attr);
    }

}

From source file:com.stratumsoft.xmlgen.SchemaTypeXmlGenerator.java

License:Open Source License

/**
 * Handle the complex type simple content restriction
 *
 * @param restriction/*from  ww w  .j a v  a  2 s.  c o m*/
 * @param dom4jEl
 */
private void handleSimpleContentRestriction(XmlSchemaSimpleContentRestriction restriction, Element dom4jEl) {
    logger.debug("Handling simple content restriction");

    //        restriction.getBaseType(); //todo: process the base schema simpletype if present

    QName baseTypeName = restriction.getBaseTypeName();
    logger.debug("Simple content restriction base type name = {}", baseTypeName);

    XmlSchemaType type = schemaColl.getTypeByQName(baseTypeName);
    if (type != null) {
        if (type instanceof XmlSchemaSimpleType) {
            logger.trace("Simple content restriction base type is simple type");
            handleSimpleType((XmlSchemaSimpleType) type, dom4jEl);
        } else if (type instanceof XmlSchemaComplexType) {
            logger.trace("Simple content restriction base type is complex type");
            handleComplexType((XmlSchemaComplexType) type, dom4jEl);

            //for restriction if there are any attributes specified, they are restrictions of the
            //base complex type attributes - so removing any attributes added in the prev step
            //and specified again and re-process it

            List<XmlSchemaAttributeOrGroupRef> attributeOrGroupRefs = restriction.getAttributes();
            if (attributeOrGroupRefs != null) {
                logger.debug("simple content restriction is overriding base type attributes");

                //collect all attributes that are being overridden
                List<XmlSchemaAttribute> attributes = new ArrayList<>();

                for (XmlSchemaAttributeOrGroupRef o : attributeOrGroupRefs) {

                    if (o instanceof XmlSchemaAttribute) {
                        attributes.add((XmlSchemaAttribute) o);

                    } else if (o instanceof XmlSchemaAttributeGroupRef) {

                        XmlSchemaRef<XmlSchemaAttributeGroup> ref = ((XmlSchemaAttributeGroupRef) o).getRef();
                        if (ref != null) {
                            XmlSchemaAttributeGroup attrGroup = ref.getTarget();
                            if (attrGroup != null) {
                                List<XmlSchemaAttributeGroupMember> attrGroupMembers = attrGroup
                                        .getAttributes();
                                for (XmlSchemaAttributeGroupMember attrGroupMember : attrGroupMembers) {
                                    if (attrGroupMember instanceof XmlSchemaAttribute) {
                                        attributes.add((XmlSchemaAttribute) attrGroupMember);
                                    }
                                    //else todo: check if we need to handle group refs within group?
                                }
                            }
                        }

                    } // end else if
                } //end while

                //now if any attribute with the same name as the overridden attribute was added from the base type
                //remove it and add it again
                for (XmlSchemaAttribute schAttr : attributes) {
                    QName qname = schAttr.getQName();
                    org.dom4j.QName dom4jQName = createDom4jQName(qname, schAttr.getForm());
                    logger.debug("Created dom4j qname: {} from XmlSchema qname: {}", dom4jQName, qname);

                    //remove this attribute
                    Attribute dom4jAttr = dom4jEl.attribute(dom4jQName);
                    dom4jEl.remove(dom4jAttr);

                    //handle this attribute again - but this time with the restrictions defined for it
                    handleAttribute(schAttr, dom4jEl);
                }

            } //end if

        } //end else if
    } //end if
}

From source file:com.sun.tools.xjc.reader.dtd.bindinfo.BIElement.java

License:Open Source License

/**
 * Wraps a given &lt;element> element in the binding file.
 * /*from  ww w . j  av  a 2s.  c om*/
 * <p>
 * Should be created only from {@link BindInfo}.
 */
BIElement(BindInfo bi, Element _e) {
    this.parent = bi;
    this.e = _e;

    {
        Element c = e.element("content");
        if (c != null) {
            if (c.attribute("property") != null) {
                // if @property is there, this is a general declaration
                this.rest = BIContent.create(c, this);
            } else {
                // this must be a model-based declaration
                Iterator itr = c.elementIterator();
                while (itr.hasNext()) {
                    Element p = (Element) itr.next();
                    if (p.getName().equals("rest"))
                        this.rest = BIContent.create(p, this);
                    else
                        this.contents.add(BIContent.create(p, this));
                }
            }
        }
    }

    // parse <attribute>s
    Iterator itr = e.elementIterator("attribute");
    while (itr.hasNext()) {
        BIAttribute a = new BIAttribute(this, (Element) itr.next());
        attributes.put(a.name(), a);
    }

    if (isClass()) {
        // if this is a class-declaration, create JClass object now
        String className = e.attributeValue("class");
        if (className == null)
            // none was specified. infer the name.
            className = parent.nameConverter.toClassName(name());
        clazz = parent.classFactory.createInterface(parent.getTargetPackage(), className, null); // TODO: source location support
    } else {
        // this is not an element-class declaration
        clazz = null;
    }

    // process conversion declarations
    itr = e.elementIterator("conversion");
    while (itr.hasNext()) {
        BIConversion c = new BIUserConversion(bi, (Element) itr.next());
        conversions.put(c.name(), c);
    }
    itr = e.elementIterator("enumeration");
    while (itr.hasNext()) {
        BIConversion c = BIEnumeration.create((Element) itr.next(), this);
        conversions.put(c.name(), c);
    }

    // parse <constructor>s
    itr = e.elementIterator("constructor");
    while (itr.hasNext())
        constructors.add(new BIConstructor((Element) itr.next()));
}

From source file:com.sun.tools.xjc.reader.dtd.bindinfo.BIInterface.java

License:Open Source License

BIInterface(Element e) {
    this.dom = e;
    name = e.attributeValue("name");
    members = parseTokens(e.attributeValue("members"));

    if (e.attribute("properties") != null) {
        fields = parseTokens(e.attributeValue("properties"));
        throw new JAXBAssertionError("//interface/@properties is not supported");
    } else // no property was specified
        fields = new String[0];
}

From source file:com.sun.tools.xjc.reader.dtd.bindinfo.DOM4JLocator.java

License:Open Source License

/**
 * Gets the location information from an element.
 * /* ww  w .j a v  a2s  .  co  m*/
 * <p>
 * For this method to work, the setLocationInfo method has to be
 * called before.
 */
public static Locator getLocationInfo(final Element e) {
    if (e.attribute(QName.get(systemId, locationNamespace)) == null)
        return null; // no location information

    return new Locator() {
        public int getLineNumber() {
            return Integer.parseInt(e.attributeValue(QName.get(line, locationNamespace)));
        }

        public int getColumnNumber() {
            return Integer.parseInt(e.attributeValue(QName.get(column, locationNamespace)));
        }

        public String getSystemId() {
            return e.attributeValue(QName.get(systemId, locationNamespace));
        }

        // we are not interested in PUBLIC ID.
        public String getPublicId() {
            return null;
        }
    };
}