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

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

Introduction

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

Prototype

public Attribute attribute(String name);

Source Link

Usage

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

License:Open Source License

@Override
public void updateDescription(String emailAddress, String description) throws GoogleAppsException {

    Document document = getDocument(getGroupURL(emailAddress));

    if (hasError(document)) {
        if (_log.isInfoEnabled()) {
            _log.info(getErrorMessage(document));
        }/*ww w  .  j a v a2 s.c  o  m*/

        return;
    }

    Element atomEntryElement = document.getRootElement();

    List<Element> appsPropertyElements = atomEntryElement.elements(getAppsQName("property"));

    for (Element appsPropertyElement : appsPropertyElements) {
        String name = appsPropertyElement.attributeValue("name");

        if (name.equals("description")) {
            Attribute valueAttribute = appsPropertyElement.attribute("value");

            valueAttribute.setValue(description);
        }
    }

    submitUpdate(getGroupURL(emailAddress), document);
}

From source file:com.liferay.journal.internal.util.impl.JournalConverterImpl.java

License:Open Source License

protected void removeAttribute(Element element, String attributeName) {
    Attribute attribute = element.attribute(attributeName);

    if (attribute == null) {
        return;/*  w ww .j  a  v  a2 s.c  om*/
    }

    element.remove(attribute);
}

From source file:com.liferay.journal.internal.util.impl.JournalConverterImpl.java

License:Open Source License

protected void updateJournalXSDDynamicElement(Element element, String defaultLanguageId) {

    String name = element.attributeValue("name");
    String type = element.attributeValue("type");

    Element metadataElement = element.element("meta-data");

    if (metadataElement == null) {
        metadataElement = element.addElement("meta-data");
    }// w  ww.j a v  a  2s.c  o m

    if (type.equals("multi-list")) {
        element.addAttribute("multiple", "true");
    } else {
        Element parentElement = element.getParent();

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

        if ((parentType != null) && parentType.equals("select")) {
            metadataElement.addAttribute("locale", defaultLanguageId);

            addMetadataEntry(metadataElement, "label", decodeURL(name));

            removeAttribute(element, "index-type");

            element.addAttribute("name",
                    "option" + parentElement.attributeValue("name") + StringUtil.randomString(8));
            element.addAttribute("type", "option");
            element.addAttribute("value", decodeURL(type));

            return;
        }
    }

    String indexType = StringPool.BLANK;

    Attribute indexTypeAttribute = element.attribute("index-type");

    if (indexTypeAttribute != null) {
        indexType = indexTypeAttribute.getValue();

        element.remove(indexTypeAttribute);
    }

    element.remove(element.attribute("type"));

    if (!type.equals("selection_break")) {
        String dataType = _ddmDataTypes.get(type);

        if (dataType == null) {
            dataType = "string";
        }

        element.addAttribute("dataType", dataType);
    }

    element.addAttribute("indexType", indexType);

    String required = "false";

    Element requiredElement = fetchMetadataEntry(metadataElement, "name", "required");

    if (requiredElement != null) {
        required = requiredElement.getText();
    }

    element.addAttribute("required", required);

    element.addAttribute("showLabel", "true");

    String newType = _journalTypesToDDMTypes.get(type);

    if (newType == null) {
        newType = type;
    }

    element.addAttribute("type", newType);

    if (newType.startsWith("ddm")) {
        element.addAttribute("fieldNamespace", "ddm");
    }

    metadataElement.addAttribute("locale", defaultLanguageId);

    List<Element> entryElements = metadataElement.elements();

    if (entryElements.isEmpty()) {
        addMetadataEntry(metadataElement, "label", name);
    } else {
        for (Element entryElement : entryElements) {
            String oldEntryName = entryElement.attributeValue("name");

            String newEntryName = _ddmMetadataAttributes.get(oldEntryName);

            if (newEntryName == null) {
                metadataElement.remove(entryElement);
            } else {
                entryElement.addAttribute("name", newEntryName);
            }
        }
    }

    if (newType.equals("ddm-date") || newType.equals("ddm-decimal") || newType.equals("ddm-integer")
            || newType.equals("ddm-link-to-page") || newType.equals("ddm-number")
            || newType.equals("ddm-text-html") || newType.equals("text") || newType.equals("textarea")) {

        element.addAttribute("width", "25");
    } else if (newType.equals("ddm-image")) {
        element.addAttribute("fieldNamespace", "ddm");
        element.addAttribute("readOnly", "false");
    }

    element.add(metadataElement.detach());

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

    for (Element dynamicElementElement : dynamicElementElements) {
        updateJournalXSDDynamicElement(dynamicElementElement, defaultLanguageId);
    }
}

From source file:com.liferay.journal.transformer.test.JournalTransformerTest.java

License:Open Source License

@Test
public void testLocaleTransformerListenerNestedFieldWithNoTranslation() throws Exception {

    Map<String, String> tokens = getTokens();

    Map<Locale, String> contents = new HashMap<>();

    contents.put(LocaleUtil.US, "Joe Bloggs");

    String xml = DDMStructureTestUtil.getSampleStructuredContent(contents,
            LanguageUtil.getLanguageId(LocaleUtil.US));

    Document document = UnsecureSAXReaderUtil.read(xml);

    Element rootElement = document.getRootElement();

    Attribute availableLocalesAttribute = rootElement.attribute("available-locales");

    availableLocalesAttribute.setValue("en_US,pt_BR");

    Element dynamicElement = (Element) document.selectSingleNode("//dynamic-element");

    dynamicElement.addElement("nestedElement");

    String script = "$name.getData()";

    String content = JournalUtil.transform(null, tokens, Constants.VIEW, "en_US", document, null, script,
            TemplateConstants.LANG_TYPE_VM);

    Assert.assertEquals("Joe Bloggs", content);

    content = JournalUtil.transform(null, tokens, Constants.VIEW, "pt_BR", document, null, script,
            TemplateConstants.LANG_TYPE_VM);

    Assert.assertEquals("Joe Bloggs", content);
}

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

License:Open Source License

public static String prepareLocalizedContentForImport(String content, Locale defaultImportLocale)
        throws LocaleException {

    try {/*from w ww.j a  va 2 s. co  m*/
        Document oldDocument = SAXReaderUtil.read(content);

        Document newDocument = SAXReaderUtil.read(content);

        Element newRootElement = newDocument.getRootElement();

        Attribute availableLocalesAttribute = newRootElement.attribute("available-locales");

        if (availableLocalesAttribute == null) {
            newRootElement = newRootElement.addAttribute("available-locales", StringPool.BLANK);

            availableLocalesAttribute = newRootElement.attribute("available-locales");
        }

        String defaultImportLanguageId = LocaleUtil.toLanguageId(defaultImportLocale);

        if (!StringUtil.contains(availableLocalesAttribute.getValue(), defaultImportLanguageId)) {

            if (Validator.isNull(availableLocalesAttribute.getValue())) {
                availableLocalesAttribute.setValue(defaultImportLanguageId);
            } else {
                availableLocalesAttribute.setValue(
                        availableLocalesAttribute.getValue() + StringPool.COMMA + defaultImportLanguageId);
            }

            _mergeArticleContentUpdate(oldDocument, newRootElement,
                    LocaleUtil.toLanguageId(defaultImportLocale));

            content = XMLUtil.formatXML(newDocument);
        }

        Attribute defaultLocaleAttribute = newRootElement.attribute("default-locale");

        if (defaultLocaleAttribute == null) {
            newRootElement = newRootElement.addAttribute("default-locale", StringPool.BLANK);

            defaultLocaleAttribute = newRootElement.attribute("default-locale");
        }

        Locale defaultContentLocale = LocaleUtil.fromLanguageId(defaultLocaleAttribute.getValue());

        if (!LocaleUtil.equals(defaultContentLocale, defaultImportLocale)) {
            defaultLocaleAttribute.setValue(defaultImportLanguageId);

            content = XMLUtil.formatXML(newDocument);
        }
    } catch (Exception e) {
        throw new LocaleException(LocaleException.TYPE_CONTENT,
                "The locale " + defaultImportLocale + " is not available");
    }

    return content;
}

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

License:Open Source License

private static void _mergeArticleContentUpdate(Element curElement, Element newElement, String defaultLocale) {

    Attribute curTypeAttribute = curElement.attribute("type");
    Attribute newTypeAttribute = newElement.attribute("type");

    curTypeAttribute.setValue(newTypeAttribute.getValue());

    Attribute curIndexTypeAttribute = curElement.attribute("index-type");
    Attribute newIndexTypeAttribute = newElement.attribute("index-type");

    if (newIndexTypeAttribute != null) {
        if (curIndexTypeAttribute == null) {
            curElement.addAttribute("index-type", newIndexTypeAttribute.getValue());
        } else {//  w  w w.  j a  v a2 s . com
            curIndexTypeAttribute.setValue(newIndexTypeAttribute.getValue());
        }
    }

    List<Element> elements = newElement.elements("dynamic-content");

    if ((elements == null) || elements.isEmpty()) {
        return;
    }

    Element newContentElement = elements.get(0);

    String newLanguageId = newContentElement.attributeValue("language-id");
    String newValue = newContentElement.getText();

    String indexType = newElement.attributeValue("index-type");

    if (Validator.isNotNull(indexType)) {
        curElement.addAttribute("index-type", indexType);
    }

    List<Element> curContentElements = curElement.elements("dynamic-content");

    if (Validator.isNull(newLanguageId)) {
        for (Element curContentElement : curContentElements) {
            curContentElement.detach();
        }

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

        if (newContentElement.element("option") != null) {
            _addElementOptions(curContentElement, newContentElement);
        } else {
            curContentElement.addCDATA(newValue);
        }

        curElement.add(curContentElement);
    } else {
        boolean alreadyExists = false;

        for (Element curContentElement : curContentElements) {
            String curLanguageId = curContentElement.attributeValue("language-id");

            if (newLanguageId.equals(curLanguageId)) {
                alreadyExists = true;

                curContentElement.clearContent();

                if (newContentElement.element("option") != null) {
                    _addElementOptions(curContentElement, newContentElement);
                } else {
                    curContentElement.addCDATA(newValue);
                }

                break;
            }
        }

        if (!alreadyExists) {
            Element curContentElement = curContentElements.get(0);

            String curLanguageId = curContentElement.attributeValue("language-id");

            if (Validator.isNull(curLanguageId)) {
                if (newLanguageId.equals(defaultLocale)) {
                    curContentElement.clearContent();

                    if (newContentElement.element("option") != null) {
                        _addElementOptions(curContentElement, newContentElement);
                    } else {
                        curContentElement.addCDATA(newValue);
                    }
                } else {
                    curElement.add(newContentElement.createCopy());
                }

                curContentElement.addAttribute("language-id", defaultLocale);
            } else {
                curElement.add(newContentElement.createCopy());
            }
        }
    }
}

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

License:Open Source License

protected void updateJournalXSDDynamicElement(Element element) {
    Locale defaultLocale = LocaleUtil.getSiteDefault();

    String name = element.attributeValue("name");
    String type = element.attributeValue("type");

    Element metadataElement = element.element("meta-data");

    if (metadataElement == null) {
        metadataElement = element.addElement("meta-data");
    }//from   w  ww  .j  av  a 2  s  . com

    if (type.equals("multi-list")) {
        element.addAttribute("multiple", "true");
    } else if (type.equals("selection_break")) {
        Element parentElement = element.getParent();

        parentElement.remove(element);

        return;
    } else {
        Element parentElement = element.getParent();

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

        if ((parentType != null) && parentType.equals("select")) {
            metadataElement.addAttribute("locale", defaultLocale.toString());

            addMetadataEntry(metadataElement, "label", name);

            element.addAttribute("name", "option" + StringUtil.randomId());
            element.addAttribute("type", "option");
            element.addAttribute("value", name);

            return;
        }
    }

    String indexType = StringPool.BLANK;

    Attribute indexTypeAttribute = element.attribute("index-type");

    if (indexTypeAttribute != null) {
        indexType = indexTypeAttribute.getValue();

        element.remove(indexTypeAttribute);
    }

    element.remove(element.attribute("type"));

    String dataType = _ddmDataTypes.get(type);

    if (dataType == null) {
        dataType = "string";
    }

    element.addAttribute("dataType", dataType);
    element.addAttribute("indexType", indexType);

    String required = "false";

    Element requiredElement = fetchMetadataEntry(metadataElement, "name", "required");

    if (requiredElement != null) {
        required = requiredElement.getText();
    }

    element.addAttribute("required", required);

    element.addAttribute("showLabel", "true");

    String newType = _journalTypesToDDMTypes.get(type);

    if (newType == null) {
        newType = type;
    }

    element.addAttribute("type", newType);

    if (newType.startsWith("ddm")) {
        element.addAttribute("fieldNamespace", "ddm");
    }

    metadataElement.addAttribute("locale", defaultLocale.toString());

    List<Element> entryElements = metadataElement.elements();

    if (entryElements.isEmpty()) {
        addMetadataEntry(metadataElement, "label", name);
    } else {
        for (Element entryElement : entryElements) {
            String oldEntryName = entryElement.attributeValue("name");

            String newEntryName = _ddmMetadataAttributes.get(oldEntryName);

            if (newEntryName == null) {
                metadataElement.remove(entryElement);
            } else {
                entryElement.addAttribute("name", newEntryName);
            }
        }
    }

    if (newType.equals("ddm-date") || newType.equals("ddm-decimal") || newType.equals("ddm-integer")
            || newType.equals("ddm-link-to-page") || newType.equals("ddm-number")
            || newType.equals("ddm-text-html") || newType.equals("text") || newType.equals("textarea")) {

        element.addAttribute("width", "25");
    } else if (newType.equals("wcm-image")) {
        element.addAttribute("fieldNamespace", "wcm");
        element.addAttribute("readOnly", "false");
    }

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

    for (Element dynamicElementElement : dynamicElementElements) {
        updateJournalXSDDynamicElement(dynamicElementElement);
    }
}

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

License:Open Source License

private static void _mergeArticleContentUpdate(Element curElement, Element newElement, String defaultLocale) {

    Attribute curTypeAttribute = curElement.attribute("type");
    Attribute newTypeAttribute = newElement.attribute("type");

    curTypeAttribute.setValue(newTypeAttribute.getValue());

    Attribute curIndexTypeAttribute = curElement.attribute("index-type");
    Attribute newIndexTypeAttribute = newElement.attribute("index-type");

    if (newIndexTypeAttribute != null) {
        if (curIndexTypeAttribute == null) {
            curElement.addAttribute("index-type", newIndexTypeAttribute.getValue());
        } else {//from  www. j av a2  s  .c o  m
            curIndexTypeAttribute.setValue(newIndexTypeAttribute.getValue());
        }
    }

    Element newContentElement = newElement.elements("dynamic-content").get(0);

    String newLanguageId = newContentElement.attributeValue("language-id");
    String newValue = newContentElement.getText();

    String indexType = newElement.attributeValue("index-type");

    if (Validator.isNotNull(indexType)) {
        curElement.addAttribute("index-type", indexType);
    }

    List<Element> curContentElements = curElement.elements("dynamic-content");

    if (Validator.isNull(newLanguageId)) {
        for (Element curContentElement : curContentElements) {
            curContentElement.detach();
        }

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

        if (newContentElement.element("option") != null) {
            _addElementOptions(curContentElement, newContentElement);
        } else {
            curContentElement.addCDATA(newValue);
        }

        curElement.add(curContentElement);
    } else {
        boolean alreadyExists = false;

        for (Element curContentElement : curContentElements) {
            String curLanguageId = curContentElement.attributeValue("language-id");

            if (newLanguageId.equals(curLanguageId)) {
                alreadyExists = true;

                curContentElement.clearContent();

                if (newContentElement.element("option") != null) {
                    _addElementOptions(curContentElement, newContentElement);
                } else {
                    curContentElement.addCDATA(newValue);
                }

                break;
            }
        }

        if (!alreadyExists) {
            Element curContentElement = curContentElements.get(0);

            String curLanguageId = curContentElement.attributeValue("language-id");

            if (Validator.isNull(curLanguageId)) {
                if (newLanguageId.equals(defaultLocale)) {
                    curContentElement.clearContent();

                    if (newContentElement.element("option") != null) {
                        _addElementOptions(curContentElement, newContentElement);
                    } else {
                        curContentElement.addCDATA(newValue);
                    }
                } else {
                    curElement.add(newContentElement.createCopy());
                }

                curContentElement.addAttribute("language-id", defaultLocale);
            } else {
                curElement.add(newContentElement.createCopy());
            }
        }
    }
}

From source file:com.liferay.portlet.wiki.importers.mediawiki.MediaWikiImporter.java

License:Open Source License

protected List<String> readSpecialNamespaces(Element root) throws ImportFilesException {

    List<String> namespaces = new ArrayList<String>();

    Element siteinfoElement = root.element("siteinfo");

    if (siteinfoElement == null) {
        throw new ImportFilesException("Invalid pages XML file");
    }//  w  w  w .j  av  a2 s. c o  m

    Element namespacesElement = siteinfoElement.element("namespaces");

    List<Element> namespaceElements = namespacesElement.elements("namespace");

    for (Element namespaceElement : namespaceElements) {
        Attribute attribute = namespaceElement.attribute("key");

        String value = attribute.getValue();

        if (!value.equals("0")) {
            namespaces.add(namespaceElement.getText());
        }
    }

    return namespaces;
}

From source file:com.liferay.resourcesimporter.util.FileSystemImporter.java

License:Open Source License

protected boolean isJournalStructureXSD(String xsd) throws Exception {
    Document document = SAXReaderUtil.read(xsd);

    Element rootElement = document.getRootElement();

    Attribute availableLocalesAttribute = rootElement.attribute("available-locales");

    if (availableLocalesAttribute == null) {
        return true;
    }/*from  ww w  .  ja va2s . c om*/

    return false;
}