Example usage for com.liferay.portal.kernel.xml SAXReaderUtil createElement

List of usage examples for com.liferay.portal.kernel.xml SAXReaderUtil createElement

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.xml SAXReaderUtil createElement.

Prototype

public static Element createElement(String name) 

Source Link

Usage

From source file:com.liferay.web.extender.internal.webbundle.WebBundleProcessor.java

License:Open Source License

protected void processPortletXML(String webContextpath) throws IOException {
    File portletXMLFile = new File(_deployedAppFolder, "WEB-INF/" + Portal.PORTLET_XML_FILE_NAME_STANDARD);

    if (!portletXMLFile.exists()) {
        return;// w  w w . j a v  a 2s. c o m
    }

    String content = FileUtil.read(portletXMLFile);

    Document document = null;

    try {
        document = SAXReaderUtil.read(content);
    } catch (DocumentException de) {
        throw new IOException(de);
    }

    Element rootElement = document.getRootElement();

    List<Element> portletElements = rootElement.elements("portlet");

    for (Element portletElement : portletElements) {
        String portletName = portletElement.elementText("portlet-name");

        String invokerPortletName = "osgi".concat(webContextpath).concat(StringPool.SLASH).concat(portletName);

        XPath xPath = SAXReaderUtil.createXPath(_INVOKER_PORTLET_NAME_XPATH);

        Element invokerPortletNameEl = (Element) xPath.selectSingleNode(portletElement);

        if (invokerPortletNameEl == null) {
            Element portletClassElement = portletElement.element("portlet-class");

            List<Node> children = portletElement.content();

            int pos = children.indexOf(portletClassElement);

            QName qName = rootElement.getQName();

            Element initParamElement = SAXReaderUtil
                    .createElement(SAXReaderUtil.createQName("init-param", qName.getNamespace()));

            initParamElement.addElement("name").setText("com.liferay.portal.invokerPortletName");
            initParamElement.addElement("value").setText(invokerPortletName);

            children.add(pos + 1, initParamElement);
        } else {
            Element valueElement = invokerPortletNameEl.element("value");

            invokerPortletName = valueElement.getTextTrim();

            if (!invokerPortletName.startsWith(StringPool.SLASH)) {
                invokerPortletName = StringPool.SLASH.concat(invokerPortletName);
            }

            invokerPortletName = "osgi".concat(webContextpath).concat(invokerPortletName);

            valueElement.setText(invokerPortletName);
        }
    }

    content = DDMXMLUtil.formatXML(document);

    FileUtil.write(portletXMLFile, content);
}

From source file:it.smc.calendar.sync.caldav.methods.BasePropMethodImpl.java

License:Open Source License

protected int writeResponseXML(WebDAVRequest webDAVRequest, Set<QName> props) throws Exception {

    WebDAVStorage storage = webDAVRequest.getWebDAVStorage();

    long depth = WebDAVUtil.getDepth(webDAVRequest.getHttpServletRequest());

    Document document = SAXReaderUtil.createDocument();

    Element multistatusElement = SAXReaderUtil.createElement(CalDAVProps.createQName("multistatus"));

    document.setRootElement(multistatusElement);

    Resource resource = storage.getResource(webDAVRequest);

    if (resource != null) {
        addResponse(storage, webDAVRequest, resource, props, multistatusElement, depth);

        String xml = document.formattedString(StringPool.FOUR_SPACES);

        if (_log.isDebugEnabled()) {
            _log.debug("Response XML\n" + xml);
        }/*  w w  w. j av a2s. c o  m*/

        // Set the status prior to writing the XML

        int status = WebDAVUtil.SC_MULTI_STATUS;

        HttpServletResponse response = webDAVRequest.getHttpServletResponse();

        response.setContentType(ContentTypes.TEXT_XML_UTF8);
        response.setStatus(status);

        try {
            ServletResponseUtil.write(response, xml);

            response.flushBuffer();
        } catch (Exception e) {
            if (_log.isWarnEnabled()) {
                _log.warn(e);
            }
        }

        return status;
    } else {
        if (_log.isDebugEnabled()) {
            _log.debug("No resource found for " + storage.getRootPath() + webDAVRequest.getPath());
        }

        return HttpServletResponse.SC_NOT_FOUND;
    }
}