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

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

Introduction

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

Prototype

public String attributeValue(String name, String defaultValue);

Source Link

Usage

From source file:com.liferay.portlet.dynamicdatamapping.util.DDMXSDImpl.java

License:Open Source License

public String getHTML(PageContext pageContext, Element element, Fields fields, String namespace, String mode,
        boolean readOnly, Locale locale) throws Exception {

    StringBundler sb = new StringBundler();

    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();

    String portletId = PortalUtil.getPortletId(request);

    String portletNamespace = PortalUtil.getPortletNamespace(portletId);

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

    for (Element dynamicElementElement : dynamicElementElements) {
        FreeMarkerContext freeMarkerContext = getFreeMarkerContext(dynamicElementElement, locale);

        freeMarkerContext.put("portletNamespace", portletNamespace);
        freeMarkerContext.put("namespace", namespace);

        if (fields != null) {
            freeMarkerContext.put("fields", fields);
        }/*from w  w w .ja v a2  s.  c om*/

        Map<String, Object> field = (Map<String, Object>) freeMarkerContext.get("fieldStructure");

        String childrenHTML = getHTML(pageContext, dynamicElementElement, fields, namespace, mode, readOnly,
                locale);

        field.put("children", childrenHTML);

        String fieldNamespace = dynamicElementElement.attributeValue("fieldNamespace", _DEFAULT_NAMESPACE);

        String defaultResourcePath = _TPL_DEFAULT_PATH;

        boolean fieldReadOnly = GetterUtil.getBoolean(field.get("readOnly"));

        if ((fieldReadOnly && Validator.isNotNull(mode)
                && mode.equalsIgnoreCase(DDMTemplateConstants.TEMPLATE_MODE_EDIT)) || readOnly) {

            fieldNamespace = _DEFAULT_READ_ONLY_NAMESPACE;

            defaultResourcePath = _TPL_DEFAULT_READ_ONLY_PATH;
        }

        String type = dynamicElementElement.attributeValue("type");

        String templateName = StringUtil.replaceFirst(type, fieldNamespace.concat(StringPool.DASH),
                StringPool.BLANK);

        StringBundler resourcePath = new StringBundler(5);

        resourcePath.append(_TPL_PATH);
        resourcePath.append(fieldNamespace.toLowerCase());
        resourcePath.append(CharPool.SLASH);
        resourcePath.append(templateName);
        resourcePath.append(_TPL_EXT);

        sb.append(processFTL(pageContext, freeMarkerContext, resourcePath.toString(), defaultResourcePath));
    }

    return sb.toString();
}

From source file:com.liferay.portlet.journal.action.ViewArticleContentAction.java

License:Open Source License

protected void format(long groupId, String articleId, double version, String previewArticleId, Element root,
        UploadServletRequest uploadServletRequest) throws Exception {

    Iterator<Element> itr = root.elements().iterator();

    while (itr.hasNext()) {
        Element el = itr.next();

        Element dynamicContent = el.element("dynamic-content");

        String elInstanceId = el.attributeValue("instance-id", StringPool.BLANK);
        String elName = el.attributeValue("name", StringPool.BLANK);
        String elType = el.attributeValue("type", StringPool.BLANK);
        String elContent = StringPool.BLANK;
        String elLanguage = StringPool.BLANK;

        if (dynamicContent != null) {
            elContent = dynamicContent.getTextTrim();

            elLanguage = dynamicContent.attributeValue("language-id", StringPool.BLANK);

            if (!elLanguage.equals(StringPool.BLANK)) {
                elLanguage = "_" + elLanguage;
            }//w ww .  ja va2 s  .  c o m
        }

        if (elType.equals("image") && Validator.isNull(elContent)) {
            File file = uploadServletRequest.getFile("structure_image_" + elName + elLanguage);
            byte[] bytes = FileUtil.getBytes(file);

            if ((bytes != null) && (bytes.length > 0)) {
                long imageId = JournalArticleImageLocalServiceUtil.getArticleImageId(groupId, previewArticleId,
                        version, elInstanceId, elName, elLanguage, true);

                String token = WebServerServletTokenUtil.getToken(imageId);

                dynamicContent.setText("/image/journal/article?img_id=" + imageId + "&t=" + token);

                ImageLocalServiceUtil.updateImage(imageId, bytes);
            } else {
                if (Validator.isNotNull(articleId)) {
                    long imageId = JournalArticleImageLocalServiceUtil.getArticleImageId(groupId, articleId,
                            version, elInstanceId, elName, elLanguage);

                    String token = WebServerServletTokenUtil.getToken(imageId);

                    dynamicContent.setText("/image/journal/article?img_id=" + imageId + "&t=" + token);
                }
            }
        }

        format(groupId, articleId, version, previewArticleId, el, uploadServletRequest);
    }
}

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

License:Open Source License

protected void format(User user, long groupId, String articleId, double version, boolean incrementVersion,
        Element root, Map<String, byte[]> images) throws PortalException, SystemException {

    for (Element element : root.elements()) {
        String elInstanceId = element.attributeValue("instance-id", StringPool.BLANK);
        String elName = element.attributeValue("name", StringPool.BLANK);
        String elType = element.attributeValue("type", StringPool.BLANK);

        if (elType.equals("image")) {
            formatImage(groupId, articleId, version, incrementVersion, element, elInstanceId, elName, images);
        } else if (elType.equals("text_area") || elType.equals("text") || elType.equals("text_box")) {

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

            for (Element dynamicContentElement : dynamicContentElements) {
                String dynamicContent = dynamicContentElement.getText();

                if (Validator.isNotNull(dynamicContent)) {
                    dynamicContent = SanitizerUtil.sanitize(user.getCompanyId(), groupId, user.getUserId(),
                            JournalArticle.class.getName(), 0, ContentTypes.TEXT_HTML, dynamicContent);

                    dynamicContentElement.setText(dynamicContent);
                }/* ww  w .jav  a  2s .  c  o m*/
            }
        }

        format(user, groupId, articleId, version, incrementVersion, element, images);
    }
}

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

License:Open Source License

protected void formatImage(long groupId, String articleId, double version, boolean incrementVersion, Element el,
        String elInstanceId, String elName, Map<String, byte[]> images)
        throws PortalException, SystemException {

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

    for (Element dynamicContent : imageContents) {
        String elLanguage = dynamicContent.attributeValue("language-id", StringPool.BLANK);

        if (!elLanguage.equals(StringPool.BLANK)) {
            elLanguage = "_" + elLanguage;
        }//w  ww . j  av  a  2  s  .c  o  m

        long imageId = journalArticleImageLocalService.getArticleImageId(groupId, articleId, version,
                elInstanceId, elName, elLanguage);

        double oldVersion = MathUtil.format(version - 0.1, 1, 1);

        long oldImageId = 0;

        if ((oldVersion >= 1) && incrementVersion) {
            oldImageId = journalArticleImageLocalService.getArticleImageId(groupId, articleId, oldVersion,
                    elInstanceId, elName, elLanguage);
        }

        String elContent = "/image/journal/article?img_id=" + imageId + "&t="
                + WebServerServletTokenUtil.getToken(imageId);

        if (dynamicContent.getText().equals("delete")) {
            dynamicContent.setText(StringPool.BLANK);

            imageLocalService.deleteImage(imageId);

            String defaultElLanguage = "";

            if (!Validator.isNotNull(elLanguage)) {
                defaultElLanguage = "_" + LocaleUtil.toLanguageId(LocaleUtil.getDefault());
            }

            long defaultImageId = journalArticleImageLocalService.getArticleImageId(groupId, articleId, version,
                    elInstanceId, elName, defaultElLanguage);

            imageLocalService.deleteImage(defaultImageId);

            continue;
        }

        byte[] bytes = images.get(elInstanceId + "_" + elName + elLanguage);

        if ((bytes != null) && (bytes.length > 0)) {
            dynamicContent.setText(elContent);
            dynamicContent.addAttribute("id", String.valueOf(imageId));

            imageLocalService.updateImage(imageId, bytes);

            continue;
        }

        if ((version > JournalArticleConstants.VERSION_DEFAULT) && (incrementVersion)) {

            Image oldImage = null;

            if (oldImageId > 0) {
                oldImage = imageLocalService.getImage(oldImageId);
            }

            if (oldImage != null) {
                dynamicContent.setText(elContent);
                dynamicContent.addAttribute("id", String.valueOf(imageId));

                bytes = oldImage.getTextObj();

                imageLocalService.updateImage(imageId, bytes);
            }

            continue;
        }

        Image image = imageLocalService.getImage(imageId);

        if (image != null) {
            dynamicContent.setText(elContent);
            dynamicContent.addAttribute("id", String.valueOf(imageId));

            continue;
        }

        long contentImageId = GetterUtil.getLong(HttpUtil.getParameter(dynamicContent.getText(), "img_id"));

        if (contentImageId <= 0) {
            contentImageId = GetterUtil
                    .getLong(HttpUtil.getParameter(dynamicContent.getText(), "img_id", false));
        }

        if (contentImageId > 0) {
            image = imageLocalService.getImage(contentImageId);

            if (image != null) {
                dynamicContent.addAttribute("id", String.valueOf(contentImageId));

                continue;
            }
        }

        String defaultElLanguage = "";

        if (!Validator.isNotNull(elLanguage)) {
            defaultElLanguage = "_" + LocaleUtil.toLanguageId(LocaleUtil.getDefault());
        }

        long defaultImageId = journalArticleImageLocalService.getArticleImageId(groupId, articleId, version,
                elInstanceId, elName, defaultElLanguage);

        Image defaultImage = imageLocalService.getImage(defaultImageId);

        if (defaultImage != null) {
            dynamicContent.setText(elContent);
            dynamicContent.addAttribute("id", String.valueOf(defaultImageId));

            bytes = defaultImage.getTextObj();

            imageLocalService.updateImage(defaultImageId, bytes);

            continue;
        }

        if (Validator.isNotNull(elLanguage)) {
            dynamicContent.setText(StringPool.BLANK);
        }
    }
}

From source file:com.liferay.portlet.journal.service.impl.JournalStructureLocalServiceImpl.java

License:Open Source License

protected void validate(List<Element> elements, Set<String> elNames) throws PortalException {

    for (Element element : elements) {
        if (element.getName().equals("meta-data")) {
            continue;
        }//from www. j  a va 2  s. c om

        String elName = element.attributeValue("name", StringPool.BLANK);
        String elType = element.attributeValue("type", StringPool.BLANK);

        if (Validator.isNull(elName) || elName.startsWith(JournalStructureConstants.RESERVED)) {

            throw new StructureXsdException();
        } else {
            char[] c = elType.toCharArray();

            for (int i = 0; i < c.length; i++) {
                if ((!Validator.isChar(c[i])) && (!Validator.isDigit(c[i])) && (c[i] != CharPool.DASH)
                        && (c[i] != CharPool.UNDERLINE)) {

                    throw new StructureXsdException();
                }
            }

            String completePath = elName;

            Element parentElement = element.getParent();

            while (!parentElement.isRootElement()) {
                completePath = parentElement.attributeValue("name", StringPool.BLANK) + StringPool.SLASH
                        + completePath;

                parentElement = parentElement.getParent();
            }

            String elNameLowerCase = completePath.toLowerCase();

            if (elNames.contains(elNameLowerCase)) {
                throw new DuplicateStructureElementException();
            } else {
                elNames.add(elNameLowerCase);
            }
        }

        if (Validator.isNull(elType)) {
            throw new StructureXsdException();
        }

        validate(element.elements(), elNames);
    }
}

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

License:Open Source License

protected String getDynamicContent(String xml, String elementName) {
    String content = null;/*w  w w .  j  a  v a2  s.com*/

    try {
        Document document = SAXReaderUtil.read(xml);

        Element rootElement = document.getRootElement();

        for (Element element : rootElement.elements()) {
            String curElementName = element.attributeValue("name", StringPool.BLANK);

            if (curElementName.equals(elementName)) {
                content = element.elementText("dynamic-content");

                break;
            }
        }
    } catch (Exception e) {
        _log.error(e, e);
    }

    return GetterUtil.getString(content);
}

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

License:Open Source License

protected void processStructure(com.liferay.portal.kernel.xml.Document structureDocument, Document document,
        Element rootElement) throws Exception {

    LinkedList<Element> queue = new LinkedList<Element>(rootElement.elements());

    Element element = null;

    while ((element = queue.poll()) != null) {
        String elName = element.attributeValue("name", StringPool.BLANK);
        String elType = element.attributeValue("type", StringPool.BLANK);
        String elIndexType = element.attributeValue("index-type", StringPool.BLANK);

        if (structureDocument != null) {
            String path = element.getPath().concat("[@name='").concat(elName).concat("']");

            Node structureNode = structureDocument.selectSingleNode(path);

            if (structureNode != null) {
                Element structureElement = (Element) structureNode;

                elType = structureElement.attributeValue("type", StringPool.BLANK);
                elIndexType = structureElement.attributeValue("index-type", StringPool.BLANK);
            }//from  w  w w.jav  a2  s . c o m
        }

        if (Validator.isNotNull(elType)) {
            indexField(document, element, elType, elIndexType);
        }

        queue.addAll(element.elements());
    }
}

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

License:Open Source License

protected void replace(Element root) {
    List<Element> elements = root.elements();

    int listIndex = elements.size() - 1;

    while (listIndex >= 0) {
        Element element = elements.get(listIndex);

        String languageId = element.attributeValue("language-id", getLanguageId());

        if (!languageId.equalsIgnoreCase(getLanguageId())) {
            root.remove(element);//from  w w  w .  ja  v a 2s  .  com
        } else {
            replace(element);
        }

        listIndex--;
    }
}

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

License:Open Source License

protected String replace(String xml) {
    if (xml == null) {
        return xml;
    }//  w  w w .j a  v a  2s. c o m

    _requestedLocale = getLanguageId();

    try {
        Document document = SAXReaderUtil.read(xml);

        Element rootElement = document.getRootElement();

        String defaultLanguageId = LocaleUtil.toLanguageId(LocaleUtil.getDefault());

        String[] availableLocales = StringUtil
                .split(rootElement.attributeValue("available-locales", defaultLanguageId));

        String defaultLocale = rootElement.attributeValue("default-locale", defaultLanguageId);

        boolean supportedLocale = false;

        for (String availableLocale : availableLocales) {
            if (availableLocale.equalsIgnoreCase(getLanguageId())) {
                supportedLocale = true;

                break;
            }
        }

        if (!supportedLocale) {
            setLanguageId(defaultLocale);
        }

        replace(rootElement);

        xml = DDMXMLUtil.formatXML(document);
    } catch (Exception e) {
        _log.error(e);
    }

    return xml;
}

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

License:Open Source License

@Override
protected List<TemplateNode> getTemplateNodes(Element element) throws Exception {

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

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

    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();
        }/* www. jav a  2 s .com*/

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

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

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

        TemplateNode templateNode = new TemplateNode(getThemeDisplay(), name, stripCDATA(data), type);

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

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

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

        TemplateNode prototypeTemplateNode = prototypeTemplateNodes.get(name);

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

            prototypeTemplateNodes.put(name, prototypeTemplateNode);

            templateNodes.add(templateNode);
        }

        prototypeTemplateNode.appendSibling(templateNode);
    }

    return templateNodes;
}