Example usage for org.dom4j Element add

List of usage examples for org.dom4j Element add

Introduction

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

Prototype

void add(Namespace namespace);

Source Link

Document

Adds the given Namespace to this element.

Usage

From source file:com.xpn.xwiki.objects.ListProperty.java

License:Open Source License

/**
 * {@inheritDoc}//from w w  w  . j  a  va  2s . c  om
 * 
 * @see com.xpn.xwiki.objects.BaseProperty#toXML()
 */
@Override
public Element toXML() {
    Element el = new DOMElement(getName());
    List<String> list = getList();
    for (String value : list) {
        if (value != null) {
            Element vel = new DOMElement("value");
            vel.setText(value);
            el.add(vel);
        }
    }

    return el;
}

From source file:com.xpn.xwiki.stats.impl.XWikiStats.java

License:Open Source License

/**
 * {@inheritDoc}//  w ww  .  ja v  a2s  . com
 * 
 * @see com.xpn.xwiki.objects.BaseCollection#toXML(com.xpn.xwiki.objects.classes.BaseClass)
 */
@Override
public Element toXML(BaseClass bclass) {
    Element oel = new DOMElement(XMLNODE_OBJECT);

    // Add Class
    if (bclass != null) {
        if (bclass.getFieldList().size() > 0) {
            oel.add(bclass.toXML());
        }
    }

    Element el = new DOMElement(XMLNODE_NAME);
    el.addText(getName());
    oel.add(el);

    el = new DOMElement(XMLNODE_NUMBER);
    el.addText(getNumber() + "");
    oel.add(el);

    el = new DOMElement(XMLNODE_CLASSNAME);
    el.addText(getClassName());
    oel.add(el);

    for (Iterator<?> it = getFieldList().iterator(); it.hasNext();) {
        Element pel = new DOMElement(XMLNODE_PROPERTY);
        PropertyInterface bprop = (PropertyInterface) it.next();
        pel.add(bprop.toXML());
        oel.add(pel);
    }

    return oel;
}

From source file:com.xpn.xwiki.tool.xar.XarMojo.java

License:Open Source License

/**
 * Generate a DOM4J Document containing the generated XML.
 * /*from ww w  .  jav  a 2 s . c o m*/
 * @param files the list of files that we want to include in the generated package XML file.
 * @return the DOM4J Document containing the generated XML
 */
private Document toXML(Collection<ArchiveEntry> files) {
    Document doc = new DOMDocument();

    Element packageElement = new DOMElement("package");
    doc.setRootElement(packageElement);

    Element infoElement = new DOMElement("infos");
    packageElement.add(infoElement);
    addInfoElements(infoElement);

    Element filesElement = new DOMElement(FILES_TAG);
    packageElement.add(filesElement);
    addFileElements(files, filesElement);

    return doc;
}

From source file:com.xpn.xwiki.tool.xar.XarMojo.java

License:Open Source License

/**
 * Add all the XML elements under the &lt;info&gt; element (name, description, license, author, version and whether
 * it's a backup pack or not).//from  w w w  . jav  a 2s .  com
 * 
 * @param infoElement the info element to which to add to
 */
private void addInfoElements(Element infoElement) {
    Element el = new DOMElement("name");
    el.addText(this.project.getName());
    infoElement.add(el);

    el = new DOMElement("description");
    String description = this.project.getDescription();
    if (description == null) {
        el.addText("");
    } else {
        el.addText(description);
    }
    infoElement.add(el);

    el = new DOMElement("licence");
    el.addText("");
    infoElement.add(el);

    el = new DOMElement("author");
    el.addText("XWiki.Admin");
    infoElement.add(el);

    el = new DOMElement("version");
    el.addText(this.project.getVersion());
    infoElement.add(el);

    el = new DOMElement("backupPack");
    el.addText("true");
    infoElement.add(el);
}

From source file:com.xpn.xwiki.tool.xar.XarMojo.java

License:Open Source License

/**
 * Add all the XML elements under the &lt;files&gt; element (the list of files present in the XAR).
 * /*from   ww  w.j  a  v a  2s  .  c o m*/
 * @param files the list of files that we want to include in the generated package XML file.
 * @param filesElement the files element to which to add to
 */
private void addFileElements(Collection<ArchiveEntry> files, Element filesElement) {
    for (ArchiveEntry entry : files) {
        // Don't add files in META-INF to the package.xml file
        if (entry.getName().indexOf("META-INF") == -1) {
            XWikiDocument xdoc = getDocFromXML(entry.getFile());
            if (xdoc != null) {
                String fullName = xdoc.getFullName();
                Element element = new DOMElement(FILE_TAG);
                element.setText(fullName);
                element.addAttribute("language", xdoc.getLanguage());
                element.addAttribute("defaultAction", "0");
                filesElement.add(element);
            }
        }
    }
}

From source file:com.zimbra.cs.dav.property.ResourceProperty.java

License:Open Source License

public Element toElement(DavContext ctxt, Element parent, boolean nameOnly) {
    Element elem = parent.addElement(mName);
    if (nameOnly) {
        return elem;
    }/*from  w  ww  . ja v a 2  s.  co  m*/

    if (mValue != null) {
        if (mLocale != null) {
            elem.addAttribute(DavElements.E_LANG, mLocale.toString());
        }
        elem.setText(mValue);
    } else {
        for (Element child : mChildren) {
            elem.add(child.createCopy());
        }
    }
    return elem;
}

From source file:com.zimbra.cs.dav.property.ResourceProperty.java

License:Open Source License

public Element toElement(boolean nameOnly) {
    Element elem = org.dom4j.DocumentHelper.createElement(mName);
    if (nameOnly) {
        return elem;
    }//w  w w  . ja  va  2s . c o m

    if (mValue != null) {
        if (mLocale != null) {
            elem.addAttribute(DavElements.E_LANG, mLocale.toString());
        }
        elem.setText(mValue);
    } else {
        for (Element child : mChildren) {
            elem.add(child.createCopy());
        }
    }
    return elem;
}

From source file:com.zimbra.cs.dav.resource.CalendarProxyReadFor.java

License:Open Source License

@Override
public Element toElement(DavContext ctxt, Element parent, boolean nameOnly) {
    Element proxy = super.toElement(ctxt, parent, true);
    if (nameOnly) {
        return proxy;
    }//from www.j a  v  a2 s.  c  om
    ArrayList<Pair<Mountpoint, ZFolder>> mps = getMountpoints(ctxt);
    HashSet<Account> writeProxies = new HashSet<Account>();
    HashMap<Account, Element> proxies = new HashMap<Account, Element>();
    for (Pair<Mountpoint, ZFolder> folder : mps) {
        try {
            short rights = ACL.stringToRights(folder.getSecond().getEffectivePerms());
            Account owner = Provisioning.getInstance().get(AccountBy.id, folder.getFirst().getOwnerId());
            if (owner == null) {
                continue;
            }
            if ((rights & ACL.RIGHT_WRITE) > 0) {
                writeProxies.add(owner);
                proxies.remove(owner);
            }
            if ((rights & ACL.RIGHT_WRITE) == 0 && (rights & ACL.RIGHT_READ) > 0) {
                if (!writeProxies.contains(owner) && !proxies.containsKey(owner)) {
                    Element e = DocumentHelper.createElement(DavElements.E_HREF);
                    e.setText(UrlNamespace.getPrincipalUrl(account, owner));
                    proxies.put(owner, e);
                }
            }
        } catch (ServiceException se) {
            ZimbraLog.dav.warn("can't convert rights", se);
        }
    }
    for (Element e : proxies.values()) {
        proxy.add(e);
    }
    return proxy;
}

From source file:com.zimbra.cs.dav.resource.MailItemResource.java

License:Open Source License

@Override
public void patchProperties(DavContext ctxt, java.util.Collection<Element> set,
        java.util.Collection<QName> remove) throws DavException, IOException {
    List<QName> reqProps = new ArrayList<QName>();
    for (QName n : remove) {
        mDeadProps.remove(n);/*from   w ww  . jav  a  2s .  c  om*/
        reqProps.add(n);
    }
    for (Element e : set) {
        QName name = e.getQName();
        if (name.equals(DavElements.E_DISPLAYNAME)
                && (type == MailItem.Type.FOLDER || type == MailItem.Type.MOUNTPOINT)) {
            // rename folder
            try {
                String val = e.getText();
                String uri = getUri();
                Mailbox mbox = getMailbox(ctxt);
                mbox.rename(ctxt.getOperationContext(), mId, type, val, mFolderId);
                setProperty(DavElements.P_DISPLAYNAME, val);
                UrlNamespace.addToRenamedResource(getOwner(), uri, this);
                UrlNamespace.addToRenamedResource(getOwner(), uri.substring(0, uri.length() - 1), this);
            } catch (ServiceException se) {
                ctxt.getResponseProp().addPropError(DavElements.E_DISPLAYNAME,
                        new DavException(se.getMessage(), DavProtocol.STATUS_FAILED_DEPENDENCY));
            }
            mDeadProps.remove(name);
            continue;
        } else if (name.equals(DavElements.E_CALENDAR_COLOR)
                && (type == MailItem.Type.FOLDER || type == MailItem.Type.MOUNTPOINT)) {
            // change color
            String colorStr = e.getText();
            Color color = new Color(colorStr.substring(0, 7));
            byte col = (byte) COLOR_LIST.indexOf(colorStr);
            if (col >= 0)
                color.setColor(col);
            try {
                Mailbox mbox = getMailbox(ctxt);
                mbox.setColor(ctxt.getOperationContext(), new int[] { mId }, type, color);
            } catch (ServiceException se) {
                ctxt.getResponseProp().addPropError(DavElements.E_CALENDAR_COLOR,
                        new DavException(se.getMessage(), DavProtocol.STATUS_FAILED_DEPENDENCY));
            }
            mDeadProps.remove(name);
            continue;
        } else if (name.equals(DavElements.E_SUPPORTED_CALENDAR_COMPONENT_SET)) {
            // change default view
            @SuppressWarnings("unchecked")
            List<Element> elements = e.elements(DavElements.E_COMP);
            boolean isTodo = false;
            boolean isEvent = false;
            for (Element element : elements) {
                Attribute attr = element.attribute(DavElements.P_NAME);
                if (attr != null && CalComponent.VTODO.name().equals(attr.getValue())) {
                    isTodo = true;
                } else if (attr != null && CalComponent.VEVENT.name().equals(attr.getValue())) {
                    isEvent = true;
                }
            }
            if (isEvent ^ isTodo) { // we support a calendar collection of type event or todo, not both or none.
                Type type = (isEvent) ? Type.APPOINTMENT : Type.TASK;
                try {
                    Mailbox mbox = getMailbox(ctxt);
                    mbox.setFolderDefaultView(ctxt.getOperationContext(), mId, type);
                    // Update the view for this collection. This collection may get cached if display name is modified.
                    // See UrlNamespace.addToRenamedResource()
                    if (this instanceof Collection) {
                        ((Collection) this).view = type;
                    }
                } catch (ServiceException se) {
                    ctxt.getResponseProp().addPropError(name,
                            new DavException(se.getMessage(), DavProtocol.STATUS_FAILED_DEPENDENCY));
                }
            } else {
                ctxt.getResponseProp().addPropError(name, new DavException.CannotModifyProtectedProperty(name));
            }
            continue;
        }
        mDeadProps.put(name, e);
        reqProps.add(name);
    }
    String configVal = "";
    if (mDeadProps.size() > 0) {
        org.dom4j.Document doc = org.dom4j.DocumentHelper.createDocument();
        Element top = doc.addElement(CONFIG_KEY);
        for (Map.Entry<QName, Element> entry : mDeadProps.entrySet())
            top.add(entry.getValue().detach());

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        OutputFormat format = OutputFormat.createCompactFormat();
        XMLWriter writer = new XMLWriter(out, format);
        writer.write(doc);
        configVal = new String(out.toByteArray(), "UTF-8");

        if (configVal.length() > PROP_LENGTH_LIMIT)
            for (Map.Entry<QName, Element> entry : mDeadProps.entrySet())
                ctxt.getResponseProp().addPropError(entry.getKey(),
                        new DavException("prop length exceeded", DavProtocol.STATUS_INSUFFICIENT_STORAGE));
    }
    Mailbox mbox = null;
    try {
        mbox = getMailbox(ctxt);
        mbox.lock.lock();
        Metadata data = mbox.getConfig(ctxt.getOperationContext(), CONFIG_KEY);
        if (data == null) {
            data = new Metadata();
        }
        data.put(Integer.toString(mId), configVal);
        mbox.setConfig(ctxt.getOperationContext(), CONFIG_KEY, data);
    } catch (ServiceException se) {
        for (QName qname : reqProps)
            ctxt.getResponseProp().addPropError(qname,
                    new DavException(se.getMessage(), HttpServletResponse.SC_FORBIDDEN));
    } finally {
        if (mbox != null)
            mbox.lock.release();
    }
}

From source file:com.zimbra.cs.dav.service.method.AclReports.java

License:Open Source License

/**
 * http://tools.ietf.org/html/rfc3744#section-9.5 DAV:principal-search-property-set REPORT
 *     This report is only defined when the Depth header has value "0";
 *     other values result in a 400 (Bad Request) error response.
 *///from   w  w w.ja  va2 s.  co m
private void handlePrincipalSearchPropertySet(DavContext ctxt, Element query)
        throws DavException, ServiceException {
    if (ctxt.getDepth() != Depth.zero) {
        throw new DavException.REPORTwithDisallowedDepthException(query.getQName().getName(), ctxt.getDepth());
    }
    Element response = ctxt.getDavResponse().getTop(DavElements.E_PRINCIPAL_SEARCH_PROPERTY_SET);
    ctxt.setStatus(HttpServletResponse.SC_OK);
    for (Pair<QName, Element> prop : PRINCIPAL_SEARCH_PROPERTIES) {
        Element searchProp = response.addElement(DavElements.E_PRINCIPAL_SEARCH_PROPERTY);
        searchProp.addElement(DavElements.E_PROP).addElement(prop.getFirst());
        searchProp.add(prop.getSecond().createCopy());
    }
}