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

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

Introduction

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

Prototype

public static Document read(URL url) throws DocumentException 

Source Link

Usage

From source file:com.liferay.document.library.internal.instance.lifecycle.AddDefaultDocumentLibraryStructuresPortalInstanceLifecycleListener.java

License:Open Source License

protected void addDLRawMetadataStructures(long userId, long groupId, ServiceContext serviceContext)
        throws Exception {

    Locale locale = _portal.getSiteDefaultLocale(groupId);

    String xsd = buildDLRawMetadataXML(RawMetadataProcessorUtil.getFields(), locale);

    Document document = UnsecureSAXReaderUtil.read(new StringReader(xsd));

    Element rootElement = document.getRootElement();

    List<Element> structureElements = rootElement.elements("structure");

    for (Element structureElement : structureElements) {
        String name = structureElement.elementText("name");
        String description = structureElement.elementText("description");

        Element structureElementRootElement = structureElement.element("root");

        String structureElementRootXML = structureElementRootElement.asXML();

        DDMStructure ddmStructure = _ddmStructureLocalService.fetchStructure(groupId,
                _portal.getClassNameId(RawMetadataProcessor.class), name);

        DDMForm ddmForm = _ddmFormXSDDeserializer.deserialize(structureElementRootXML);

        if (ddmStructure != null) {
            ddmStructure.setDDMForm(ddmForm);

            _ddmStructureLocalService.updateDDMStructure(ddmStructure);
        } else {//from   w  ww.  j a  va 2  s  .c om
            Map<Locale, String> nameMap = new HashMap<>();

            nameMap.put(locale, name);

            Map<Locale, String> descriptionMap = new HashMap<>();

            descriptionMap.put(locale, description);

            DDMFormLayout ddmFormLayout = _ddm.getDefaultDDMFormLayout(ddmForm);

            _ddmStructureLocalService.addStructure(userId, groupId,
                    DDMStructureConstants.DEFAULT_PARENT_STRUCTURE_ID,
                    _portal.getClassNameId(RawMetadataProcessor.class), name, nameMap, descriptionMap, ddmForm,
                    ddmFormLayout, StorageType.JSON.toString(), DDMStructureConstants.TYPE_DEFAULT,
                    serviceContext);
        }
    }
}

From source file:com.liferay.dynamic.data.mapping.internal.util.DefaultDDMStructureHelperImpl.java

License:Open Source License

protected List<Element> getDDMStructures(ClassLoader classLoader, String fileName, Locale locale)
        throws Exception {

    String xml = StringUtil.read(classLoader, fileName);

    xml = StringUtil.replace(xml, "[$LOCALE_DEFAULT$]", locale.toString());

    Document document = UnsecureSAXReaderUtil.read(xml);

    Element rootElement = document.getRootElement();

    return rootElement.elements("structure");
}

From source file:com.liferay.dynamic.data.mapping.test.util.DDMStructureTestUtil.java

License:Open Source License

public static Map<String, Map<String, String>> getXSDMap(String xsd) throws Exception {

    Map<String, Map<String, String>> map = new HashMap<>();

    Document document = UnsecureSAXReaderUtil.read(xsd);

    XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element");

    List<Node> nodes = xPathSelector.selectNodes(document);

    for (Node node : nodes) {
        Element dynamicElementElement = (Element) node;

        String elementName = getElementName(dynamicElementElement);

        map.put(elementName, getElementMap(dynamicElementElement));
    }/*w w w.j  a v  a2s.co m*/

    return map;
}

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

License:Open Source License

public static Map<String, Map<String, String>> getXsdMap(String xsd) throws Exception {

    Map<String, Map<String, String>> map = new HashMap<>();

    Document document = UnsecureSAXReaderUtil.read(xsd);

    XPath xPathSelector = SAXReaderUtil.createXPath("//dynamic-element");

    List<Node> nodes = xPathSelector.selectNodes(document);

    for (Node node : nodes) {
        Element dynamicElementElement = (Element) node;

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

        if (Objects.equals(type, "selection_break")) {
            continue;
        }/*from   w w w  .  j  a v  a 2  s  .  c  o m*/

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

        map.put(name, _getMap(dynamicElementElement));
    }

    return map;
}

From source file:com.liferay.journal.transformer.test.JournalTransformerTest.java

License:Open Source License

@Test
public void testContentTransformerListener() throws Exception {
    DDMForm ddmForm = new DDMForm();

    ddmForm.addAvailableLocale(LocaleUtil.US);
    ddmForm.addDDMFormField(DDMFormTestUtil.createTextDDMFormField("link", false, false, false));
    ddmForm.addDDMFormField(DDMFormTestUtil.createTextDDMFormField("name", false, false, false));
    ddmForm.setDefaultLocale(LocaleUtil.US);

    _ddmStructure = DDMStructureTestUtil.addStructure(JournalArticle.class.getName(), ddmForm);

    String xsl = "$name.getData()";

    _ddmTemplate = DDMTemplateTestUtil.addTemplate(_ddmStructure.getStructureId(),
            PortalUtil.getClassNameId(JournalArticle.class), TemplateConstants.LANG_TYPE_VM, xsl);

    String xml = DDMStructureTestUtil.getSampleStructuredContent("Joe Bloggs");

    _article = JournalTestUtil.addArticleWithXMLContent(xml, _ddmStructure.getStructureKey(),
            _ddmTemplate.getTemplateKey());

    Map<String, String> tokens = getTokens();

    String content = JournalUtil.transform(null, tokens, Constants.VIEW, "en_US",
            UnsecureSAXReaderUtil.read(xml), null, xsl, TemplateConstants.LANG_TYPE_VM);

    Assert.assertEquals("Joe Bloggs", content);

    Document document = UnsecureSAXReaderUtil.read(xml);

    Element element = (Element) document.selectSingleNode("//dynamic-content");

    element.setText("[@" + _article.getArticleId() + ";name@]");

    content = JournalUtil.transform(null, tokens, Constants.VIEW, "en_US", document, null, xsl,
            TemplateConstants.LANG_TYPE_VM);

    Assert.assertEquals("Joe Bloggs", content);
}

From source file:com.liferay.journal.transformer.test.JournalTransformerTest.java

License:Open Source License

@Test
public void testFTLTransformation() throws Exception {
    Map<String, String> tokens = getTokens();

    String xml = DDMStructureTestUtil.getSampleStructuredContent("name", "Joe Bloggs");

    String script = "${name.getData()} - ${viewMode}";

    String content = JournalUtil.transform(null, tokens, Constants.PRINT, "en_US",
            UnsecureSAXReaderUtil.read(xml), null, script, TemplateConstants.LANG_TYPE_FTL);

    Assert.assertEquals("Joe Bloggs - print", content);
}

From source file:com.liferay.journal.transformer.test.JournalTransformerTest.java

License:Open Source License

@Test
public void testLocaleTransformerListener() throws Exception {
    Map<String, String> tokens = getTokens();

    Map<Locale, String> contents = new HashMap<>();

    contents.put(LocaleUtil.BRAZIL, "Joao da Silva");
    contents.put(LocaleUtil.US, "Joe Bloggs");

    String xml = DDMStructureTestUtil.getSampleStructuredContent(contents,
            LanguageUtil.getLanguageId(LocaleUtil.US));

    String script = "$name.getData()";

    String content = JournalUtil.transform(null, tokens, Constants.VIEW, "en_US",
            UnsecureSAXReaderUtil.read(xml), null, script, TemplateConstants.LANG_TYPE_VM);

    Assert.assertEquals("Joe Bloggs", content);

    content = JournalUtil.transform(null, tokens, Constants.VIEW, "pt_BR", UnsecureSAXReaderUtil.read(xml),
            null, script, TemplateConstants.LANG_TYPE_VM);

    Assert.assertEquals("Joao da Silva", content);

    content = JournalUtil.transform(null, tokens, Constants.VIEW, "fr_CA", UnsecureSAXReaderUtil.read(xml),
            null, script, TemplateConstants.LANG_TYPE_VM);

    Assert.assertEquals("Joe Bloggs", content);
}

From source file:com.liferay.journal.transformer.test.JournalTransformerTest.java

License:Open Source License

@Test
public void testLocaleTransformerListenerNestedFieldWithNoTranslation() throws Exception {

    Map<String, String> tokens = getTokens();

    Map<Locale, String> contents = new HashMap<>();

    contents.put(LocaleUtil.US, "Joe Bloggs");

    String xml = DDMStructureTestUtil.getSampleStructuredContent(contents,
            LanguageUtil.getLanguageId(LocaleUtil.US));

    Document document = UnsecureSAXReaderUtil.read(xml);

    Element rootElement = document.getRootElement();

    Attribute availableLocalesAttribute = rootElement.attribute("available-locales");

    availableLocalesAttribute.setValue("en_US,pt_BR");

    Element dynamicElement = (Element) document.selectSingleNode("//dynamic-element");

    dynamicElement.addElement("nestedElement");

    String script = "$name.getData()";

    String content = JournalUtil.transform(null, tokens, Constants.VIEW, "en_US", document, null, script,
            TemplateConstants.LANG_TYPE_VM);

    Assert.assertEquals("Joe Bloggs", content);

    content = JournalUtil.transform(null, tokens, Constants.VIEW, "pt_BR", document, null, script,
            TemplateConstants.LANG_TYPE_VM);

    Assert.assertEquals("Joe Bloggs", content);
}

From source file:com.liferay.journal.transformer.test.JournalTransformerTest.java

License:Open Source License

@Test
public void testRegexTransformerListener() throws Exception {
    initRegexTransformerUtil();//from ww w  .ja  v a 2 s .  c om

    Map<String, String> tokens = getTokens();

    String xml = DDMStructureTestUtil.getSampleStructuredContent("name", "Joe Bloggs");

    String script = "Hello $name.getData(), Welcome to beta.sample.com.";

    String content = JournalUtil.transform(null, tokens, Constants.VIEW, "en_US",
            UnsecureSAXReaderUtil.read(xml), null, script, TemplateConstants.LANG_TYPE_VM);

    Assert.assertEquals("Hello Joe Bloggs, Welcome to production.sample.com.", content);
}

From source file:com.liferay.journal.transformer.test.JournalTransformerTest.java

License:Open Source License

@Test
public void testTokensTransformerListener() throws Exception {
    Map<String, String> tokens = getTokens();

    String xml = DDMStructureTestUtil.getSampleStructuredContent();

    String script = "@company_id@";

    String content = JournalUtil.transform(null, tokens, Constants.VIEW, "en_US",
            UnsecureSAXReaderUtil.read(xml), null, script, TemplateConstants.LANG_TYPE_VM);

    Assert.assertEquals(String.valueOf(TestPropsValues.getCompanyId()), content);

    script = "@@company_id@@";

    content = JournalUtil.transform(null, tokens, Constants.VIEW, "en_US", UnsecureSAXReaderUtil.read(xml),
            null, script, TemplateConstants.LANG_TYPE_VM);

    Assert.assertEquals(String.valueOf(TestPropsValues.getCompanyId()), content);
}