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

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

Introduction

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

Prototype

public static Document read(URL url) throws DocumentException 

Source Link

Usage

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

License:Open Source License

@Override
public Document getDocument() {
    if (_document == null) {
        try {//from  w w  w.j  a v  a  2 s. c  o  m
            _document = SAXReaderUtil.read(getXsd());
        } catch (Exception e) {
            StackTraceElement[] stackTraceElements = e.getStackTrace();

            for (StackTraceElement stackTraceElement : stackTraceElements) {

                String className = stackTraceElement.getClassName();

                if (className.endsWith("DDMStructurePersistenceTest")) {
                    return null;
                }
            }

            _log.error(e, e);
        }
    }

    return _document;
}

From source file:com.liferay.portlet.dynamicdatamapping.service.impl.DDMContentLocalServiceImpl.java

License:Open Source License

protected void validate(String name, String xml) throws PortalException {
    if (Validator.isNull(name)) {
        throw new ContentNameException();
    }/*w w w .  j a  va  2 s . co m*/

    if (Validator.isNull(xml)) {
        throw new ContentException();
    }

    try {
        SAXReaderUtil.read(xml);
    } catch (DocumentException de) {
        throw new ContentException();
    }
}

From source file:com.liferay.portlet.dynamicdatamapping.service.impl.DDMStructureLocalServiceImpl.java

License:Open Source License

protected void appendNewStructureRequiredFields(DDMStructure structure, Document templateDocument) {

    String xsd = structure.getXsd();

    Document structureDocument = null;

    try {//  www  .j  a v  a  2s  .co  m
        structureDocument = SAXReaderUtil.read(xsd);
    } catch (DocumentException de) {
        if (_log.isWarnEnabled()) {
            _log.warn(de, de);
        }

        return;
    }

    Element templateElement = templateDocument.getRootElement();

    XPath structureXPath = SAXReaderUtil
            .createXPath("//dynamic-element[.//meta-data/entry[@name=\"required\"]=" + "\"true\"]");

    List<Node> nodes = structureXPath.selectNodes(structureDocument);

    Iterator<Node> itr = nodes.iterator();

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

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

        XPath templateXPath = SAXReaderUtil.createXPath("//dynamic-element[@name=\"" + name + "\"]");

        if (!templateXPath.booleanValueOf(templateDocument)) {
            templateElement.add(element.createCopy());
        }
    }
}

From source file:com.liferay.portlet.dynamicdatamapping.service.impl.DDMStructureLocalServiceImpl.java

License:Open Source License

protected void syncStructureTemplatesFields(DDMStructure structure) throws PortalException, SystemException {

    List<DDMTemplate> templates = ddmTemplateLocalService.getTemplates(structure.getStructureId(),
            DDMTemplateConstants.TEMPLATE_TYPE_DETAIL);

    for (DDMTemplate template : templates) {
        String script = template.getScript();

        Document templateDocument = null;

        try {/*from   w ww  .  j  ava 2 s . c  om*/
            templateDocument = SAXReaderUtil.read(script);
        } catch (DocumentException de) {
            if (_log.isWarnEnabled()) {
                _log.warn(de, de);
            }

            continue;
        }

        Element templateRootElement = templateDocument.getRootElement();

        syncStructureTemplatesFields(template, templateRootElement);

        appendNewStructureRequiredFields(structure, templateDocument);

        try {
            script = DDMXMLUtil.formatXML(templateDocument.asXML());
        } catch (Exception e) {
            throw new StructureXsdException();
        }

        template.setScript(script);

        ddmTemplatePersistence.update(template, false);
    }
}

From source file:com.liferay.portlet.dynamicdatamapping.service.impl.DDMStructureLocalServiceImpl.java

License:Open Source License

protected void validate(Map<Locale, String> nameMap, String xsd) throws PortalException {

    validateName(nameMap);//from w ww  .j a va2  s.c  om

    if (Validator.isNull(xsd)) {
        throw new StructureXsdException();
    } else {
        try {
            List<Element> elements = new ArrayList<Element>();

            Document document = SAXReaderUtil.read(xsd);

            Element rootElement = document.getRootElement();

            if (rootElement.elements().isEmpty()) {
                throw new StructureXsdException();
            }

            elements.addAll(rootElement.elements());

            Set<String> elNames = new HashSet<String>();

            validate(elements, elNames);
        } catch (StructureDuplicateElementException fdsee) {
            throw fdsee;
        } catch (StructureXsdException sxe) {
            throw sxe;
        } catch (Exception e) {
            throw new StructureXsdException();
        }
    }
}

From source file:com.liferay.portlet.dynamicdatamapping.storage.XMLStorageAdapter.java

License:Open Source License

@Override
protected int doQueryCount(long ddmStructureId, Condition condition) throws Exception {

    XPath conditionXPath = null;//from   w  w w  .ja va2 s. co m

    if (condition != null) {
        conditionXPath = _parseCondition(condition);
    }

    int count = 0;

    long[] classPKs = _getStructureClassPKs(ddmStructureId);

    for (long classPK : classPKs) {
        DDMContent ddmContent = DDMContentLocalServiceUtil.getContent(classPK);

        Document document = SAXReaderUtil.read(ddmContent.getXml());

        if ((conditionXPath == null) || ((conditionXPath != null) && conditionXPath.booleanValueOf(document))) {

            count++;
        }
    }

    return count;
}

From source file:com.liferay.portlet.dynamicdatamapping.storage.XMLStorageAdapter.java

License:Open Source License

@Override
protected void doUpdate(long classPK, Fields fields, boolean mergeFields, ServiceContext serviceContext)
        throws Exception {

    DDMContent ddmContent = DDMContentLocalServiceUtil.getContent(classPK);

    Document document = null;/*w  w w  .  j  ava2s .  c  o  m*/

    Element rootElement = null;

    if (mergeFields) {
        document = SAXReaderUtil.read(ddmContent.getXml());

        rootElement = document.getRootElement();
    } else {
        document = SAXReaderUtil.createDocument();

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

    Iterator<Field> itr = fields.iterator();

    while (itr.hasNext()) {
        Field field = itr.next();

        Object value = field.getValue();

        if (value instanceof Date) {
            Date valueDate = (Date) value;

            value = valueDate.getTime();
        }

        String fieldName = field.getName();
        String fieldValue = String.valueOf(value);

        Element dynamicElementElement = _getElementByName(document, fieldName);

        if (dynamicElementElement == null) {
            _appendField(rootElement, fieldName, fieldValue);
        } else {
            _updateField(dynamicElementElement, fieldName, fieldValue);
        }
    }

    ddmContent.setModifiedDate(serviceContext.getModifiedDate(null));
    ddmContent.setXml(document.formattedString());

    DDMContentLocalServiceUtil.updateContent(ddmContent.getPrimaryKey(), ddmContent.getName(),
            ddmContent.getDescription(), ddmContent.getXml(), serviceContext);
}

From source file:com.liferay.portlet.dynamicdatamapping.storage.XMLStorageAdapter.java

License:Open Source License

private List<Fields> _doQuery(long ddmStructureId, long[] classPKs, List<String> fieldNames,
        Condition condition, OrderByComparator orderByComparator) throws Exception {

    List<Fields> fieldsList = new ArrayList<Fields>();

    XPath conditionXPath = null;//from  www. j av  a  2  s  . co  m

    if (condition != null) {
        conditionXPath = _parseCondition(condition);
    }

    DDMStructure ddmStructure = DDMStructureLocalServiceUtil.getDDMStructure(ddmStructureId);

    for (long classPK : classPKs) {
        DDMContent ddmContent = DDMContentLocalServiceUtil.getContent(classPK);

        Document document = SAXReaderUtil.read(ddmContent.getXml());

        if ((conditionXPath != null) && !conditionXPath.booleanValueOf(document)) {

            continue;
        }

        Fields fields = new Fields();

        Element rootElement = document.getRootElement();

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

        for (Element dynamicElementElement : dynamicElementElements) {
            String fieldName = dynamicElementElement.attributeValue("name");
            String fieldValue = dynamicElementElement.elementText("dynamic-content");

            if (!ddmStructure.hasField(fieldName)
                    || ((fieldNames != null) && !fieldNames.contains(fieldName))) {

                continue;
            }

            String fieldDataType = ddmStructure.getFieldDataType(fieldName);

            Serializable fieldValueSerializable = FieldConstants.getSerializable(fieldDataType, fieldValue);

            Field field = new Field(ddmStructureId, fieldName, fieldValueSerializable);

            fields.put(field);
        }

        fieldsList.add(fields);
    }

    if (orderByComparator != null) {
        Collections.sort(fieldsList, orderByComparator);
    }

    return fieldsList;
}

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

License:Open Source License

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    try {//w ww.j  a  v a 2  s.c om
        long groupId = ParamUtil.getLong(request, "groupId");
        String articleId = ParamUtil.getString(request, "articleId");

        String languageId = LanguageUtil.getLanguageId(request);

        JournalArticle article = JournalArticleServiceUtil.getLatestArticle(groupId, articleId,
                WorkflowConstants.STATUS_APPROVED);

        ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

        Map<String, String> tokens = JournalUtil.getTokens(groupId, themeDisplay);

        String xml = article.getContentByLocale(languageId);

        Document doc = SAXReaderUtil.read(xml);

        Element root = doc.getRootElement();

        addProcessingInstructions(doc, request, themeDisplay, article);

        JournalUtil.addAllReservedEls(root, tokens, article, languageId);

        xml = DDMXMLUtil.formatXML(doc);

        String contentType = ContentTypes.TEXT_XML_UTF8;

        String fileName = null;
        byte[] bytes = xml.getBytes();

        ServletResponseUtil.sendFile(request, response, fileName, bytes, contentType);

        return null;
    } catch (Exception e) {
        PortalUtil.sendError(e, request, response);

        return null;
    }
}

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

License:Open Source License

protected byte[] getContent(HttpServletRequest request, List<JournalArticle> articles) throws Exception {

    long groupId = ParamUtil.getLong(request, "groupId");

    String languageId = LanguageUtil.getLanguageId(request);

    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

    Map<String, String> tokens = JournalUtil.getTokens(groupId, themeDisplay);

    Document resultsDoc = SAXReaderUtil.createDocument(StringPool.UTF8);

    Element resultSetEl = resultsDoc.addElement("result-set");

    for (JournalArticle article : articles) {
        Element resultEl = resultSetEl.addElement("result");

        Document articleDoc = SAXReaderUtil.read(article.getContentByLocale(languageId));

        resultEl.content().add(articleDoc.getRootElement().createCopy());

        resultEl = resultEl.element("root");

        JournalUtil.addAllReservedEls(resultEl, tokens, article, languageId);
    }//from   w w  w  .ja v a 2s.  co  m

    return DDMXMLUtil.formatXML(resultsDoc).getBytes(StringPool.UTF8);
}