Example usage for com.liferay.portal.kernel.xml Element attributes

List of usage examples for com.liferay.portal.kernel.xml Element attributes

Introduction

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

Prototype

public List<Attribute> attributes();

Source Link

Usage

From source file:com.acs.DDMXSD.java

License:Open Source License

public JSONArray getJSONArray(Element element) throws JSONException {
    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

    Document document = element.getDocument();

    String defaultLocale = LocalizationUtil.getDefaultLocale(document.asXML());

    List<Element> dynamicElementElements = element.elements("dynamic-element");

    for (Element dynamicElementElement : dynamicElementElements) {
        JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
        JSONObject localizationMapJSONObject = JSONFactoryUtil.createJSONObject();

        List<Element> metadataElements = dynamicElementElement.elements("meta-data");

        for (Element metadataElement : metadataElements) {
            if (metadataElement == null) {
                continue;
            }/*from   ww w .j av  a 2 s. c  o m*/

            String locale = metadataElement.attributeValue("locale");

            JSONObject localeMap = JSONFactoryUtil.createJSONObject();

            localizationMapJSONObject.put(locale, localeMap);

            for (Element metadataEntryElement : metadataElement.elements()) {

                String attributeName = metadataEntryElement.attributeValue("name");
                String attributeValue = metadataEntryElement.getText();

                localeMap.put(attributeName, attributeValue);

                if (defaultLocale.equals(locale)) {
                    jsonObject.put(attributeName, attributeValue);
                }
            }
        }

        jsonObject.put("localizationMap", localizationMapJSONObject);

        for (Attribute attribute : dynamicElementElement.attributes()) {
            jsonObject.put(attribute.getName(), attribute.getValue());
        }

        jsonObject.put("id", dynamicElementElement.attributeValue("name"));

        JSONArray hiddenAttributesJSONArray = JSONFactoryUtil.createJSONArray();

        String type = jsonObject.getString("type");

        if (type.equals(_TYPE_CHECKBOX)) {
            hiddenAttributesJSONArray.put("required");
        }

        if (type.equals(_TYPE_DDM_FILEUPLOAD)) {
            hiddenAttributesJSONArray.put("predefinedValue");
        }

        hiddenAttributesJSONArray.put("readOnly");

        jsonObject.put("hiddenAttributes", hiddenAttributesJSONArray);

        String key = "fields";

        if (type.equals(_TYPE_RADIO) || type.equals(_TYPE_SELECT)) {
            key = "options";

            String predefinedValue = jsonObject.getString("predefinedValue");

            jsonObject.put("predefinedValue", JSONFactoryUtil.createJSONArray(predefinedValue));
        }

        jsonObject.put(key, getJSONArray(dynamicElementElement));

        jsonArray.put(jsonObject);
    }

    return jsonArray;
}

From source file:com.acs.DDMXSD.java

License:Open Source License

protected Map<String, Object> getFieldContext(Element dynamicElementElement, Locale locale) {

    Document document = dynamicElementElement.getDocument();

    String[] availableLocales = LocalizationUtil.getAvailableLocales(document.asXML());

    String defaultLanguageId = LocalizationUtil.getDefaultLocale(document.asXML());

    String languageId = LocaleUtil.toLanguageId(locale);

    if (!ArrayUtil.contains(availableLocales, languageId)) {
        languageId = defaultLanguageId;/*from  www  . j av a 2  s . c o  m*/
    }

    Element metadataElement = (Element) dynamicElementElement
            .selectSingleNode("meta-data[@locale='" + languageId + "']");

    Map<String, Object> field = new HashMap<String, Object>();

    if (metadataElement != null) {
        for (Element metadataEntry : metadataElement.elements()) {
            field.put(metadataEntry.attributeValue("name"), metadataEntry.getText());
        }
    }

    for (Attribute attribute : dynamicElementElement.attributes()) {
        field.put(attribute.getName(), attribute.getValue());
    }

    return field;
}

From source file:com.liferay.dynamic.data.mapping.test.util.DDMStructureTestUtil.java

License:Open Source License

protected static Map<String, String> getElementMap(Element element) {
    Map<String, String> elementMap = new HashMap<>();

    // Attributes

    for (Attribute attribute : element.attributes()) {
        elementMap.put(attribute.getName(), attribute.getValue());
    }//w  w w  .ja  va  2  s. co m

    // Metadata

    for (Element metadadataElement : element.elements("meta-data")) {
        String metadataLanguageId = metadadataElement.attributeValue("locale");

        for (Element entryElement : metadadataElement.elements("entry")) {
            String entryName = entryElement.attributeValue("name");

            elementMap.put(entryName.concat(metadataLanguageId), entryElement.getText());
        }
    }

    return elementMap;
}

From source file:com.liferay.exportimport.controller.PortletImportController.java

License:Open Source License

public void importServicePortletPreferences(PortletDataContext portletDataContext, Element serviceElement)
        throws PortalException {

    long ownerId = GetterUtil.getLong(serviceElement.attributeValue("owner-id"));
    int ownerType = GetterUtil.getInteger(serviceElement.attributeValue("owner-type"));
    String serviceName = serviceElement.attributeValue("service-name");

    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) {
        ownerId = portletDataContext.getGroupId();
    } else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) {
        ownerId = portletDataContext.getCompanyId();
    }//from   www .  j av  a 2  s .  c  o m

    PortletPreferences portletPreferences = getPortletPreferences(portletDataContext.getCompanyId(), ownerId,
            ownerType, LayoutConstants.DEFAULT_PLID, serviceName);

    for (Attribute attribute : serviceElement.attributes()) {
        serviceElement.remove(attribute);
    }

    String xml = serviceElement.asXML();

    portletPreferences.setPreferences(xml);

    _portletPreferencesLocalService.updatePortletPreferences(portletPreferences);
}

From source file:com.liferay.google.apps.connector.GHelperUtil.java

License:Open Source License

public static String getErrorMessage(Document document) {
    Element rootElement = document.getRootElement();

    Element errorElement = rootElement.element("error");

    List<Attribute> attributes = errorElement.attributes();

    StringBundler sb = new StringBundler(attributes.size() * 4 + 1);

    sb.append(StringPool.OPEN_CURLY_BRACE);

    for (int i = 0; i < attributes.size(); i++) {
        Attribute attribute = attributes.get(i);

        sb.append(attribute.getName());// w  w  w .j  av a2 s.  com
        sb.append(StringPool.EQUAL);
        sb.append(attribute.getValue());

        if ((i + 1) <= attributes.size()) {
            sb.append(StringPool.COMMA_AND_SPACE);
        }
    }

    sb.append(StringPool.CLOSE_CURLY_BRACE);

    return sb.toString();
}

From source file:com.liferay.journal.test.util.JournalTestUtil.java

License:Open Source License

private static Map<String, String> _getMap(Element dynamicElementElement) {
    Map<String, String> map = new HashMap<>();

    Element parentElement = dynamicElementElement.getParent();

    String parentType = parentElement.attributeValue("type");

    // Attributes

    for (Attribute attribute : dynamicElementElement.attributes()) {

        // Option element should not contain index type atribute

        if ((Objects.equals(parentType, "list") || Objects.equals(parentType, "multi-list"))
                && Objects.equals(attribute.getName(), "index-type")) {

            continue;
        }//from  w w  w.  j av a2  s .  co  m

        map.put(attribute.getName(), attribute.getValue());
    }

    // Metadata

    Element metadadataElement = dynamicElementElement.element("meta-data");

    if (metadadataElement == null) {
        return map;
    }

    List<Element> entryElements = metadadataElement.elements("entry");

    for (Element entryElement : entryElements) {
        map.put(entryElement.attributeValue("name"), entryElement.getText());
    }

    return map;
}

From source file:com.liferay.journal.transformer.JournalTransformer.java

License:Open Source License

protected List<TemplateNode> getTemplateNodes(ThemeDisplay themeDisplay, Element element, long ddmStructureId)
        throws Exception {

    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getStructure(ddmStructureId);

    DDMForm ddmForm = ddmStructure.getDDMForm();

    Map<String, DDMFormField> ddmFormFieldsMap = ddmForm.getDDMFormFieldsMap(true);

    List<TemplateNode> templateNodes = new ArrayList<>();

    Map<String, TemplateNode> prototypeTemplateNodes = new HashMap<>();

    List<Element> dynamicElementElements = element.elements("dynamic-element");

    for (Element dynamicElementElement : dynamicElementElements) {
        Element dynamicContentElement = dynamicElementElement.element("dynamic-content");

        String data = StringPool.BLANK;

        if (dynamicContentElement != null) {
            data = dynamicContentElement.getText();
        }//w ww  .ja va  2s .c  om

        String name = dynamicElementElement.attributeValue("name", StringPool.BLANK);

        if (name.length() == 0) {
            throw new TransformException("Element missing \"name\" attribute");
        }

        String type = dynamicElementElement.attributeValue("type", StringPool.BLANK);

        Map<String, String> attributes = new HashMap<>();

        if (type.equals("image")) {
            JSONObject dataJSONObject = JSONFactoryUtil.createJSONObject(data);

            Iterator<String> itr = dataJSONObject.keys();

            while (itr.hasNext()) {
                String key = itr.next();

                String value = dataJSONObject.getString(key);

                attributes.put(key, value);
            }
        }

        if (dynamicContentElement != null) {
            for (Attribute attribute : dynamicContentElement.attributes()) {
                attributes.put(attribute.getName(), attribute.getValue());
            }
        }

        TemplateNode templateNode = new TemplateNode(themeDisplay, name, StringUtil.stripCDATA(data), type,
                attributes);

        if (dynamicElementElement.element("dynamic-element") != null) {
            templateNode.appendChildren(getTemplateNodes(themeDisplay, dynamicElementElement, ddmStructureId));
        } else if ((dynamicContentElement != null) && (dynamicContentElement.element("option") != null)) {

            List<Element> optionElements = dynamicContentElement.elements("option");

            for (Element optionElement : optionElements) {
                templateNode.appendOption(StringUtil.stripCDATA(optionElement.getText()));
            }
        }

        DDMFormField ddmFormField = ddmFormFieldsMap.get(name);

        if (ddmFormField != null) {
            DDMFormFieldOptions ddmFormFieldOptions = ddmFormField.getDDMFormFieldOptions();

            Map<String, LocalizedValue> options = ddmFormFieldOptions.getOptions();

            for (Entry<String, LocalizedValue> entry : options.entrySet()) {
                String optionValue = StringUtil.stripCDATA(entry.getKey());

                LocalizedValue localizedLabel = entry.getValue();

                String optionLabel = localizedLabel.getString(themeDisplay.getLocale());

                templateNode.appendOptionMap(optionValue, optionLabel);
            }
        }

        TemplateNode prototypeTemplateNode = prototypeTemplateNodes.get(name);

        if (prototypeTemplateNode == null) {
            prototypeTemplateNode = templateNode;

            prototypeTemplateNodes.put(name, prototypeTemplateNode);

            templateNodes.add(templateNode);
        }

        prototypeTemplateNode.appendSibling(templateNode);
    }

    return templateNodes;
}

From source file:com.liferay.portlet.dynamicdatamapping.model.impl.DDMStructureImpl.java

License:Open Source License

private Map<String, String> _getField(Element element, String locale) {
    Map<String, String> field = new HashMap<String, String>();

    List<String> availableLocales = getAvailableLocales();

    if ((locale != null) && !(availableLocales.contains(locale))) {
        locale = getDefaultLocale();//  w w  w.j a v  a 2  s .  c om
    }

    String xPathExpression = "meta-data[@locale=\"".concat(locale).concat("\"]");

    XPath xPathSelector = SAXReaderUtil.createXPath(xPathExpression);

    Node node = xPathSelector.selectSingleNode(element);

    Element metaDataElement = (Element) node.asXPathResult(node.getParent());

    if (metaDataElement != null) {
        List<Element> childMetaDataElements = metaDataElement.elements();

        for (Element childMetaDataElement : childMetaDataElements) {
            String name = childMetaDataElement.attributeValue("name");
            String value = childMetaDataElement.getText();

            field.put(name, value);
        }
    }

    for (Attribute attribute : element.attributes()) {
        field.put(attribute.getName(), attribute.getValue());
    }

    return field;
}