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

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

Introduction

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

Prototype

public static Attribute createAttribute(Element element, String name, String value) 

Source Link

Usage

From source file:com.liferay.ide.servlet.WebServicesServlet.java

License:Open Source License

protected String getWebServicesXML() {
    Map<String, Set<JSONWebServiceActionMapping>> jsonWebServiceClazz = getJSONWebServiceClazz();

    Document document = SAXReaderUtil.createDocument("UTF-8");

    Element root = SAXReaderUtil.createElement("templates");

    document.add(root);/*from  w  ww.  ja va2 s .c  o  m*/

    for (String jsonWebServiceClassName : jsonWebServiceClazz.keySet()) {
        Set<JSONWebServiceActionMapping> jsonWebServiceMappings = jsonWebServiceClazz
                .get(jsonWebServiceClassName);

        String className = jsonWebServiceClassName;

        if (className.endsWith("Impl")) {
            className = className.substring(0, className.length() - 4);
        }

        if (className.endsWith("Service")) {
            className = className.substring(0, className.length() - 7);
        }

        for (JSONWebServiceActionMapping jsonWebServiceActionMapping : jsonWebServiceMappings) {

            Element element = SAXReaderUtil.createElement("template");

            String path = jsonWebServiceActionMapping.getPath();

            int pos = path.lastIndexOf(CharPool.SLASH);

            String actionName = path.substring(pos + 1);

            element.add(
                    SAXReaderUtil.createAttribute(element, "name", "jsonws-" + className + "-" + actionName));
            element.add(SAXReaderUtil.createAttribute(element, "description",
                    "jsonws-" + className + "-" + actionName));
            element.add(SAXReaderUtil.createAttribute(element, "context", "javaScript"));
            element.add(SAXReaderUtil.createAttribute(element, "enabled", "true"));
            element.add(SAXReaderUtil.createAttribute(element, "autoinsert", "true"));

            StringBuffer sb = new StringBuffer();
            sb.append("Liferay.Service(\n   '");
            sb.append(path);
            sb.append("',\n   {\n");

            MethodParameter[] methodParameters = jsonWebServiceActionMapping.getMethodParameters();

            if (methodParameters.length > 0) {
                for (int t = 0; t < methodParameters.length; t++) {
                    String parameterName = methodParameters[t].getName();

                    sb.append("      ");
                    sb.append(parameterName);
                    sb.append(":");
                    sb.append("${");
                    sb.append(parameterName);
                    sb.append("}");

                    if (t < methodParameters.length - 1) {
                        sb.append(",\n");
                    }
                }

                element.add(SAXReaderUtil.createAttribute(element, "id", "com.liferay.ide.ui.templates."
                        + className + "." + actionName + methodParameters.length));
            } else {
                element.add(SAXReaderUtil.createAttribute(element, "id",
                        "com.liferay.ide.ui.templates." + className + "." + actionName));
            }

            sb.append("\n   },\n   function(obj) {\n      console.log(obj);\n   }\n);");
            element.add(SAXReaderUtil.createText(sb.toString()));

            root.add(element);
        }
    }

    return document.asXML();
}

From source file:com.liferay.portlet.journal.util.JournalUtil.java

License:Open Source License

public static void addReservedEl(Element rootElement, Map<String, String> tokens, String name, String value) {

    // XML//w  w w.j av  a  2 s .  c o  m

    if (rootElement != null) {
        Element dynamicElementElement = SAXReaderUtil.createElement("dynamic-element");

        Attribute nameAttribute = SAXReaderUtil.createAttribute(dynamicElementElement, "name", name);

        dynamicElementElement.add(nameAttribute);

        Attribute typeAttribute = SAXReaderUtil.createAttribute(dynamicElementElement, "type", "text");

        dynamicElementElement.add(typeAttribute);

        Element dynamicContentElement = SAXReaderUtil.createElement("dynamic-content");

        //dynamicContentElement.setText("<![CDATA[" + value + "]]>");
        dynamicContentElement.setText(value);

        dynamicElementElement.add(dynamicContentElement);

        rootElement.add(dynamicElementElement);
    }

    // Tokens

    tokens.put(StringUtil.replace(name, CharPool.DASH, CharPool.UNDERLINE), value);
}