Example usage for org.dom4j Element elements

List of usage examples for org.dom4j Element elements

Introduction

In this page you can find the example usage for org.dom4j Element elements.

Prototype

List<Element> elements(QName qName);

Source Link

Document

Returns the elements contained in this element with the given fully qualified name.

Usage

From source file:com.gote.pojo.Round.java

License:Apache License

/**
 * Load Round from XML/*from  w  w  w. j av  a 2s.c om*/
 * 
 * @param pRoot Element
 * @param pTournament Tournament
 */
public void fromXML(Element pRoot, Tournament pTournament) {
    if (pRoot.attribute(TournamentGOTEUtil.ATT_ROUND_NUMBER) != null) {
        setNumber(Integer.parseInt(pRoot.attribute(TournamentGOTEUtil.ATT_ROUND_NUMBER).getValue()));
    } else {
        LOGGER.log(Level.WARNING, "The attribute " + TournamentGOTEUtil.ATT_ROUND_NUMBER + " is null");
    }

    if (pRoot.attribute(TournamentGOTEUtil.ATT_ROUND_DATESTART) != null) {
        setDateStart(new DateTime(pRoot.attribute(TournamentGOTEUtil.ATT_ROUND_DATESTART).getValue()));
    } else {
        LOGGER.log(Level.WARNING, "The attribute " + TournamentGOTEUtil.ATT_ROUND_DATESTART + " is null");
    }

    if (pRoot.attribute(TournamentGOTEUtil.ATT_ROUND_DATEEND) != null) {
        setDateEnd(new DateTime(pRoot.attribute(TournamentGOTEUtil.ATT_ROUND_DATEEND).getValue()));
    } else {
        LOGGER.log(Level.WARNING, "The attribute " + TournamentGOTEUtil.ATT_ROUND_DATEEND + " is null");
    }

    Element elementGames = pRoot.element(TournamentGOTEUtil.TAG_GAMES);
    if (elementGames != null) {
        List<Game> games = new ArrayList<Game>();
        @SuppressWarnings("unchecked")
        List<Element> gamesElements = elementGames.elements(TournamentGOTEUtil.TAG_GAME);
        if (gamesElements != null && !gamesElements.isEmpty()) {
            for (Element gameElement : gamesElements) {
                Game game = new Game();
                game.fromXML(gameElement, pTournament);
                games.add(game);
            }
        } else {
            LOGGER.log(Level.WARNING, "No round has been found");
        }
        setGameList(games);
    } else {
        LOGGER.log(Level.SEVERE, "The element " + TournamentGOTEUtil.TAG_GAMES + " is null");
    }
}

From source file:com.gote.pojo.Tournament.java

License:Apache License

/**
 * Load Tournament from XML/*from  w  w w.j  a  v a 2 s .  co m*/
 * 
 * @param pRoot Tournament element
 */
public void fromXML(Element pRoot) {
    if (pRoot.attribute(TournamentGOTEUtil.ATT_TOURNAMENT_NAME) != null) {
        setTitle(pRoot.attribute(TournamentGOTEUtil.ATT_TOURNAMENT_NAME).getValue());
    } else {
        LOGGER.log(Level.SEVERE, "The attribute " + TournamentGOTEUtil.ATT_TOURNAMENT_NAME + " is null");
    }

    if (pRoot.attribute(TournamentGOTEUtil.ATT_TOURNAMENT_SERVER) != null) {
        setServerType(pRoot.attribute(TournamentGOTEUtil.ATT_TOURNAMENT_SERVER).getValue());
    } else {
        LOGGER.log(Level.SEVERE, "The attribute " + TournamentGOTEUtil.ATT_TOURNAMENT_NAME + " is null");
    }

    if (pRoot.attribute(TournamentGOTEUtil.ATT_TOURNAMENT_DATESTART) != null) {
        setStartDate(new DateTime(pRoot.attribute(TournamentGOTEUtil.ATT_TOURNAMENT_DATESTART).getValue()));
    } else {
        LOGGER.log(Level.SEVERE, "The attribute " + TournamentGOTEUtil.ATT_TOURNAMENT_DATESTART + " is null");
    }

    if (pRoot.attribute(TournamentGOTEUtil.ATT_TOURNAMENT_DATEEND) != null) {
        setEndDate(new DateTime(pRoot.attribute(TournamentGOTEUtil.ATT_TOURNAMENT_DATEEND).getValue()));
    } else {
        LOGGER.log(Level.SEVERE, "The attribute " + TournamentGOTEUtil.ATT_TOURNAMENT_DATEEND + " is null");
    }

    Element elementTournamentRules = pRoot.element(TournamentGOTEUtil.TAG_RULES);
    if (elementTournamentRules != null) {
        TournamentRules tournamentRules = new TournamentRules();
        tournamentRules.fromXML(elementTournamentRules);
        setTournamentRules(tournamentRules);
    } else {
        LOGGER.log(Level.SEVERE, "The element " + TournamentGOTEUtil.TAG_RULES + " is null");
    }

    Element elementPlayers = pRoot.element(TournamentGOTEUtil.TAG_PLAYERS);
    if (elementPlayers != null) {
        List<Player> players = new ArrayList<Player>();
        @SuppressWarnings("unchecked")
        List<Element> playersElements = elementPlayers.elements(TournamentGOTEUtil.TAG_PLAYER);
        if (playersElements != null && !playersElements.isEmpty()) {
            for (Element playerElement : playersElements) {
                Player player = new Player();
                player.fromXML(playerElement);
                players.add(player);
            }
        } else {
            LOGGER.log(Level.WARNING, "No player has been found");
        }
        setParticipantsList(players);
    } else {
        LOGGER.log(Level.SEVERE, "The element " + TournamentGOTEUtil.TAG_ROUNDS + " is null");
    }

    Element elementRounds = pRoot.element(TournamentGOTEUtil.TAG_ROUNDS);
    if (elementRounds != null) {
        List<Round> rounds = new ArrayList<Round>();
        @SuppressWarnings("unchecked")
        List<Element> roundsElements = elementRounds.elements(TournamentGOTEUtil.TAG_ROUND);
        if (roundsElements != null && !roundsElements.isEmpty()) {
            for (Element roundElement : roundsElements) {
                Round round = new Round();
                round.fromXML(roundElement, this);
                rounds.add(round);
            }
        } else {
            LOGGER.log(Level.WARNING, "No round has been found");
        }
        setRounds(rounds);
    } else {
        LOGGER.log(Level.SEVERE, "The element " + TournamentGOTEUtil.TAG_ROUNDS + " is null");
    }
}

From source file:com.haulmont.bali.util.Dom4j.java

License:Apache License

@SuppressWarnings("unchecked")
public static List<Element> elements(Element element, String name) {
    return element.elements(name);
}

From source file:com.haulmont.chile.core.datatypes.Datatypes.java

License:Apache License

private Datatypes() {
    SAXReader reader = new SAXReader();
    URL resource = Datatypes.class.getResource("/datatypes.xml");
    if (resource == null) {
        log.info("Can't find /datatypes.xml, using default datatypes settings");
        resource = Datatypes.class.getResource("/com/haulmont/chile/core/datatypes/datatypes.xml");
    }/*w  w w . j  a  v  a 2  s.  c o m*/

    try {
        Document document = reader.read(resource);
        Element element = document.getRootElement();

        List<Element> datatypeElements = element.elements("datatype");
        for (Element datatypeElement : datatypeElements) {
            String datatypeClassName = datatypeElement.attributeValue("class");
            try {
                Datatype datatype;
                Class<Datatype> datatypeClass = ReflectionHelper.getClass(datatypeClassName);
                try {
                    final Constructor<Datatype> constructor = datatypeClass.getConstructor(Element.class);
                    datatype = constructor.newInstance(datatypeElement);
                } catch (Throwable e) {
                    datatype = datatypeClass.newInstance();
                }

                __register(datatype);
            } catch (Throwable e) {
                log.error(String.format("Fail to load datatype '%s'", datatypeClassName), e);
            }
        }
    } catch (DocumentException e) {
        log.error("Fail to load datatype settings", e);
    }
}

From source file:com.haulmont.cuba.core.sys.AbstractViewRepository.java

License:Apache License

protected void loadView(Element rootElem, Element viewElem, View view, boolean systemProperties,
        Set<ViewInfo> visited) {
    final MetaClass metaClass = metadata.getClassNN(view.getEntityClass());
    final String viewName = view.getName();

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

    for (Element propElem : (List<Element>) viewElem.elements("property")) {
        String propertyName = propElem.attributeValue("name");

        if (propertyNames.contains(propertyName)) {
            throw new DevelopmentException(
                    String.format("View %s/%s definition error: view declared property %s twice",
                            metaClass.getName(), viewName, propertyName));
        }/*from   w ww .j  av a2 s .  c o  m*/
        propertyNames.add(propertyName);

        MetaProperty metaProperty = metaClass.getProperty(propertyName);
        if (metaProperty == null) {
            throw new DevelopmentException(
                    String.format("View %s/%s definition error: property %s doesn't exists",
                            metaClass.getName(), viewName, propertyName));
        }

        View refView = null;
        String refViewName = propElem.attributeValue("view");

        MetaClass refMetaClass;
        Range range = metaProperty.getRange();
        if (range == null) {
            throw new RuntimeException("cannot find range for meta property: " + metaProperty);
        }

        final List<Element> propertyElements = Dom4j.elements(propElem, "property");
        boolean inlineView = !propertyElements.isEmpty();

        if (!range.isClass() && (refViewName != null || inlineView)) {
            throw new DevelopmentException(
                    String.format("View %s/%s definition error: property %s is not an entity",
                            metaClass.getName(), viewName, propertyName));
        }

        if (refViewName != null) {
            refMetaClass = getMetaClass(propElem, range);

            refView = retrieveView(refMetaClass, refViewName, visited);
            if (refView == null) {
                for (Element e : Dom4j.elements(rootElem, "view")) {
                    if (refMetaClass.equals(getMetaClass(e.attributeValue("entity"), e.attributeValue("class")))
                            && refViewName.equals(e.attributeValue("name"))) {
                        refView = deployView(rootElem, e, visited);
                        break;
                    }
                }

                if (refView == null) {
                    MetaClass originalMetaClass = metadata.getExtendedEntities()
                            .getOriginalMetaClass(refMetaClass);
                    if (originalMetaClass != null) {
                        refView = retrieveView(originalMetaClass, refViewName, visited);
                    }
                }

                if (refView == null) {
                    throw new DevelopmentException(String.format(
                            "View %s/%s definition error: unable to find/deploy referenced view %s/%s",
                            metaClass.getName(), viewName, range.asClass().getName(), refViewName));
                }
            }
        }

        if (inlineView) {
            // try to import anonymous views
            Class rangeClass = range.asClass().getJavaClass();

            if (refView != null) {
                refView = new View(refView, rangeClass, "", false); // system properties are already in the source view
            } else {
                ViewProperty existingProperty = view.getProperty(propertyName);
                if (existingProperty != null && existingProperty.getView() != null) {
                    refView = new View(existingProperty.getView(), rangeClass, "", systemProperties);
                } else {
                    refView = new View(rangeClass, systemProperties);
                }
            }
            loadView(rootElem, propElem, refView, systemProperties, visited);
        }

        FetchMode fetchMode = FetchMode.AUTO;
        String fetch = propElem.attributeValue("fetch");
        if (fetch != null)
            fetchMode = FetchMode.valueOf(fetch);

        view.addProperty(propertyName, refView, fetchMode);
    }
}

From source file:com.haulmont.cuba.desktop.sys.DesktopExternalUIComponentsSource.java

License:Apache License

@SuppressWarnings("unchecked")
protected void _registerComponent(InputStream is) throws ClassNotFoundException {
    ClassLoader classLoader = App.class.getClassLoader();

    Element rootElement = Dom4j.readDocument(is).getRootElement();
    List<Element> components = rootElement.elements("component");
    for (Element component : components) {
        String name = trimToEmpty(component.elementText("name"));
        String componentClassName = trimToEmpty(component.elementText("class"));

        String componentLoaderClassName = trimToEmpty(component.elementText("componentLoader"));
        String tag = trimToEmpty(component.elementText("tag"));
        if (StringUtils.isEmpty(tag)) {
            tag = name;/*from w  ww  .ja v a 2 s  . c o  m*/
        }

        if (StringUtils.isEmpty(name) && StringUtils.isEmpty(tag)) {
            log.warn("You have to provide name or tag for custom component");
            // skip this <component> element
            continue;
        }

        if (StringUtils.isEmpty(componentLoaderClassName) && StringUtils.isEmpty(componentClassName)) {
            log.warn("You have to provide at least <class> or <componentLoader> for custom component {} / <{}>",
                    name, tag);
            // skip this <component> element
            continue;
        }

        if (StringUtils.isNotEmpty(name) && StringUtils.isNotEmpty(componentClassName)) {
            Class<?> componentClass = classLoader.loadClass(componentClassName);

            if (Component.class.isAssignableFrom(componentClass)) {
                log.trace("Register component {} class {}", name, componentClass.getCanonicalName());

                DesktopComponentsFactory.registerComponent(name, (Class<? extends Component>) componentClass);
            } else {
                log.warn("Component {} is not a subclass of com.haulmont.cuba.gui.components.Component",
                        componentClassName);
            }
        }

        if (StringUtils.isNotEmpty(tag) && StringUtils.isNotEmpty(componentLoaderClassName)) {
            Class<?> componentLoaderClass = classLoader.loadClass(componentLoaderClassName);

            if (ComponentLoader.class.isAssignableFrom(componentLoaderClass)) {
                log.trace("Register tag {} loader {}", tag, componentLoaderClass.getCanonicalName());

                LayoutLoaderConfig.registerLoader(tag, (Class<? extends ComponentLoader>) componentLoaderClass);
            } else {
                log.warn(
                        "Component loader {} is not a subclass of com.haulmont.cuba.gui.xml.layout.ComponentLoader",
                        componentLoaderClassName);
            }
        }
    }

    _loadWindowLoaders(rootElement);
}

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 . jav  a  2s. co 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.gui.config.WindowConfig.java

License:Apache License

@SuppressWarnings("unchecked")
protected void loadConfig(Element rootElem) {
    for (Element element : (List<Element>) rootElem.elements("include")) {
        String fileName = element.attributeValue("file");
        if (!StringUtils.isBlank(fileName)) {
            String incXml = resources.getResourceAsString(fileName);
            if (incXml == null) {
                log.warn("File {} not found, ignore it", fileName);
                continue;
            }//ww  w  . j  a  v  a2s . c  o  m
            loadConfig(Dom4j.readDocument(incXml).getRootElement());
        }
    }
    for (Element element : (List<Element>) rootElem.elements("screen")) {
        String id = element.attributeValue("id");
        if (StringUtils.isBlank(id)) {
            log.warn("Invalid window config: 'id' attribute not defined");
            continue;
        }

        ScreenAgent targetAgent = null;
        String agent = element.attributeValue("agent");
        if (StringUtils.isNotEmpty(agent)) {
            targetAgent = activeScreenAgents.get(agent);

            if (targetAgent == null) {
                throw new DevelopmentException("Unable to find target screen agent", "agent", agent);
            }
        }

        WindowInfo windowInfo = new WindowInfo(id, element, targetAgent);

        List<WindowInfo> screenInfos = screens.get(id);
        if (screenInfos == null) {
            screenInfos = new ArrayList<>();
            screens.put(id, screenInfos);
        } else {
            WindowInfo existingScreen = screenInfos.stream().filter(
                    existingWindowInfo -> existingWindowInfo.getScreenAgent() == windowInfo.getScreenAgent())
                    .findFirst().orElse(null);

            if (existingScreen != null) {
                screenInfos.remove(existingScreen);
            }
        }

        screenInfos.add(windowInfo);
    }
}

From source file:com.haulmont.cuba.gui.model.impl.ScreenDataImpl.java

License:Apache License

protected void loadInstanceContainer(Element element) {
    String containerId = getRequiredAttr(element, "id");

    InstanceContainer<Entity> container = factory.createInstanceContainer(getEntityClass(element));
    loadView(element, getEntityClass(element), container);

    Element loaderEl = element.element("loader");
    if (loaderEl != null) {
        loadInstanceLoader(loaderEl, container);
    }/*  w  w w.  j a  va 2s.  c o  m*/

    registerContainer(containerId, container);

    for (Element collectionEl : element.elements("collection")) {
        loadNestedCollectionContainer(collectionEl, container);
    }
}

From source file:com.haulmont.cuba.gui.model.impl.ScreenDataImpl.java

License:Apache License

protected void loadCollectionContainer(Element element) {
    String containerId = getRequiredAttr(element, "id");

    CollectionContainer<Entity> container = factory.createCollectionContainer(getEntityClass(element));
    loadView(element, getEntityClass(element), container);

    Element loaderEl = element.element("loader");
    if (loaderEl != null) {
        loadCollectionLoader(loaderEl, container);
    }//from  w ww .  ja v a  2s.c  om

    registerContainer(containerId, container);

    for (Element collectionEl : element.elements("collection")) {
        loadNestedCollectionContainer(collectionEl, container);
    }
}