Example usage for org.dom4j Element addAttribute

List of usage examples for org.dom4j Element addAttribute

Introduction

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

Prototype

Element addAttribute(QName qName, String value);

Source Link

Document

Adds the attribute value of the given fully qualified name.

Usage

From source file:com.beetle.framework.util.file.XMLReader.java

License:Apache License

public static void setProperties(String xmlFileName, String itemPath, String ElementName, String keyName,
        String valueName, String key, String value) {
    // ? ??????/*from w  w w.  j  av a  2  s .co m*/
    // ????
    synchronized (writeLock) {
        SAXReader reader = new SAXReader();
        XMLWriter writer = null;
        try {
            Document doc = reader.read(new File(xmlFileName));
            Node node = doc.selectSingleNode(convertPath(itemPath));
            if (node != null) {
                Iterator<?> it = node.selectNodes(ElementName).iterator();
                while (it.hasNext()) {
                    Element e = (Element) it.next();
                    if (e.attributeValue(keyName).equals(key)) {
                        e.addAttribute(valueName, value);
                        break;
                    }
                }
            }
            writer = new XMLWriter(new FileWriter(xmlFileName));
            writer.write(doc);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
            reader = null;
        }
    }
}

From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java

License:Open Source License

/**
 * //from  www .j ava  2 s. co  m
 * 
 * 
 * 
 * 
 * @param ele
 *            Element
 * @param attributeName
 *            String
 * @param attributeValue
 *            String
 * @return Element
 */
public static Element appendAttribute(Element ele, String attributeName, String attributeValue) {
    if (ele == null) {
        return null;
    }

    ele.addAttribute(attributeName, attributeValue);
    return ele;
}

From source file:com.bsoft.baseframe.baseframe_utils.beanUtils.CreateXMLFile.java

License:Open Source License

/**
 * xml??//from ww  w  . ja v  a  2s  .c o m
 * @return ??
 */
public boolean createXMLFile() {
    String clsName = FileUtils.getClassName(tbName);
    String fileName = path + clsName + ".xml";
    String beanName = beanPkg + "." + clsName;

    List<GenaBean> list = GenaBeanDBUtils.getFields(tbName);
    if (list.size() == 0) {
        return false;
    }
    StringBuffer fields = new StringBuffer();
    StringBuffer values = new StringBuffer();
    GenaBean gb = null;
    String fn = null;
    for (int i = 0; i < list.size(); i++) {
        gb = list.get(i);
        fn = gb.getColumnName();
        fields.append(fn);
        if ("delflag".equals(fn)) {
            values.append("'0'");
        } else if ("ctb1".equals(fn)) {
            values.append("getdate()");
        } else {
            values.append("#").append(fn).append("#");
        }
        if (i != list.size() - 1) {
            fields.append(", ");
            values.append(", ");
        }
    }
    System.out.println(fields.toString());
    System.out.println(values.toString());

    //Document
    Document document = DocumentHelper.createDocument();

    //
    document.addDocType("sqlMap", "-//ibatis.apache.org//DTD SQL Map 2.0//EN",
            "http://ibatis.apache.org/dtd/sql-map-2.dtd");

    // document
    Element rootElement = document.addElement("sqlMap");

    //??
    rootElement.addAttribute("namespace", clsName.toUpperCase());

    //insert
    Element insertNode = rootElement.addElement("insert");
    insertNode.addAttribute("id", "add" + clsName);
    insertNode.addAttribute("parameterClass", beanName);
    insertNode.setText("insert into " + tbName + "(" + fields + ") values (" + values + ")");

    //?
    Element removeNode = rootElement.addElement("delete");
    removeNode.addAttribute("id", "remove" + clsName);
    removeNode.addAttribute("parameterClass", beanName);
    removeNode.setText("delete from " + tbName + " where id = #id#");

    //
    Element deleteNode = rootElement.addElement("update");
    deleteNode.addAttribute("id", "del" + clsName);
    deleteNode.addAttribute("parameterClass", beanName);
    deleteNode.setText("update " + tbName + " set delflag=#delflag# where id = #id#");

    //selectOne
    Element selectOneNode = rootElement.addElement("select");
    selectOneNode.addAttribute("id", "getOne" + clsName);
    selectOneNode.addAttribute("parameterClass", beanName);
    selectOneNode.addAttribute("resultClass", beanName);
    selectOneNode.setText("select *  from " + tbName + " where id = #id# ");

    //select
    Element selectNode = rootElement.addElement("select");
    selectNode.addAttribute("id", "queryCount");
    selectNode.addAttribute("resultClass", "int");
    selectNode.setText("select count(*) as countnum from " + tbName + " where 1=1");

    //select
    Element selectNode2 = rootElement.addElement("select");
    selectNode2.addAttribute("id", "list" + clsName);
    selectNode2.addAttribute("resultClass", beanName);
    selectNode2.addAttribute("parameterClass", "java.util.Map");
    selectNode2.setText("select * from (select top  $endNum$ row_number()over(order by TT.id) as RN,* from"
            + "(SELECT * FROM " + tbName + " where 1=1) as TT)as H where RN > #startNum#");

    //update
    Element updateNode = rootElement.addElement("update");
    updateNode.addAttribute("id", "upSap" + clsName);
    updateNode.addAttribute("parameterClass", beanName);
    updateNode.setText("UPDATE " + tbName + " ");
    Element erElement = updateNode.addElement("dynamic");
    erElement.addAttribute("prepend", "set");
    for (int i = 0; i < list.size(); i++) {
        gb = list.get(i);
        fn = gb.getColumnName();
        if (!"id".equals(fn)) {
            Element saElement = erElement.addElement("isNotNull");
            saElement.addAttribute("property", fn);
            saElement.addAttribute("removeFirstPrepend", "true");
            saElement.addAttribute("prepend", ",");
            saElement.setText(fn + "=#" + fn + "#");
        }
    }
    updateNode.addText(" where ");
    Element terElement = updateNode.addElement("isNotNull");
    terElement.addAttribute("property", "id");
    terElement.setText(" id = #id# ");

    //update
    Element updateSimpleNode = rootElement.addElement("update");
    updateSimpleNode.addAttribute("id", "update" + clsName);
    updateSimpleNode.addAttribute("parameterClass", beanName);

    StringBuffer sb = new StringBuffer();
    sb.append("update ").append(tbName).append(" set ");

    for (int i = 0; i < list.size(); i++) {
        gb = list.get(i);
        fn = gb.getColumnName();
        if (!"id".equals(fn)) {
            if ("ctb3".equals(fn)) {
                sb.append(fn).append("=").append("getdate()");
            } else {
                sb.append(fn).append("=#").append(fn).append("#");
            }
            if (i != list.size() - 1) {
                sb.append(", ");
            }

        }
    }
    sb.append(" where id = #id# ");
    updateSimpleNode.setText(sb.toString());
    return FileUtils.wrieteXML2Doc(document, new File(fileName));
}

From source file:com.bstek.dorado.idesupport.output.RuleSetOutputter.java

License:Open Source License

public void output(Writer writer, RuleTemplateManager ruleTemplateManager) throws Exception {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setEncoding(Constants.DEFAULT_CHARSET);

    XMLWriter xmlWriter = new XMLWriter(writer, format);
    xmlWriter.startDocument();//from   w  ww  .j  ava  2 s . com

    Element rootElement = DocumentHelper.createElement("RuleSet");
    rootElement.addAttribute("version", ruleTemplateManager.getVersion());
    xmlWriter.writeOpen(rootElement);

    OutputContext context = new OutputContext();
    outputPackageInfos(xmlWriter, ruleTemplateManager, context);

    for (RuleTemplate ruleTemplate : ruleTemplateManager.getRuleTemplates()) {
        // PropertyDataType?
        // if (ruleTemplate.isAbstract()
        // && ruleTemplate.getSubRuleTemplates().length == 0) {
        // continue;
        // }
        outputRuleTemplate(xmlWriter, ruleTemplate, context);
    }

    xmlWriter.writeClose(rootElement);
    xmlWriter.endDocument();
    xmlWriter.close();
}

From source file:com.bstek.dorado.idesupport.output.RuleSetOutputter.java

License:Open Source License

protected void outputRuleTemplate(XMLWriter xmlWriter, RuleTemplate ruleTemplate, OutputContext context)
        throws Exception {
    Element element = DocumentHelper.createElement("Rule");
    setElementAttribute(element, ruleTemplate, "name");

    RuleTemplate[] parents = ruleTemplate.getParents();
    if (parents != null && parents.length > 0) {
        String parentsText = "";
        for (RuleTemplate parent : parents) {
            if (parentsText.length() > 0)
                parentsText += ',';
            parentsText += parent.getName();
        }/*w  ww.  j  a  va2  s.c  o  m*/
        element.addAttribute("parents", parentsText);
    }

    setElementAttributes(element, ruleTemplate,
            "label,abstract,nodeName,type,scope,sortFactor,category,robots,icon,labelProperty,autoGenerateId,clientTypes,deprecated,reserve");
    if (!ruleTemplate.isVisible()) {
        element.addAttribute("visible", "false");
    }

    xmlWriter.writeOpen(element);

    Collection<PropertyTemplate> primitiveProperties = ruleTemplate.getPrimitiveProperties().values();
    if (!primitiveProperties.isEmpty()) {
        Element subElement = DocumentHelper.createElement("PrimitiveProps");
        xmlWriter.writeOpen(subElement);
        for (PropertyTemplate property : primitiveProperties) {
            outputProperty(xmlWriter, property, context);
        }
        xmlWriter.writeClose(subElement);
    }

    Collection<PropertyTemplate> properties = ruleTemplate.getProperties().values();
    if (!properties.isEmpty()) {
        Element subElement = DocumentHelper.createElement("Props");
        xmlWriter.writeOpen(subElement);
        for (PropertyTemplate property : properties) {
            outputProperty(xmlWriter, property, context);
        }
        xmlWriter.writeClose(subElement);
    }

    Collection<ClientEvent> clientEvents = ruleTemplate.getClientEvents().values();
    if (!clientEvents.isEmpty()) {
        Element subElement = DocumentHelper.createElement("ClientEvents");
        xmlWriter.writeOpen(subElement);
        for (ClientEvent clientEvent : clientEvents) {
            outputClientEvent(xmlWriter, clientEvent, context);
        }
        xmlWriter.writeClose(subElement);
    }
    Collection<ChildTemplate> children = ruleTemplate.getChildren().values();
    if (!children.isEmpty()) {
        Element subElement = DocumentHelper.createElement("Children");
        xmlWriter.writeOpen(subElement);
        for (ChildTemplate childTemplate : children) {
            outputChildTemplate(xmlWriter, childTemplate, context);
        }
        xmlWriter.writeClose(subElement);
    }

    xmlWriter.writeClose(element);
}

From source file:com.bstek.dorado.idesupport.output.RuleSetOutputter.java

License:Open Source License

protected void outputProperty(XMLWriter xmlWriter, PropertyTemplate property, OutputContext context)
        throws Exception {
    Element element = DocumentHelper.createElement("Prop");
    setElementAttributes(element, property,
            "name,defaultValue,highlight,fixed,enumValues,editor,clientTypes,deprecated,reserve");
    if (!property.isVisible()) {
        element.addAttribute("visible", "false");
    }/* ww w.j  a v  a  2s.  co  m*/

    if (StringUtils.isNotEmpty(property.getType())) {
        try {
            Class<?> type = ClassUtils.forName(property.getType());
            if (!String.class.equals(type) && !type.isEnum()) {
                element.addAttribute("type", type.getName());
            }
        } catch (ClassNotFoundException e) {
            // do nothing
        }
    }

    CompositeType compositeType = property.getCompositeType();
    if (compositeType != CompositeType.Unsupport) {
        element.addAttribute("compositeType", compositeType.toString());
    }

    ReferenceTemplate reference = property.getReference();
    if (reference != null) {
        String referenceText = reference.getRuleTemplate().getName();
        if (StringUtils.isNotEmpty(reference.getProperty())) {
            referenceText += ':' + reference.getProperty();
        }
        element.addAttribute("reference", referenceText);
    }

    if (compositeType == CompositeType.Fixed || compositeType == CompositeType.Open) {
        xmlWriter.writeOpen(element);
        for (PropertyTemplate subProperty : property.getProperties().values()) {
            outputProperty(xmlWriter, subProperty, context);
        }
        xmlWriter.writeClose(element);
    } else {
        xmlWriter.write(element);
    }
}

From source file:com.bstek.dorado.idesupport.output.RuleSetOutputter.java

License:Open Source License

protected void outputChildTemplate(XMLWriter xmlWriter, ChildTemplate childTemplate, OutputContext context)
        throws Exception {
    Element element = DocumentHelper.createElement("Child");

    RuleTemplate ruleTemplate = childTemplate.getRuleTemplate();
    String ruleName = ruleTemplate.getName();
    if (ruleTemplate.isGlobal()) {
        element.addAttribute("rule", ruleName);
    }//from   w  w  w.  j ava2 s. c  o  m

    setElementAttributes(element, childTemplate, "name,fixed,aggregated,clientTypes,deprecated,reserve");
    if (!childTemplate.isPublic()) {
        element.addAttribute("public", Boolean.FALSE.toString());
    }
    if (!childTemplate.isVisible()) {
        element.addAttribute("visible", "false");
    }
    xmlWriter.writeOpen(element);

    if (!ruleTemplate.isGlobal()) {
        outputRuleTemplate(xmlWriter, ruleTemplate, context);
    }
    xmlWriter.writeClose(element);
}

From source file:com.bstek.dorado.idesupport.output.RuleSetOutputter.java

License:Open Source License

private void setElementAttribute(Element element, Object object, String propertyName) throws Exception {
    Object value = PropertyUtils.getProperty(object, propertyName);
    if (value == null || Boolean.FALSE.equals(value)
            || (value instanceof Number && ((Number) value).intValue() == 0)) {
        return;/*from  w ww  .jav a  2  s.  co m*/
    }

    String text = null;
    if (propertyName.equals("clientTypes")) {
        text = ClientType.toString(((Integer) value).intValue());
    } else if (value instanceof String[]) {
        text = StringUtils.join((String[]) value, ',');
    } else if (value instanceof Class<?>) {
        if (value != String.class)
            text = ((Class<?>) value).getName();
    } else {
        text = value.toString();
    }
    if (StringUtils.isEmpty(text)) {
        return;
    }

    element.addAttribute(propertyName, text);
}

From source file:com.buddycloud.channeldirectory.search.handler.common.PostQueryHandler.java

License:Apache License

protected IQ createIQResponse(IQ iq, List<PostData> allContent, RSM rsm) {
    IQ result = IQ.createResultIQ(iq);//www.j  a  v  a 2  s .c  o m

    Element queryEl = iq.getElement().element("query");

    Set<String> options = FeatureUtils.parseOptions(queryEl);

    Element queryElement = result.getElement().addElement("query", getNamespace());

    for (PostData postObject : allContent) {
        Element itemElement = queryElement.addElement("item");

        FeatureUtils.addAttribute(options, itemElement, "id", postObject.getId());
        FeatureUtils.addAttribute(options, itemElement, "type", postObject.getType());

        Element entryElement = itemElement.addElement("entry", ATOM_NAMESPACE);

        FeatureUtils.addElement(options, entryElement, "author", postObject.getAuthor());

        Element contentElement = FeatureUtils.addElement(options, entryElement, "content",
                postObject.getContent());
        if (contentElement != null) {
            contentElement.addAttribute("type", "text");
        }

        if (postObject.getUpdated() != null) {
            FeatureUtils.addElement(options, entryElement, "updated",
                    DATE_FORMAT.format(postObject.getUpdated()));
        }

        if (postObject.getPublished() != null) {
            FeatureUtils.addElement(options, entryElement, "published",
                    DATE_FORMAT.format(postObject.getPublished()));
        }

        FeatureUtils.addElement(options, entryElement, "parent_fullid", postObject.getParentFullId());
        FeatureUtils.addElement(options, entryElement, "parent_simpleid", postObject.getParentSimpleId());

        Element geoElement = FeatureUtils.addNamespaceElement(options, entryElement, "geoloc",
                Geolocation.NAMESPACE);
        GeolocationUtils.appendGeoLocation(geoElement, postObject.getGeolocation());

        Element inReplyEl = FeatureUtils.addNamespaceElement(options, entryElement, "in-reply-to",
                THREAD_NAMESPACE);
        if (inReplyEl != null) {
            inReplyEl.addAttribute("ref", postObject.getInReplyTo());
        }
    }

    RSMUtils.appendRSMElement(queryElement, rsm);

    return result;
}

From source file:com.buddycloud.channeldirectory.search.rsm.RSMUtils.java

License:Apache License

/**
 * Appends RSM info to query response./*  w w w . ja  v a2 s . co  m*/
 * @param queryElement
 * @param rsm
 */
public static void appendRSMElement(Element queryElement, RSM rsm) {
    Element setElement = queryElement.addElement("set", RSM.NAMESPACE);

    if (rsm.getFirst() != null) {
        Element firstElement = setElement.addElement("first");
        firstElement.addAttribute("index", rsm.getIndex().toString());
        firstElement.setText(rsm.getFirst());
    }

    if (rsm.getLast() != null) {
        Element lastElement = setElement.addElement("last");
        lastElement.setText(rsm.getLast());
    }

    setElement.addElement("count").setText(String.valueOf(rsm.getCount()));
}