/* * Reads 'resource-bundle' property from portlet.xml * */ private static boolean readPortletXML( String xml, ServletContext servletContext, String bundleName) throws DocumentException { boolean resourceBundleSpecified = false; Document document = SAXReaderUtil.read(xml, true); Element rootElement = document.getRootElement(); for (Element portletElement : rootElement.elements("portlet")) { String resourceBundleString = portletElement.elementText("resource-bundle"); if (StringUtils.isNotBlank(resourceBundleString)) { String resourceBundleName = portletElement.elementText("resource-bundle").replace('.', '/'); if (resourceBundleName != null) { resourceBundleSpecified = true; processBundle(resourceBundleName, servletContext, bundleName); } } } return resourceBundleSpecified; }
private String _getAssetEntryXml(String assetEntryType, String assetEntryUuid) { String xml = null; try { Document document = SAXReaderUtil.createDocument(StringPool.UTF8); Element assetEntryElement = document.addElement("asset-entry"); Element assetEntryTypeElement = assetEntryElement.addElement("asset-entry-type"); assetEntryTypeElement.addText(assetEntryType); Element assetEntryUuidElement = assetEntryElement.addElement("asset-entry-uuid"); assetEntryUuidElement.addText(assetEntryUuid); xml = document.formattedString(StringPool.BLANK); } catch (IOException ioe) { if (_log.isWarnEnabled()) { _log.warn(ioe); } } return xml; }
protected void exportAssetTags(PortletDataContext portletDataContext) throws Exception { Document document = SAXReaderUtil.createDocument(); Element rootElement = document.addElement("tags"); Map<String, String[]> assetTagNamesMap = portletDataContext.getAssetTagNamesMap(); for (Map.Entry<String, String[]> entry : assetTagNamesMap.entrySet()) { String[] assetTagNameParts = StringUtil.split(entry.getKey(), CharPool.POUND); String className = assetTagNameParts[0]; String classPK = assetTagNameParts[1]; Element assetElement = rootElement.addElement("asset"); assetElement.addAttribute("class-name", className); assetElement.addAttribute("class-pk", classPK); assetElement.addAttribute("tags", StringUtil.merge(entry.getValue())); } List<AssetTag> assetTags = AssetTagServiceUtil.getGroupTags(portletDataContext.getScopeGroupId()); for (AssetTag assetTag : assetTags) { exportAssetTag(portletDataContext, assetTag, rootElement); } portletDataContext.addZipEntry( portletDataContext.getRootPath() + "/tags.xml", document.formattedString()); }
protected void readExpandoTables(PortletDataContext portletDataContext) throws Exception { String xml = portletDataContext.getZipEntryAsString( ExportImportPathUtil.getSourceRootPath(portletDataContext) + "/expando-tables.xml"); if (xml == null) { return; } Document document = SAXReaderUtil.read(xml); Element rootElement = document.getRootElement(); List<Element> expandoTableElements = rootElement.elements("expando-table"); for (Element expandoTableElement : expandoTableElements) { String className = expandoTableElement.attributeValue("class-name"); ExpandoTable expandoTable = null; try { expandoTable = _expandoTableLocalService.getDefaultTable(portletDataContext.getCompanyId(), className); } catch (NoSuchTableException nste) { expandoTable = _expandoTableLocalService.addDefaultTable(portletDataContext.getCompanyId(), className); } List<Element> expandoColumnElements = expandoTableElement.elements("expando-column"); for (Element expandoColumnElement : expandoColumnElements) { long columnId = GetterUtil.getLong(expandoColumnElement.attributeValue("column-id")); String name = expandoColumnElement.attributeValue("name"); int type = GetterUtil.getInteger(expandoColumnElement.attributeValue("type")); String defaultData = expandoColumnElement.elementText("default-data"); String typeSettings = expandoColumnElement.elementText("type-settings"); Serializable defaultDataObject = ExpandoConverterUtil.getAttributeFromString(type, defaultData); ExpandoColumn expandoColumn = _expandoColumnLocalService.getColumn(expandoTable.getTableId(), name); if (expandoColumn != null) { _expandoColumnLocalService.updateColumn( expandoColumn.getColumnId(), name, type, defaultDataObject); } else { expandoColumn = _expandoColumnLocalService.addColumn( expandoTable.getTableId(), name, type, defaultDataObject); } _expandoColumnLocalService.updateTypeSettings(expandoColumn.getColumnId(), typeSettings); portletDataContext.importPermissions( ExpandoColumn.class, columnId, expandoColumn.getColumnId()); } } }
protected void exportAssetLinks(PortletDataContext portletDataContext) throws Exception { Document document = SAXReaderUtil.createDocument(); Element rootElement = document.addElement("links"); Element exportDataRootElement = portletDataContext.getExportDataRootElement(); try { portletDataContext.setExportDataRootElement(rootElement); ActionableDynamicQuery linkActionableDynamicQuery = _assetLinkLocalService.getExportActionbleDynamicQuery(portletDataContext); linkActionableDynamicQuery.performActions(); for (long linkId : portletDataContext.getAssetLinkIds()) { AssetLink assetLink = _assetLinkLocalService.getAssetLink(linkId); StagedAssetLink stagedAssetLink = ModelAdapterUtil.adapt(assetLink, AssetLink.class, StagedAssetLink.class); portletDataContext.addClassedModel( portletDataContext.getExportDataElement(stagedAssetLink), ExportImportPathUtil.getModelPath(stagedAssetLink), stagedAssetLink); } } finally { portletDataContext.setExportDataRootElement(exportDataRootElement); } portletDataContext.addZipEntry( ExportImportPathUtil.getRootPath(portletDataContext) + "/links.xml", document.formattedString()); }
protected void read(ClassLoader classLoader, String source) throws Exception { InputStream is = classLoader.getResourceAsStream(source); if (is == null) { return; } if (_log.isDebugEnabled()) { _log.debug("Loading " + source); } Document document = SAXReaderUtil.read(is); Element rootElement = document.getRootElement(); for (Element sqlElement : rootElement.elements("sql")) { String file = sqlElement.attributeValue("file"); if (Validator.isNotNull(file)) { read(classLoader, file); } else { String id = sqlElement.attributeValue("id"); String content = transform(sqlElement.getText()); content = replaceIsNull(content); _sqlPool.put(id, content); } } }
@Override protected PortletPreferences doImportData( PortletDataContext portletDataContext, String portletId, PortletPreferences portletPreferences, String data) throws Exception { portletDataContext.importPermissions( "com.liferay.portlet.calendar", portletDataContext.getSourceGroupId(), portletDataContext.getScopeGroupId()); Document document = SAXReaderUtil.read(data); Element rootElement = document.getRootElement(); for (Element eventElement : rootElement.elements("event")) { String path = eventElement.attributeValue("path"); if (!portletDataContext.isPathNotProcessed(path)) { continue; } CalEvent event = (CalEvent) portletDataContext.getZipEntryAsObject(path); importEvent(portletDataContext, eventElement, event); } return null; }
public EARBuilder(String originalApplicationXML, String[] pluginFileNames) { try { Document document = SAXReaderUtil.read(new File(originalApplicationXML)); Element rootElement = document.getRootElement(); for (String pluginFileName : pluginFileNames) { Element moduleElement = rootElement.addElement("module"); Element webElement = moduleElement.addElement("web"); Element webURIElement = webElement.addElement("web-uri"); webURIElement.addText(pluginFileName); Element contextRootElement = webElement.addElement("context-root"); String contextRoot = _getContextRoot(pluginFileName); contextRootElement.addText(contextRoot); } FileUtil.write(originalApplicationXML, document.formattedString(), true); } catch (Exception e) { e.printStackTrace(); } }
protected void importSQL() throws Exception { Class<?> clazz = getClass(); ClassLoader classLoader = clazz.getClassLoader(); InputStream inputStream = classLoader.getResourceAsStream("/resources/data/sql.xml"); String xml = new String(FileUtil.getBytes(inputStream)); Document document = SAXReaderUtil.read(xml); Element rootElement = document.getRootElement(); List<Element> batchElements = rootElement.elements("batch"); for (Element batchElement : batchElements) { String testSQL = batchElement.elementText("test-sql"); int count = getCount(testSQL); if (count > 0) { continue; } String[] importSQLs = StringUtil.split(batchElement.elementText("import-sql"), StringPool.SEMICOLON); runSQL(importSQLs); } }
@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(DDMStructure.class, JournalArticle.class)); Assert.assertEquals( 1, manifestSummary.getModelAdditionCount(DDMTemplate.class, DDMStructure.class)); Assert.assertEquals(1, manifestSummary.getModelAdditionCount(JournalArticle.class)); Assert.assertEquals(1, manifestSummary.getModelAdditionCount(JournalFolder.class)); Document document = SAXReaderUtil.createDocument(); Element rootElement = document.addElement("root"); Element headerElement = rootElement.addElement("header"); _exportDate = new Date(); headerElement.addAttribute("export-date", Time.getRFC822(_exportDate)); ExportImportHelperUtil.writeManifestSummary(document, manifestSummary); zipWriter.addEntry("/manifest.xml", document.asXML()); }
private String _getJavadocXml(JavaClass javaClass) throws Exception { Element rootElement = _saxReaderUtil.createElement("javadoc"); Document document = _saxReaderUtil.createDocument(rootElement); DocUtil.add(rootElement, "name", javaClass.getName()); DocUtil.add(rootElement, "type", javaClass.getFullyQualifiedName()); _addClassCommentElement(rootElement, javaClass); _addDocletElements(rootElement, javaClass, "author"); _addDocletElements(rootElement, javaClass, "deprecated"); _addDocletElements(rootElement, javaClass, "see"); _addDocletElements(rootElement, javaClass, "serial"); _addDocletElements(rootElement, javaClass, "since"); _addDocletElements(rootElement, javaClass, "version"); JavaMethod[] javaMethods = javaClass.getMethods(); for (JavaMethod javaMethod : javaMethods) { _addMethodElement(rootElement, javaMethod); } JavaField[] javaFields = javaClass.getFields(); for (JavaField javaField : javaFields) { _addFieldElement(rootElement, javaField); } return document.formattedString(); }
public static String getUserPreference( long userId, long plid, String portletId, String preferenceName) { String _preference = StringPool.BLANK; PortletPreferences preferences = null; try { preferences = PortletPreferencesLocalServiceUtil.getPortletPreferences( userId, PortletKeys.PREFS_OWNER_TYPE_USER, plid, portletId); } catch (PortalException e) { // ignore } catch (SystemException e) { // ignore } if (Validator.isNull(preferences)) return _preference; Document document = null; try { document = SAXReaderUtil.read(preferences.getPreferences()); } catch (DocumentException e) { e.printStackTrace(); } for (Iterator<Element> itr = document.getRootElement().elementIterator("preference"); itr.hasNext(); ) { Element preference = (Element) itr.next(); if (preference.element("name").getText().equalsIgnoreCase(preferenceName)) { _preference = preference.element("value").getText(); } } return _preference; }
protected void exportLocks(PortletDataContext portletDataContext) throws Exception { Document document = SAXReaderUtil.createDocument(); Element rootElement = document.addElement("locks"); Map<String, Lock> locksMap = portletDataContext.getLocks(); for (Map.Entry<String, Lock> entry : locksMap.entrySet()) { Lock lock = entry.getValue(); String entryKey = entry.getKey(); int pos = entryKey.indexOf(CharPool.POUND); String className = entryKey.substring(0, pos); String key = entryKey.substring(pos + 1); String path = getLockPath(portletDataContext, className, key, lock); Element assetElement = rootElement.addElement("asset"); assetElement.addAttribute("path", path); assetElement.addAttribute("class-name", className); assetElement.addAttribute("key", key); portletDataContext.addZipEntry(path, lock); } portletDataContext.addZipEntry( ExportImportPathUtil.getRootPath(portletDataContext) + "/locks.xml", document.formattedString()); }
@Override public String getXML(Document document, Fields fields) { Element rootElement = null; try { if (document != null) { rootElement = document.getRootElement(); } else { document = SAXReaderUtil.createDocument(); rootElement = document.addElement("root"); } Iterator<Field> itr = fields.iterator(true); while (itr.hasNext()) { Field field = itr.next(); List<Node> nodes = getElementsByName(document, field.getName()); for (Node node : nodes) { document.remove(node); } appendField(rootElement, field); } return document.formattedString(); } catch (IOException ioe) { throw new SystemException(ioe); } }
protected void validate(String name, String description, String xsd) throws PortalException { if (Validator.isNull(name)) { throw new StructureNameException(); } else if (Validator.isNull(description)) { throw new StructureDescriptionException(); } if (Validator.isNull(xsd)) { throw new StructureXsdException(); } else { try { Document doc = SAXReaderUtil.read(xsd); Element root = doc.getRootElement(); List<Element> children = root.elements(); if (children.size() == 0) { throw new StructureXsdException(); } Set<String> elNames = new HashSet<String>(); validate(children, elNames); } catch (Exception e) { throw new StructureXsdException(); } } }
protected Set<String> getArticleFieldNames(long articleId) throws Exception { Set<String> articleFieldNames = new HashSet<>(); try (LoggingTimer loggingTimer = new LoggingTimer()) { String sql = "select JournalArticle.content from JournalArticle where " + "JournalArticle.id_ = ?"; try (PreparedStatement ps = connection.prepareStatement(sql)) { ps.setLong(1, articleId); try (ResultSet rs = ps.executeQuery()) { if (rs.next()) { String content = rs.getString("content"); Document document = SAXReaderUtil.read(content); Element rootElement = document.getRootElement(); articleFieldNames = getArticleDynamicElements(rootElement); } } } } return articleFieldNames; }
protected void exportAssetCategories(PortletDataContext portletDataContext) throws Exception { Document document = SAXReaderUtil.createDocument(); Element rootElement = document.addElement("categories-hierarchy"); Element assetVocabulariesElement = rootElement.addElement("vocabularies"); List<AssetVocabulary> assetVocabularies = AssetVocabularyLocalServiceUtil.getGroupVocabularies(portletDataContext.getGroupId()); for (AssetVocabulary assetVocabulary : assetVocabularies) { _portletExporter.exportAssetVocabulary( portletDataContext, assetVocabulariesElement, assetVocabulary); } Element categoriesElement = rootElement.addElement("categories"); List<AssetCategory> assetCategories = AssetCategoryUtil.findByGroupId(portletDataContext.getGroupId()); for (AssetCategory assetCategory : assetCategories) { _portletExporter.exportAssetCategory( portletDataContext, assetVocabulariesElement, categoriesElement, assetCategory); } _portletExporter.exportAssetCategories(portletDataContext, rootElement); portletDataContext.addZipEntry( portletDataContext.getRootPath() + "/categories-hierarchy.xml", document.formattedString()); }
protected void importAssetLinks(PortletDataContext portletDataContext) throws Exception { String xml = portletDataContext.getZipEntryAsString( ExportImportPathUtil.getSourceRootPath(portletDataContext) + "/links.xml"); if (xml == null) { return; } Element importDataRootElement = portletDataContext.getImportDataRootElement(); try { Document document = SAXReaderUtil.read(xml); Element rootElement = document.getRootElement(); portletDataContext.setImportDataRootElement(rootElement); Element linksElement = portletDataContext.getImportDataGroupElement(StagedAssetLink.class); List<Element> linkElements = linksElement.elements(); for (Element linkElement : linkElements) { StagedModelDataHandlerUtil.importStagedModel(portletDataContext, linkElement); } } finally { portletDataContext.setImportDataRootElement(importDataRootElement); } }
@Override protected PortletPreferences doImportData( PortletDataContext portletDataContext, String portletId, PortletPreferences portletPreferences, String data) throws Exception { Document document = SAXReaderUtil.read(data); Element rootElement = document.getRootElement(); if (portletDataContext.getBooleanParameter(_NAMESPACE, "wsrp-producers")) { Element wsrpProducersElement = rootElement.element("wsrp-producers"); importWSRPProducers(portletDataContext, wsrpProducersElement); } if (portletDataContext.getBooleanParameter(_NAMESPACE, "wsrp-consumers")) { Element wsrpConsumersElement = rootElement.element("wsrp-consumers"); importWSRPConsumers(portletDataContext, wsrpConsumersElement); } return null; }
protected String formatContent(String content) throws Exception { String oldCompanyId = (String) _companyIdColumn.getOldValue(); Long newCompanyId = (Long) _companyIdColumn.getNewValue(); Long groupId = (Long) _groupIdColumn.getNewValue(); String articleId = (String) _articleIdColumn.getNewValue(); Double version = (Double) _versionColumn.getNewValue(); try { Document doc = SAXReaderUtil.read(content); Element root = doc.getRootElement(); format( oldCompanyId, newCompanyId.longValue(), groupId.longValue(), articleId, version.doubleValue(), root); content = JournalUtil.formatXML(doc); } catch (Exception e) { _log.error( "Unable to format content for {articleId=" + articleId + ",version=" + version + "}: " + e.getMessage()); } return content; }
protected Document createSampleDocument() { Document document = createEmptyDocument(); addTextElement(document.getRootElement(), "Unlocalizable", "Text 2", false); return document; }
protected void readLocks(PortletDataContext portletDataContext) throws Exception { String xml = portletDataContext.getZipEntryAsString( ExportImportPathUtil.getSourceRootPath(portletDataContext) + "/locks.xml"); if (xml == null) { return; } Document document = SAXReaderUtil.read(xml); Element rootElement = document.getRootElement(); List<Element> assetElements = rootElement.elements("asset"); for (Element assetElement : assetElements) { String path = assetElement.attributeValue("path"); String className = assetElement.attributeValue("class-name"); String key = assetElement.attributeValue("key"); Lock lock = (Lock) portletDataContext.getZipEntryAsObject(path); if (lock != null) { portletDataContext.addLocks(className, key, lock); } } }
@Override public void removeAndStoreSelection( List<String> assetEntryUuids, PortletPreferences portletPreferences) throws Exception { if (assetEntryUuids.size() == 0) { return; } String[] assetEntryXmls = portletPreferences.getValues("assetEntryXml", new String[0]); List<String> assetEntryXmlsList = ListUtil.fromArray(assetEntryXmls); Iterator<String> itr = assetEntryXmlsList.iterator(); while (itr.hasNext()) { String assetEntryXml = itr.next(); Document document = SAXReaderUtil.read(assetEntryXml); Element rootElement = document.getRootElement(); String assetEntryUuid = rootElement.elementText("asset-entry-uuid"); if (assetEntryUuids.contains(assetEntryUuid)) { itr.remove(); } } portletPreferences.setValues( "assetEntryXml", assetEntryXmlsList.toArray(new String[assetEntryXmlsList.size()])); portletPreferences.store(); }
protected void exportAssetLinks(PortletDataContext portletDataContext) throws Exception { Document document = SAXReaderUtil.createDocument(); Element rootElement = document.addElement("links"); Map<String, String[]> assetLinkUuidsMap = portletDataContext.getAssetLinkUuidsMap(); for (Map.Entry<String, String[]> entry : assetLinkUuidsMap.entrySet()) { String[] assetLinkNameParts = StringUtil.split(entry.getKey(), CharPool.POUND); String[] targetAssetEntryUuids = entry.getValue(); String sourceAssetEntryUuid = assetLinkNameParts[0]; String assetLinkType = assetLinkNameParts[1]; Element assetElement = rootElement.addElement("asset-link"); assetElement.addAttribute("source-uuid", sourceAssetEntryUuid); assetElement.addAttribute("target-uuids", StringUtil.merge(targetAssetEntryUuids)); assetElement.addAttribute("type", assetLinkType); } portletDataContext.addZipEntry( portletDataContext.getRootPath() + "/links.xml", document.formattedString()); }
@Override public void setEvaluationModel(Course course, JSONObject model) throws PortalException, SystemException, DocumentException, IOException { Document document = SAXReaderUtil.createDocument(); Element rootElement = document.addElement("eval"); rootElement.addElement("courseEval").setText(PonderatedCourseEval.class.getName()); course.setCourseExtraData(document.formattedString()); }
protected void indexField(Document document, Element element, String elType, String elIndexType) { if (Validator.isNull(elIndexType)) { return; } com.liferay.portal.kernel.xml.Document structureDocument = element.getDocument(); Element rootElement = structureDocument.getRootElement(); String defaultLocale = GetterUtil.getString(rootElement.attributeValue("default-locale")); String name = encodeFieldName(element.attributeValue("name")); List<Element> dynamicContentElements = element.elements("dynamic-content"); for (Element dynamicContentElement : dynamicContentElements) { String contentLocale = GetterUtil.getString(dynamicContentElement.attributeValue("language-id")); String[] value = new String[] {dynamicContentElement.getText()}; if (elType.equals("multi-list")) { List<Element> optionElements = dynamicContentElement.elements("option"); value = new String[optionElements.size()]; for (int i = 0; i < optionElements.size(); i++) { value[i] = optionElements.get(i).getText(); } } if (elIndexType.equals("keyword")) { if (Validator.isNull(contentLocale)) { document.addKeyword(name, value); } else { if (defaultLocale.equals(contentLocale)) { document.addKeyword(name, value); } document.addKeyword(name.concat(StringPool.UNDERLINE).concat(contentLocale), value); } } else if (elIndexType.equals("text")) { if (Validator.isNull(contentLocale)) { document.addText(name, StringUtil.merge(value, StringPool.SPACE)); } else { if (defaultLocale.equals(contentLocale)) { document.addText(name, StringUtil.merge(value, StringPool.SPACE)); } document.addText( name.concat(StringPool.UNDERLINE).concat(contentLocale), StringUtil.merge(value, StringPool.SPACE)); } } } }
private Element _getDataEl() throws DocumentException { if (_dataEl == null) { Document doc = SAXReaderUtil.read(getData()); _dataEl = doc.getRootElement(); } return _dataEl; }
protected Document createDocument(String... fieldNames) { Document document = createEmptyDocument(); for (String fieldName : fieldNames) { addTextElement(document.getRootElement(), fieldName, fieldName, false); } return document; }
protected Definition doParse(InputStream inputStream) throws Exception { Document document = SAXReaderUtil.read(inputStream, _validate); Element rootElement = document.getRootElement(); String name = rootElement.elementText("name"); String description = rootElement.elementText("description"); int version = GetterUtil.getInteger(rootElement.elementText("version")); Definition definition = new Definition(name, description, document.formattedString(), version); List<Element> conditionElements = rootElement.elements("condition"); for (Element conditionElement : conditionElements) { Condition condition = parseCondition(conditionElement); definition.addNode(condition); } List<Element> forkElements = rootElement.elements("fork"); for (Element forkElement : forkElements) { Fork fork = parseFork(forkElement); definition.addNode(fork); } List<Element> joinElements = rootElement.elements("join"); for (Element joinElement : joinElements) { Join join = parseJoin(joinElement); definition.addNode(join); } List<Element> stateElements = rootElement.elements("state"); for (Element stateElement : stateElements) { State state = parseState(stateElement); definition.addNode(state); } List<Element> taskElements = rootElement.elements("task"); for (Element taskElement : taskElements) { Task task = parseTask(taskElement); definition.addNode(task); } parseTransitions( definition, conditionElements, forkElements, joinElements, stateElements, taskElements); return definition; }
protected List<Element> getDDMStructures(String fileName) throws DocumentException { String xml = ContentUtil.get("com/liferay/portal/events/dependencies/" + fileName); Document document = SAXReaderUtil.read(xml); Element rootElement = document.getRootElement(); return rootElement.elements("structure"); }