Example usage for org.dom4j Element getStringValue

List of usage examples for org.dom4j Element getStringValue

Introduction

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

Prototype

String getStringValue();

Source Link

Document

Returns the XPath string-value of this node.

Usage

From source file:org.nuxeo.adullact.importer.ImporterServiceImpl.java

License:Open Source License

protected Object resolveXP(Element el, String xpr) {

    @SuppressWarnings("unchecked")
    List<Object> nodes = el.selectNodes(xpr);
    if (nodes.size() == 1) {
        return nodes.get(0);
    } else if (nodes.size() > 1) {
        // Workaround for NXP-11834
        if (xpr.endsWith("text()")) {
            String value = "";
            for (Object node : nodes) {
                if (!(node instanceof DefaultText)) {
                    String msg = "Text selector must return a string (expr:\"%s\") element %s";
                    log.error(String.format(msg, xpr, el.getStringValue()));
                    return value;
                }//from w w w .ja va  2  s  .c o  m
                value += ((DefaultText) node).getText();
            }
            return new DefaultText(value);
        }
        return nodes;
    }
    return null;
}

From source file:org.nuxeo.ecm.platform.importer.xml.parser.XMLImporterServiceImpl.java

License:Open Source License

protected Object resolveXP(Element el, String xpr) {
    List<Object> nodes = el.selectNodes(xpr);
    if (nodes.size() == 1) {
        return nodes.get(0);
    } else if (nodes.size() > 1) {
        // Workaround for NXP-11834
        if (xpr.endsWith("text()")) {
            String value = "";
            for (Object node : nodes) {
                if (!(node instanceof DefaultText)) {
                    String msg = "Text selector must return a string (expr:\"%s\") element %s";
                    log.error(String.format(msg, xpr, el.getStringValue()));
                    return value;
                }/*from  w w w . j  a va 2  s . c o  m*/
                value += ((DefaultText) node).getText();
            }
            return new DefaultText(value);
        }
        return nodes;
    }
    return null;
}

From source file:org.olat.core.gui.control.generic.textmarker.TextMarker.java

License:Apache License

/**
 * Constructor, used to create an object from an XML element
 * // ww  w .  j av  a2  s  .  co m
 * @param textMarkerElement
 */
public TextMarker(Element textMarkerElement) {
    Element markedTexEl = textMarkerElement.element(XML_MARKED_TEXT_ELEMENT);
    this.markedText = (markedTexEl == null ? null : markedTexEl.getStringValue());
    Element hooverEl = textMarkerElement.element(XML_HOOVER_TEXT_ELEMENT);
    this.hooverText = (hooverEl == null ? null : hooverEl.getStringValue());
    Element cssEl = textMarkerElement.element(XML_CSS_CLASS_ELEMENT);
    this.cssClass = (cssEl == null ? null : cssEl.getStringValue());
}

From source file:org.olat.ims.qti.qpool.QTIImportProcessor.java

License:Apache License

@SuppressWarnings("unchecked")
protected List<String> getMaterials(Element el) {
    List<String> materialPath = new ArrayList<String>();
    //mattext//  ww w .  j  av  a2  s . c o  m
    List<Element> mattextList = el.selectNodes(".//mattext");
    for (Element mat : mattextList) {
        Attribute texttypeAttr = mat.attribute("texttype");
        if (texttypeAttr != null) {
            String texttype = texttypeAttr.getValue();
            if ("text/html".equals(texttype)) {
                String content = mat.getStringValue();
                findMaterialInMatText(content, materialPath);
            }
        }
    }
    //matimage uri
    List<Element> matList = new ArrayList<Element>();
    matList.addAll(el.selectNodes(".//matimage"));
    matList.addAll(el.selectNodes(".//mataudio"));
    matList.addAll(el.selectNodes(".//matvideo"));

    for (Element mat : matList) {
        Attribute uriAttr = mat.attribute("uri");
        String uri = uriAttr.getValue();
        materialPath.add(uri);
    }
    return materialPath;
}

From source file:org.opencms.webdav.CmsWebdavServlet.java

License:Open Source License

/**
 * Process a LOCK WebDAV request for the specified resource.<p>
 * /*from   ww w.  j  a va 2s. co m*/
 * @param req the servlet request we are processing
 * @param resp the servlet response we are creating
 *
 * @throws IOException if an input/output error occurs
 */
@SuppressWarnings("unchecked")
protected void doLock(HttpServletRequest req, HttpServletResponse resp) throws IOException {

    String path = getRelativePath(req);

    // Check if webdav is set to read only
    if (m_readOnly) {

        resp.setStatus(CmsWebdavStatus.SC_FORBIDDEN);

        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_WEBDAV_READ_ONLY_0));
        }

        return;
    }

    // Check if resource is locked
    if (isLocked(req)) {

        resp.setStatus(CmsWebdavStatus.SC_LOCKED);

        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_ITEM_LOCKED_1, path));
        }

        return;
    }

    CmsRepositoryLockInfo lock = new CmsRepositoryLockInfo();

    // Parsing depth header
    String depthStr = req.getHeader(HEADER_DEPTH);
    if (depthStr == null) {
        lock.setDepth(CmsRepositoryLockInfo.DEPTH_INFINITY_VALUE);
    } else {
        if (depthStr.equals("0")) {
            lock.setDepth(0);
        } else {
            lock.setDepth(CmsRepositoryLockInfo.DEPTH_INFINITY_VALUE);
        }
    }

    // Parsing timeout header
    int lockDuration = CmsRepositoryLockInfo.TIMEOUT_INFINITE_VALUE;
    lock.setExpiresAt(System.currentTimeMillis() + (lockDuration * 1000));

    int lockRequestType = LOCK_CREATION;

    Element lockInfoNode = null;

    try {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new InputSource(req.getInputStream()));

        // Get the root element of the document
        Element rootElement = document.getRootElement();
        lockInfoNode = rootElement;
    } catch (Exception e) {
        lockRequestType = LOCK_REFRESH;
    }

    if (lockInfoNode != null) {

        // Reading lock information
        Iterator<Element> iter = lockInfoNode.elementIterator();

        Element lockScopeNode = null;
        Element lockTypeNode = null;
        Element lockOwnerNode = null;

        while (iter.hasNext()) {
            Element currentElem = iter.next();
            switch (currentElem.getNodeType()) {
            case Node.TEXT_NODE:
                break;
            case Node.ELEMENT_NODE:
                String nodeName = currentElem.getName();
                if (nodeName.endsWith(TAG_LOCKSCOPE)) {
                    lockScopeNode = currentElem;
                }
                if (nodeName.endsWith(TAG_LOCKTYPE)) {
                    lockTypeNode = currentElem;
                }
                if (nodeName.endsWith(TAG_OWNER)) {
                    lockOwnerNode = currentElem;
                }
                break;
            default:
                break;
            }
        }

        if (lockScopeNode != null) {

            iter = lockScopeNode.elementIterator();
            while (iter.hasNext()) {
                Element currentElem = iter.next();
                switch (currentElem.getNodeType()) {
                case Node.TEXT_NODE:
                    break;
                case Node.ELEMENT_NODE:
                    String tempScope = currentElem.getName();
                    if (tempScope.indexOf(':') != -1) {
                        lock.setScope(tempScope.substring(tempScope.indexOf(':') + 1));
                    } else {
                        lock.setScope(tempScope);
                    }
                    break;
                default:
                    break;
                }
            }

            if (lock.getScope() == null) {

                // Bad request
                resp.setStatus(CmsWebdavStatus.SC_BAD_REQUEST);
            }

        } else {

            // Bad request
            resp.setStatus(CmsWebdavStatus.SC_BAD_REQUEST);
        }

        if (lockTypeNode != null) {

            iter = lockTypeNode.elementIterator();
            while (iter.hasNext()) {
                Element currentElem = iter.next();
                switch (currentElem.getNodeType()) {
                case Node.TEXT_NODE:
                    break;
                case Node.ELEMENT_NODE:
                    String tempType = currentElem.getName();
                    if (tempType.indexOf(':') != -1) {
                        lock.setType(tempType.substring(tempType.indexOf(':') + 1));
                    } else {
                        lock.setType(tempType);
                    }
                    break;
                default:
                    break;
                }
            }

            if (lock.getType() == null) {

                // Bad request
                resp.setStatus(CmsWebdavStatus.SC_BAD_REQUEST);
            }

        } else {

            // Bad request
            resp.setStatus(CmsWebdavStatus.SC_BAD_REQUEST);
        }

        if (lockOwnerNode != null) {

            iter = lockOwnerNode.elementIterator();
            while (iter.hasNext()) {
                Element currentElem = iter.next();
                switch (currentElem.getNodeType()) {
                case Node.TEXT_NODE:
                    lock.setOwner(lock.getOwner() + currentElem.getStringValue());
                    break;
                case Node.ELEMENT_NODE:
                    lock.setOwner(lock.getOwner() + currentElem.getStringValue());
                    break;
                default:
                    break;
                }
            }

            if (lock.getOwner() == null) {

                // Bad request
                resp.setStatus(CmsWebdavStatus.SC_BAD_REQUEST);
            }

        } else {
            lock.setOwner("");
        }

    }

    lock.setPath(path);
    lock.setUsername(m_username);

    if (lockRequestType == LOCK_REFRESH) {

        CmsRepositoryLockInfo currentLock = m_session.getLock(path);
        if (currentLock == null) {
            lockRequestType = LOCK_CREATION;
        }
    }

    if (lockRequestType == LOCK_CREATION) {

        try {

            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(Messages.LOG_LOCK_ITEM_1, lock.getOwner()));
            }

            boolean result = m_session.lock(path, lock);
            if (result) {

                // Add the Lock-Token header as by RFC 2518 8.10.1
                // - only do this for newly created locks
                resp.addHeader(HEADER_LOCKTOKEN, "<opaquelocktoken:" + generateLockToken(req, lock) + ">");

                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(Messages.LOG_LOCK_ITEM_FAILED_0));
                }

            } else {

                resp.setStatus(CmsWebdavStatus.SC_LOCKED);

                if (LOG.isDebugEnabled()) {
                    LOG.debug(Messages.get().getBundle().key(Messages.LOG_LOCK_ITEM_SUCCESS_0));
                }

                return;
            }
        } catch (CmsVfsResourceNotFoundException rnfex) {
            resp.setStatus(CmsWebdavStatus.SC_NOT_FOUND);

            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(Messages.LOG_ITEM_NOT_FOUND_1, path));
            }

            return;
        } catch (CmsSecurityException sex) {
            resp.setStatus(CmsWebdavStatus.SC_FORBIDDEN);

            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(Messages.LOG_NO_PERMISSION_0));
            }

            return;
        } catch (CmsException ex) {
            resp.setStatus(CmsWebdavStatus.SC_INTERNAL_SERVER_ERROR);

            if (LOG.isErrorEnabled()) {
                LOG.error(Messages.get().getBundle().key(Messages.LOG_REPOSITORY_ERROR_2, "LOCK", path), ex);
            }

            return;
        }
    }

    // Set the status, then generate the XML response containing
    // the lock information
    Document doc = DocumentHelper.createDocument();
    Element propElem = doc.addElement(new QName(TAG_PROP, Namespace.get(DEFAULT_NAMESPACE)));

    Element lockElem = addElement(propElem, TAG_LOCKDISCOVERY);
    addLockElement(lock, lockElem, generateLockToken(req, lock));

    resp.setStatus(CmsWebdavStatus.SC_OK);
    resp.setContentType("text/xml; charset=UTF-8");

    Writer writer = resp.getWriter();
    doc.write(writer);
    writer.close();
}

From source file:org.opencms.xml.containerpage.CmsXmlContainerPage.java

License:Open Source License

/**
 * @see org.opencms.xml.A_CmsXmlDocument#initDocument(org.dom4j.Document, java.lang.String, org.opencms.xml.CmsXmlContentDefinition)
 *///from  w  w  w . ja v  a  2 s  .c  om
@Override
protected void initDocument(Document document, String encoding, CmsXmlContentDefinition definition) {

    m_document = document;
    m_contentDefinition = definition;
    m_encoding = CmsEncoder.lookupEncoding(encoding, encoding);
    m_elementLocales = new HashMap<String, Set<Locale>>();
    m_elementNames = new HashMap<Locale, Set<String>>();
    m_locales = new HashSet<Locale>();
    m_cntPages = new HashMap<Locale, CmsContainerPageBean>();
    clearBookmarks();

    // initialize the bookmarks
    for (Iterator<Element> itCntPages = CmsXmlGenericWrapper
            .elementIterator(m_document.getRootElement()); itCntPages.hasNext();) {
        Element cntPage = itCntPages.next();

        try {
            Locale locale = CmsLocaleManager.getLocale(
                    cntPage.attribute(CmsXmlContentDefinition.XSD_ATTRIBUTE_VALUE_LANGUAGE).getValue());

            addLocale(locale);

            List<CmsContainerBean> containers = new ArrayList<CmsContainerBean>();
            for (Iterator<Element> itCnts = CmsXmlGenericWrapper.elementIterator(cntPage,
                    XmlNode.Containers.name()); itCnts.hasNext();) {
                Element container = itCnts.next();

                // container itself
                int cntIndex = CmsXmlUtils.getXpathIndexInt(container.getUniquePath(cntPage));
                String cntPath = CmsXmlUtils.createXpathElement(container.getName(), cntIndex);
                I_CmsXmlSchemaType cntSchemaType = definition.getSchemaType(container.getName());
                I_CmsXmlContentValue cntValue = cntSchemaType.createValue(this, container, locale);
                addBookmark(cntPath, locale, true, cntValue);
                CmsXmlContentDefinition cntDef = ((CmsXmlNestedContentDefinition) cntSchemaType)
                        .getNestedContentDefinition();

                // name
                Element name = container.element(XmlNode.Name.name());
                addBookmarkForElement(name, locale, container, cntPath, cntDef);

                // type
                Element type = container.element(XmlNode.Type.name());
                addBookmarkForElement(type, locale, container, cntPath, cntDef);

                List<CmsContainerElementBean> elements = new ArrayList<CmsContainerElementBean>();
                // Elements
                for (Iterator<Element> itElems = CmsXmlGenericWrapper.elementIterator(container,
                        XmlNode.Elements.name()); itElems.hasNext();) {
                    Element element = itElems.next();

                    // element itself
                    int elemIndex = CmsXmlUtils.getXpathIndexInt(element.getUniquePath(container));
                    String elemPath = CmsXmlUtils.concatXpath(cntPath,
                            CmsXmlUtils.createXpathElement(element.getName(), elemIndex));
                    I_CmsXmlSchemaType elemSchemaType = cntDef.getSchemaType(element.getName());
                    I_CmsXmlContentValue elemValue = elemSchemaType.createValue(this, element, locale);
                    addBookmark(elemPath, locale, true, elemValue);
                    CmsXmlContentDefinition elemDef = ((CmsXmlNestedContentDefinition) elemSchemaType)
                            .getNestedContentDefinition();

                    // uri
                    Element uri = element.element(XmlNode.Uri.name());
                    addBookmarkForElement(uri, locale, element, elemPath, elemDef);
                    Element uriLink = uri.element(CmsXmlPage.NODE_LINK);
                    CmsUUID elementId = null;
                    if (uriLink == null) {
                        // this can happen when adding the elements node to the xml content
                        // it is not dangerous since the link has to be set before saving 
                    } else {
                        elementId = new CmsLink(uriLink).getStructureId();
                    }
                    Element createNewElement = element.element(XmlNode.CreateNew.name());
                    boolean createNew = (createNewElement != null)
                            && Boolean.parseBoolean(createNewElement.getStringValue());

                    // formatter
                    Element formatter = element.element(XmlNode.Formatter.name());
                    addBookmarkForElement(formatter, locale, element, elemPath, elemDef);
                    Element formatterLink = formatter.element(CmsXmlPage.NODE_LINK);
                    CmsUUID formatterId = null;
                    if (formatterLink == null) {
                        // this can happen when adding the elements node to the xml content
                        // it is not dangerous since the link has to be set before saving 
                    } else {
                        formatterId = new CmsLink(formatterLink).getStructureId();
                    }

                    // the properties
                    Map<String, String> propertiesMap = CmsXmlContentPropertyHelper.readProperties(this, locale,
                            element, elemPath, elemDef);

                    if (elementId != null) {
                        elements.add(
                                new CmsContainerElementBean(elementId, formatterId, propertiesMap, createNew));
                    }
                }
                CmsContainerBean newContainerBean = new CmsContainerBean(name.getText(), type.getText(),
                        elements);
                containers.add(newContainerBean);
            }

            m_cntPages.put(locale, new CmsContainerPageBean(locale, containers));
        } catch (NullPointerException e) {
            LOG.error(org.opencms.xml.content.Messages.get().getBundle()
                    .key(org.opencms.xml.content.Messages.LOG_XMLCONTENT_INIT_BOOKMARKS_0), e);
        }
    }
}

From source file:org.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.java

License:Apache License

private String loadCategory(Document xmlDoc) {
    Element el = xmlDoc.getRootElement().element(new QName(KEYWORD_CATEGORY, namespaceCP));
    if (el != null)
        return el.getStringValue();
    else/*  w w  w  .  j a v a2  s . co  m*/
        return null;
}

From source file:org.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.java

License:Apache License

private String loadContentStatus(Document xmlDoc) {
    Element el = xmlDoc.getRootElement().element(new QName(KEYWORD_CONTENT_STATUS, namespaceCP));
    if (el != null)
        return el.getStringValue();
    else//from w w w.j a  va 2 s.  co  m
        return null;
}

From source file:org.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.java

License:Apache License

private String loadContentType(Document xmlDoc) {
    Element el = xmlDoc.getRootElement().element(new QName(KEYWORD_CONTENT_TYPE, namespaceCP));
    if (el != null)
        return el.getStringValue();
    else//from www  . j av  a 2  s. c  o m
        return null;
}

From source file:org.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.java

License:Apache License

private String loadCreated(Document xmlDoc) {
    Element el = xmlDoc.getRootElement().element(new QName(KEYWORD_CREATED, namespaceDcTerms));
    if (el != null)
        return el.getStringValue();
    else//from   ww  w .j  a  v  a  2s.  c o  m
        return null;
}