Example usage for org.dom4j Document getRootElement

List of usage examples for org.dom4j Document getRootElement

Introduction

In this page you can find the example usage for org.dom4j Document getRootElement.

Prototype

Element getRootElement();

Source Link

Document

Returns the root Element for this document.

Usage

From source file:com.haulmont.cuba.desktop.theme.impl.DesktopThemeLoaderImpl.java

License:Apache License

private DesktopThemeImpl createTheme(String themeName, String[] locationList) {
    String themeClassName = null;
    for (String location : locationList) {
        String xmlLocation = getConfigFileName(themeName, location);
        Resource resource = resources.getResource(xmlLocation);
        if (resource.exists()) {
            try {
                Document doc = readXmlDocument(resource);
                final Element rootElement = doc.getRootElement();

                List<Element> classElements = rootElement.elements("class");
                if (!classElements.isEmpty()) {
                    themeClassName = classElements.get(0).getTextTrim();
                }//w  ww .j  a  v a  2s  .  c o m
            } catch (IOException e) {
                log.error("Error", e);
            }
        } else {
            log.warn("Resource " + location + " not found, ignore it");
        }
    }
    if (themeClassName != null) {
        try {
            Class themeClass = Class.forName(themeClassName);
            return (DesktopThemeImpl) themeClass.newInstance();
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
            throw new RuntimeException(e);
        }
    }
    return new DesktopThemeImpl();
}

From source file:com.haulmont.cuba.desktop.theme.impl.DesktopThemeLoaderImpl.java

License:Apache License

private void loadThemeFromXml(DesktopThemeImpl theme, Resource resource) throws IOException {
    log.info("Loading theme file " + resource.getURL());

    Document doc = readXmlDocument(resource);
    final Element rootElement = doc.getRootElement();

    for (Element element : (List<Element>) rootElement.elements()) {
        String elementName = element.getName();
        if ("lookAndFeel".equals(elementName)) {
            String lookAndFeel = element.getTextTrim();
            if (StringUtils.isNotEmpty(lookAndFeel)) {
                theme.setLookAndFeel(lookAndFeel);
            }/*  w w w.  ja va2 s.  com*/
        } else if ("ui-defaults".equals(elementName)) {
            loadUIDefaults(theme.getUiDefaults(), element);
        } else if ("layout".equals(elementName)) {
            loadLayoutSettings(theme, element);
        } else if ("style".equals(elementName)) {
            DesktopStyle style = loadStyle(element);
            theme.addStyle(style);
        } else if ("include".equals(elementName)) {
            includeThemeFile(theme, element, resource);
        } else if ("class".equals(elementName)) {
            // ignore it
        } else {
            log.error("Unknown tag: " + elementName);
        }
    }
}

From source file:com.haulmont.cuba.gui.components.filter.UserSetHelper.java

License:Apache License

public static String removeEntities(String filterXml, Collection ids) {
    Document document;
    try {/*  www  .  ja  v a  2s .c  o m*/
        document = DocumentHelper.parseText(filterXml);
    } catch (DocumentException e) {
        throw new RuntimeException(e);
    }
    Element param = document.getRootElement().element("and").element("c").element("param");
    String currentIds = param.getTextTrim();
    Set<String> set = parseSet(currentIds);
    String listOfIds = removeIds(set, ids);
    param.setText(listOfIds);
    return document.asXML();
}

From source file:com.haulmont.cuba.gui.components.filter.UserSetHelper.java

License:Apache License

public static String addEntities(String filterXml, Collection ids) {
    Document document;
    try {/*w w w  .ja  v a  2 s  . c o  m*/
        document = DocumentHelper.parseText(filterXml);
    } catch (DocumentException e) {
        throw new RuntimeException(e);
    }
    Element param = document.getRootElement().element("and").element("c").element("param");
    String currentIds = param.getTextTrim();
    Set<String> set = parseSet(currentIds);
    String listOfIds = createIdsString(set, ids);
    param.setText(listOfIds);
    return document.asXML();
}

From source file:com.haulmont.cuba.gui.presentations.PresentationsImpl.java

License:Apache License

@Override
public Element getSettings(Presentation p) {
    p = getPresentation(p.getId());//from www .  ja  v a 2s .co  m
    if (p != null) {
        Document doc;
        if (!StringUtils.isEmpty(p.getXml())) {
            doc = Dom4j.readDocument(p.getXml());
        } else {
            doc = DocumentHelper.createDocument();
            doc.setRootElement(doc.addElement("presentation"));
        }
        return doc.getRootElement();
    } else {
        return null;
    }
}

From source file:com.haulmont.cuba.gui.xml.layout.ScreenXmlLoader.java

License:Apache License

/**
 * Loads a descriptor./* w  w  w .  j av  a  2  s . c  om*/
 * @param resourcePath  path to the resource containing the XML
 * @param id            screen ID
 * @param params        screen parameters
 * @return root XML element
 */
public Element load(String resourcePath, String id, Map<String, Object> params) {
    StopWatch xmlLoadWatch = new Slf4JStopWatch(id + "#" + UIPerformanceLogger.LifeCycle.XML,
            LoggerFactory.getLogger(UIPerformanceLogger.class));

    String template = loadTemplate(resourcePath);
    Document document = getDocument(template, params);

    xmlLoadWatch.stop();
    return document.getRootElement();
}

From source file:com.haulmont.cuba.gui.xml.layout.ScreenXmlParser.java

License:Apache License

protected void replaceAssignParameters(Document document) {
    Map<String, String> assignedParams = new HashMap<>();

    List<Element> assignElements = Dom4j.elements(document.getRootElement(), "assign");
    ThemeConstantsManager themeManager = AppBeans.get(ThemeConstantsManager.NAME);
    for (Element assignElement : assignElements) {
        String name = assignElement.attributeValue("name");
        if (StringUtils.isEmpty(name)) {
            throw new RuntimeException("'name' attribute of assign tag is empty");
        }/*from w  w w .  j  av  a2 s  .c  om*/

        String value = assignElement.attributeValue("value");
        if (StringUtils.isEmpty(value)) {
            throw new RuntimeException("'value' attribute of assign tag is empty");
        }

        if (StringUtils.startsWith(value, ThemeConstants.PREFIX)) {
            ThemeConstants theme = themeManager.getConstants();
            value = theme.get(value.substring(ThemeConstants.PREFIX.length()));
        }

        assignedParams.put(name, value);
    }

    if (!assignedParams.isEmpty()) {
        Element layoutElement = document.getRootElement().element("layout");
        if (layoutElement != null) {
            Dom4j.walkAttributesRecursive(layoutElement, (element, attribute) -> {
                String attributeValue = attribute.getValue();
                if (StringUtils.isNotEmpty(attributeValue) && attributeValue.startsWith("${")
                        && attributeValue.endsWith("}")) {
                    String paramKey = attributeValue.substring(2, attributeValue.length() - 1);

                    String assignedValue = assignedParams.get(paramKey);
                    if (assignedValue == null) {
                        throw new RuntimeException("Unable to find value of assign param: " + paramKey);
                    }

                    attribute.setValue(assignedValue);
                }
            });
        }
    }
}

From source file:com.haulmont.cuba.gui.xml.XmlInheritanceProcessor.java

License:Apache License

public XmlInheritanceProcessor(Document document, Map<String, Object> params) {
    this.document = document;
    this.params = params;

    extNs = document.getRootElement().getNamespaceForPrefix("ext");

    targetLocators.add(new ViewPropertyElementTargetLocator());
    targetLocators.add(new ViewElementTargetLocator());
    targetLocators.add(new ButtonElementTargetLocator());
    targetLocators.add(new CommonElementTargetLocator());
}

From source file:com.haulmont.cuba.restapi.XMLConverter2.java

License:Apache License

@Override
@Nonnull/*  ww  w  . jav  a  2  s  .c o  m*/
public String processServiceMethodResult(Object result, Class resultType) throws Exception {
    Document document = DocumentHelper.createDocument();
    Element resultEl = document.addElement("result");
    if (result instanceof Entity) {
        Entity entity = (Entity) result;
        Document convertedEntity = _process(entity, null);
        resultEl.add(convertedEntity.getRootElement());
    } else if (result instanceof Collection) {
        if (!checkCollectionItemTypes((Collection) result, Entity.class))
            throw new IllegalArgumentException(
                    "Items that are not instances of Entity class found in service method result");
        ArrayList list = new ArrayList((Collection) result);
        MetaClass metaClass = null;
        if (!list.isEmpty())
            metaClass = ((Entity) list.get(0)).getMetaClass();

        Document processed = _process(list, metaClass, null);
        resultEl.add(processed.getRootElement());
    } else {
        if (result != null && resultType != Void.TYPE) {
            Datatype datatype = getDatatype(resultType);
            resultEl.setText(datatype != null ? datatype.format(result) : result.toString());
        } else {
            encodeNull(resultEl);
        }
    }
    return documentToString(document);
}

From source file:com.haulmont.cuba.restapi.XMLConverter2.java

License:Apache License

@Override
public CommitRequest parseCommitRequest(String content) {
    try {//from w w  w  . j  ava2  s.  com
        CommitRequest commitRequest = new CommitRequest();

        Document document = Dom4j.readDocument(content);
        Element rootElement = document.getRootElement();

        //commit instances
        Element commitInstancesEl = rootElement.element("commitInstances");
        if (commitInstancesEl != null) {
            //first find and register ids of all entities to be commited
            Set<String> commitIds = new HashSet<>();
            for (Object instance : commitInstancesEl.elements("instance")) {
                Element instanceEl = (Element) instance;
                String id = instanceEl.attributeValue("id");
                if (id.startsWith("NEW-"))
                    id = id.substring(id.indexOf('-') + 1);
                commitIds.add(id);
            }
            commitRequest.setCommitIds(commitIds);

            List commitInstanceElements = commitInstancesEl.elements("instance");
            List<Entity> commitInstances = new ArrayList<>();
            for (Object el : commitInstanceElements) {
                Element commitInstanceEl = (Element) el;
                String id = commitInstanceEl.attributeValue("id");
                InstanceRef ref = commitRequest.parseInstanceRefAndRegister(id);
                Entity instance = ref.getInstance();
                parseEntity(commitInstanceEl, instance, commitRequest);
                commitInstances.add(instance);
            }
            commitRequest.setCommitInstances(commitInstances);
        }

        //remove instances
        Element removeInstancesEl = rootElement.element("removeInstances");
        if (removeInstancesEl != null) {
            List removeInstanceElements = removeInstancesEl.elements("instance");
            List<Entity> removeInstances = new ArrayList<>();
            for (Object el : removeInstanceElements) {
                Element removeInstance = (Element) el;
                String id = removeInstance.attributeValue("id");
                InstanceRef ref = commitRequest.parseInstanceRefAndRegister(id);
                Entity instance = ref.getInstance();
                removeInstances.add(instance);
            }
            commitRequest.setRemoveInstances(removeInstances);
        }

        //soft deletion
        Element softDeletionEl = rootElement.element("softDeletion");
        if (softDeletionEl != null) {
            commitRequest.setSoftDeletion(Boolean.parseBoolean(softDeletionEl.getText()));
        }

        return commitRequest;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}