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

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

Introduction

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

Prototype

public Element addAttribute(String name, String value);

Source Link

Usage

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

License:Open Source License

@Override
public String getContent(DDMStructure ddmStructure, Fields ddmFields) throws Exception {

    Document document = SAXReaderUtil.createDocument();

    Element rootElement = document.addElement("root");

    String availableLocales = getAvailableLocales(ddmFields);

    rootElement.addAttribute("available-locales", availableLocales);

    Locale defaultLocale = ddmFields.getDefaultLocale();

    rootElement.addAttribute("default-locale", LocaleUtil.toLanguageId(defaultLocale));

    DDMFieldsCounter ddmFieldsCounter = new DDMFieldsCounter();

    for (String fieldName : ddmStructure.getRootFieldNames()) {
        int repetitions = countFieldRepetition(ddmFields, fieldName, null, -1);

        for (int i = 0; i < repetitions; i++) {
            Element dynamicElementElement = rootElement.addElement("dynamic-element");

            dynamicElementElement.addAttribute("name", fieldName);

            updateContentDynamicElement(dynamicElementElement, ddmStructure, ddmFields, ddmFieldsCounter);
        }//from   www  .ja  v  a2s.co m
    }

    try {
        return XMLUtil.formatXML(document.asXML());
    } catch (Exception e) {
        throw new ArticleContentException("Unable to read content with an XML parser", e);
    }
}

From source file:com.liferay.journal.lar.test.ManifestSummaryTest.java

License:Open Source License

@Override
protected void validateExport(PortletDataContext portletDataContext, StagedModel stagedModel,
        Map<String, List<StagedModel>> dependentStagedModelsMap) throws Exception {

    ManifestSummary manifestSummary = portletDataContext.getManifestSummary();

    Map<String, LongWrapper> modelAdditionCounters = manifestSummary.getModelAdditionCounters();

    Assert.assertEquals(4, modelAdditionCounters.size());

    Assert.assertEquals(1, manifestSummary
            .getModelAdditionCount(new StagedModelType(DDMStructure.class, JournalArticle.class)));
    Assert.assertEquals(1,/*from w  w  w.j  av a  2 s  .c  o  m*/
            manifestSummary.getModelAdditionCount(new StagedModelType(DDMTemplate.class, DDMStructure.class)));
    Assert.assertEquals(1, manifestSummary.getModelAdditionCount(new StagedModelType(JournalArticle.class)));
    Assert.assertEquals(1, manifestSummary.getModelAdditionCount(new StagedModelType(JournalFolder.class)));

    Document document = SAXReaderUtil.createDocument();

    Element rootElement = document.addElement("root");

    Element headerElement = rootElement.addElement("header");

    DateFormat dateFormat = DateFormatFactoryUtil.getSimpleDateFormat(Time.RFC822_FORMAT);

    String rfc822DateString = Time.getRFC822();

    _exportDate = dateFormat.parse(rfc822DateString);

    headerElement.addAttribute("export-date", rfc822DateString);

    ExportImportHelperUtil.writeManifestSummary(document, manifestSummary);

    zipWriter.addEntry("/manifest.xml", document.asXML());
}

From source file:com.liferay.journal.service.impl.JournalArticleLocalServiceImpl.java

License:Open Source License

protected void copyArticleImages(JournalArticle oldArticle, JournalArticle newArticle) throws Exception {

    Folder folder = newArticle.addImagesFolder();

    for (FileEntry fileEntry : oldArticle.getImagesFileEntries()) {
        PortletFileRepositoryUtil.addPortletFileEntry(oldArticle.getGroupId(), newArticle.getUserId(),
                JournalArticle.class.getName(), newArticle.getResourcePrimKey(), JournalConstants.SERVICE_NAME,
                folder.getFolderId(), fileEntry.getContentStream(), fileEntry.getFileName(),
                fileEntry.getMimeType(), false);
    }/*from  www  .j  a va2s  .  c  om*/

    Document contentDocument = oldArticle.getDocument();

    contentDocument = contentDocument.clone();

    XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element[@type='image']");

    List<Node> imageNodes = xPathSelector.selectNodes(contentDocument);

    for (Node imageNode : imageNodes) {
        Element imageEl = (Element) imageNode;

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

        for (Element dynamicContentEl : dynamicContentEls) {
            String fileName = dynamicContentEl.attributeValue("name");

            FileEntry fileEntry = PortletFileRepositoryUtil.getPortletFileEntry(newArticle.getGroupId(),
                    folder.getFolderId(), fileName);

            String previewURL = DLUtil.getPreviewURL(fileEntry, fileEntry.getFileVersion(), null,
                    StringPool.BLANK, false, true);

            dynamicContentEl.addAttribute("resourcePrimKey", String.valueOf(newArticle.getResourcePrimKey()));

            dynamicContentEl.clearContent();

            dynamicContentEl.addCDATA(previewURL);
        }
    }

    newArticle.setContent(contentDocument.formattedString());
}

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

License:Open Source License

public static Element addMetadataElement(Element element, String locale, String label) {

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

    metadataElement.addAttribute("locale", locale);

    Element entryElement = metadataElement.addElement("entry");

    entryElement.addAttribute("name", "label");

    entryElement.addCDATA(label);/*from   www .j  av  a 2s  . co  m*/

    return entryElement;
}

From source file:com.liferay.journal.util.impl.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  a  v  a 2  s . co m*/

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

        dynamicElementElement.addAttribute("name", name);

        dynamicElementElement.addAttribute("type", "text");

        Element dynamicContentElement = dynamicElementElement.addElement("dynamic-content");

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

    // Tokens

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

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

License:Open Source License

public static String mergeArticleContent(String curContent, String newContent, boolean removeNullElements) {

    try {/*from   w w w .  jav  a 2 s .  co  m*/
        Document curDocument = SAXReaderUtil.read(curContent);
        Document newDocument = SAXReaderUtil.read(newContent);

        Element curRootElement = curDocument.getRootElement();
        Element newRootElement = newDocument.getRootElement();

        curRootElement.addAttribute("default-locale", newRootElement.attributeValue("default-locale"));
        curRootElement.addAttribute("available-locales", newRootElement.attributeValue("available-locales"));

        _mergeArticleContentUpdate(curDocument, newRootElement,
                LocaleUtil.toLanguageId(LocaleUtil.getSiteDefault()));

        if (removeNullElements) {
            _mergeArticleContentDelete(curRootElement, newDocument);
        }

        curContent = XMLUtil.formatXML(curDocument);
    } catch (Exception e) {
        _log.error(e, e);
    }

    return curContent;
}

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  www . ja v a2  s.  c  o 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

public static String removeArticleLocale(Document document, String content, String languageId) {

    try {//from w  w w  .j  a va  2 s.c o m
        Element rootElement = document.getRootElement();

        String availableLocales = rootElement.attributeValue("available-locales");

        if (availableLocales == null) {
            return content;
        }

        availableLocales = StringUtil.removeFromList(availableLocales, languageId);

        if (availableLocales.endsWith(",")) {
            availableLocales = availableLocales.substring(0, availableLocales.length() - 1);
        }

        rootElement.addAttribute("available-locales", availableLocales);

        removeArticleLocale(rootElement, languageId);

        content = XMLUtil.formatXML(document);
    } catch (Exception e) {
        _log.error(e, e);
    }

    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 {/*from  w  w w  . j  a v  a2 s  .  c  o m*/
            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.knowledgebase.admin.lar.AdminPortletDataHandler.java

License:Open Source License

@Override
protected String doExportData(PortletDataContext portletDataContext, String portletId,
        PortletPreferences portletPreferences) throws Exception {

    portletDataContext.addPortletPermissions(RESOURCE_NAME);

    Element rootElement = addExportDataRootElement(portletDataContext);

    rootElement.addAttribute("group-id", String.valueOf(portletDataContext.getScopeGroupId()));

    ActionableDynamicQuery kbArticleActionableDynamicQuery = getKBArticleActionableDynamicQuery(
            portletDataContext);/*from   www .  ja va  2  s.c om*/

    kbArticleActionableDynamicQuery.performActions();

    ActionableDynamicQuery kbTemplateActionableDynamicQuery = KBTemplateLocalServiceUtil
            .getExportActionableDynamicQuery(portletDataContext);

    kbTemplateActionableDynamicQuery.performActions();

    ActionableDynamicQuery kbCommentActionableDynamicQuery = getKBCommentActionableDynamicQuery(
            portletDataContext);

    kbCommentActionableDynamicQuery.performActions();

    return getExportDataRootElementString(rootElement);
}