protected boolean isValidStructureField(long groupId, String structureId, String contentField) { if (contentField.equals(JournalFeedConstants.WEB_CONTENT_DESCRIPTION) || contentField.equals(JournalFeedConstants.RENDERED_WEB_CONTENT)) { return true; } try { DDMStructure ddmStructure = ddmStructureLocalService.getStructure( groupId, classNameLocalService.getClassNameId(JournalArticle.class), structureId); Document document = SAXReaderUtil.read(ddmStructure.getXsd()); contentField = HtmlUtil.escapeXPathAttribute(contentField); XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element[@name=" + contentField + "]"); Node node = xPathSelector.selectSingleNode(document); if (node != null) { return true; } } catch (Exception e) { _log.error(e, e); } return false; }
protected void validate(Document document) throws Exception { XPath xPathSelector = _saxReader.createXPath("//dynamic-element"); List<Node> nodes = xPathSelector.selectNodes(document); Set<String> elementNames = new HashSet<>(); for (Node node : nodes) { Element element = (Element) node; String name = StringUtil.toLowerCase(element.attributeValue("name")); if (Validator.isNull(name)) { throw new StructureDefinitionException( "Element must have a name attribute " + element.formattedString()); } if (name.startsWith(DDMStructureConstants.XSD_NAME_RESERVED)) { throw new StructureDefinitionException("Element name " + name + " is reserved"); } if (elementNames.contains(name)) { throw new StructureDuplicateElementException( "Element with name " + name + " already exists"); } elementNames.add(name); } }
protected boolean isValidStructureField(long groupId, String structureId, String contentField) { if (contentField.equals(JournalFeedConstants.WEB_CONTENT_DESCRIPTION) || contentField.equals(JournalFeedConstants.RENDERED_WEB_CONTENT)) { return true; } else { try { JournalStructure structure = journalStructurePersistence.findByG_S(groupId, structureId); Document document = SAXReaderUtil.read(structure.getXsd()); XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element[@name='" + contentField + "']"); Node node = xPathSelector.selectSingleNode(document); if (node != null) { return true; } } catch (Exception e) { _log.error(e, e); } } return false; }
protected List<Node> getElementsByName(Document document, String name) { name = HtmlUtil.escapeXPathAttribute(name); XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element[@name=".concat(name).concat("]")); return xPathSelector.selectNodes(document); }
@Test public void testGetFieldsFromContentWithDocumentLibraryElement() throws Exception { Fields expectedFields = new Fields(); ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext(group.getGroupId(), TestPropsValues.getUserId()); FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry( TestPropsValues.getUserId(), group.getGroupId(), DLFolderConstants.DEFAULT_PARENT_FOLDER_ID, "Test 1.txt", ContentTypes.TEXT_PLAIN, RandomTestUtil.randomBytes(), serviceContext); Field documentLibraryField = getDocumentLibraryField(fileEntry, _ddmStructure.getStructureId()); expectedFields.put(documentLibraryField); Field fieldsDisplayField = getFieldsDisplayField(_ddmStructure.getStructureId(), "document_library_INSTANCE_4aGOvP3N"); expectedFields.put(fieldsDisplayField); String content = read("test-journal-content-doc-library-field.xml"); XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-content"); Document document = SAXReaderUtil.read(content); Element element = (Element) xPathSelector.selectSingleNode(document); String[] previewURLs = new String[2]; previewURLs[0] = DLUtil.getPreviewURL( fileEntry, fileEntry.getFileVersion(), null, StringPool.BLANK, true, true); previewURLs[1] = DLUtil.getPreviewURL( fileEntry, fileEntry.getFileVersion(), null, StringPool.BLANK, false, false); for (int i = 0; i < previewURLs.length; i++) { element.addCDATA(previewURLs[i]); Fields actualFields = JournalConverterUtil.getDDMFields(_ddmStructure, document.asXML()); Assert.assertEquals(expectedFields, actualFields); } }
private void _indexFieldsMap(String locale) throws PortalException, SystemException { Map<String, Map<String, String>> fieldsMap = _localizedFieldsMap.get(locale); Map<String, Map<String, String>> transientFieldsMap = _localizedTransientFieldsMap.get(locale); if (fieldsMap != null) { return; } if (getParentStructureId() > 0) { DDMStructure parentStructure = DDMStructureLocalServiceUtil.getStructure(getParentStructureId()); fieldsMap = parentStructure.getFieldsMap(locale); transientFieldsMap = parentStructure.getTransientFieldsMap(locale); } else { fieldsMap = new LinkedHashMap<String, Map<String, String>>(); transientFieldsMap = new LinkedHashMap<String, Map<String, String>>(); } XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element"); List<Node> nodes = xPathSelector.selectNodes(getDocument()); for (Node node : nodes) { Element element = (Element) node; String name = element.attributeValue("name"); if (Validator.isNotNull(element.attributeValue("dataType"))) { fieldsMap.put(name, _getField(element, locale)); } else { transientFieldsMap.put(name, _getField(element, locale)); } } String[] privateFieldNames = PropsValues.DYNAMIC_DATA_MAPPING_STRUCTURE_PRIVATE_FIELD_NAMES; for (String privateFieldName : privateFieldNames) { Map<String, String> privateField = _getPrivateField(privateFieldName); fieldsMap.put(privateFieldName, privateField); } _localizedFieldsMap.put(locale, fieldsMap); _localizedTransientFieldsMap.put(locale, transientFieldsMap); }
private Map<String, String> _getField(Element element, String locale) { Map<String, String> field = new HashMap<String, String>(); List<String> availableLocales = getAvailableLanguageIds(); if ((locale != null) && !availableLocales.contains(locale)) { locale = getDefaultLanguageId(); } locale = HtmlUtil.escapeXPathAttribute(locale); 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()); } Element parentElement = element.getParent(); if (parentElement != null) { String parentName = parentElement.attributeValue("name"); if (Validator.isNotNull(parentName)) { field.put(_getPrivateAttributeKey("parentName"), parentName); } } return field; }
protected void validate(Document document) throws Exception { XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element"); List<Node> nodes = xPathSelector.selectNodes(document); Set<String> elementNames = new HashSet<>(); for (Node node : nodes) { Element element = (Element) node; String name = StringUtil.toLowerCase(element.attributeValue("name")); if (name.startsWith(DDMStructureConstants.XSD_NAME_RESERVED)) { throw new StructureDefinitionException(); } if (elementNames.contains(name)) { throw new StructureDuplicateElementException(); } elementNames.add(name); } }
@Override public Fields getFields(DDMStructure structure, XPath xPath, String xml, List<String> fieldNames) throws PortalException { Document document = null; try { document = SAXReaderUtil.read(xml); } catch (DocumentException de) { return null; } if ((xPath != null) && !xPath.booleanValueOf(document)) { return null; } Fields fields = new Fields(); Element rootElement = document.getRootElement(); List<Element> dynamicElementElements = rootElement.elements("dynamic-element"); for (Element dynamicElementElement : dynamicElementElements) { String fieldName = dynamicElementElement.attributeValue("name"); if (!structure.hasField(fieldName) || ((fieldNames != null) && !fieldNames.contains(fieldName))) { continue; } String fieldDataType = structure.getFieldDataType(fieldName); List<Element> dynamicContentElements = dynamicElementElement.elements("dynamic-content"); for (Element dynamicContentElement : dynamicContentElements) { String fieldValue = dynamicContentElement.getText(); String languageId = dynamicContentElement.attributeValue("language-id"); Locale locale = LocaleUtil.fromLanguageId(languageId); Serializable fieldValueSerializable = FieldConstants.getSerializable(fieldDataType, fieldValue); Field field = fields.get(fieldName); if (field == null) { field = new Field(); String defaultLanguageId = dynamicElementElement.attributeValue("default-language-id"); Locale defaultLocale = LocaleUtil.fromLanguageId(defaultLanguageId); field.setDefaultLocale(defaultLocale); field.setDDMStructureId(structure.getStructureId()); field.setName(fieldName); field.setValue(locale, fieldValueSerializable); fields.put(field); } else { field.addValue(locale, fieldValueSerializable); } } } return fields; }
protected String processContent( JournalFeed feed, JournalArticle article, String languageId, ThemeDisplay themeDisplay, SyndEntry syndEntry, SyndContent syndContent) throws Exception { String content = article.getDescription(languageId); String contentField = feed.getContentField(); if (contentField.equals(JournalFeedConstants.RENDERED_WEB_CONTENT)) { String rendererTemplateId = article.getTemplateId(); if (Validator.isNotNull(feed.getRendererTemplateId())) { rendererTemplateId = feed.getRendererTemplateId(); } JournalArticleDisplay articleDisplay = JournalContentUtil.getDisplay( feed.getGroupId(), article.getArticleId(), rendererTemplateId, null, languageId, themeDisplay, 1, _XML_REQUUEST); if (articleDisplay != null) { content = articleDisplay.getContent(); } } else if (!contentField.equals(JournalFeedConstants.WEB_CONTENT_DESCRIPTION)) { Document document = SAXReaderUtil.read(article.getContentByLocale(languageId)); contentField = HtmlUtil.escapeXPathAttribute(contentField); XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element[@name=" + contentField + "]"); List<Node> results = xPathSelector.selectNodes(document); if (results.size() == 0) { return content; } Element element = (Element) results.get(0); String elType = element.attributeValue("type"); if (elType.equals("document_library")) { String url = element.elementText("dynamic-content"); url = processURL(feed, url, themeDisplay, syndEntry); } else if (elType.equals("image") || elType.equals("image_gallery")) { String url = element.elementText("dynamic-content"); url = processURL(feed, url, themeDisplay, syndEntry); content = content + "<br /><br /><img alt='' src='" + themeDisplay.getURLPortal() + url + "' />"; } else if (elType.equals("text_box")) { syndContent.setType("text"); content = element.elementText("dynamic-content"); } else { content = element.elementText("dynamic-content"); } } return content; }