Example usage for org.jdom2 Element getChildren

List of usage examples for org.jdom2 Element getChildren

Introduction

In this page you can find the example usage for org.jdom2 Element getChildren.

Prototype

public List<Element> getChildren() 

Source Link

Document

This returns a List of all the child elements nested directly (one level deep) within this element, as Element objects.

Usage

From source file:com.rhythm.louie.server.LouieProperties.java

License:Apache License

private static void processServices(Element services, boolean internal) {
    if (services == null)
        return;//from www . j  av a  2  s .  c  o m

    for (Element elem : services.getChildren()) {
        if (DEFAULT.equalsIgnoreCase(elem.getName())) {
            processServiceDefaults(elem);
            break;
        }
    }

    List<ServiceProperties> servicesList = new ArrayList<>();
    for (Element service : services.getChildren()) {
        String elementName = service.getName();
        if (!SERVICE.equalsIgnoreCase(elementName)) {
            if (!DEFAULT.equalsIgnoreCase(elementName)) {
                LoggerFactory.getLogger(LouieProperties.class).warn("Unknown {} element: {}", SERVICE_PARENT,
                        elementName);
            }
            continue;
        }

        String serviceName = null;
        Boolean enable = null;
        for (Attribute attr : service.getAttributes()) {
            String propName = attr.getName().toLowerCase();
            String propValue = attr.getValue();
            if (null != propName)
                switch (propName) {
                case NAME:
                    serviceName = propValue;
                    break;
                case ENABLE:
                    enable = Boolean.valueOf(propValue);
                    break;
                default:
                    LoggerFactory.getLogger(LouieProperties.class).warn("Unexpected service attribute {}:{}",
                            propName, propValue);
                    break;
                }
        }

        if (serviceName == null) {
            LoggerFactory.getLogger(LouieProperties.class)
                    .error("A service was missing it's 'name' attribute and will be skipped");
            continue;
        }

        ServiceProperties prop = new ServiceProperties(serviceName);
        if (enable != null) {
            prop.setEnable(enable);
        }

        for (Element serviceProp : service.getChildren()) {
            String propName = serviceProp.getName().toLowerCase();
            String propValue = serviceProp.getTextTrim();
            if (null != propName)
                switch (propName) {
                case CACHING:
                    prop.setCaching(Boolean.valueOf(propValue));
                    break;
                case READ_ONLY:
                    prop.setReadOnly(Boolean.valueOf(propValue));
                    break;
                case PROVIDER_CL:
                    prop.setProviderClass(propValue);
                    break;
                case RESPECTED_GROUPS:
                    AccessManager.loadServiceAccess(serviceName, serviceProp);
                    break;
                case RESERVED:
                    if (internal)
                        prop.setReserved(Boolean.valueOf(propValue));
                    break;
                case LAYERS:
                    processServiceLayers(serviceProp, prop);
                    break;
                case CUSTOM:
                    for (Element child : serviceProp.getChildren()) {
                        prop.addCustomProp(child.getName(), child.getTextTrim());
                    }
                    break;
                default:
                    LoggerFactory.getLogger(LouieProperties.class).warn("Unexpected server property  {}:{}",
                            propName, propValue);
                }
        }
        servicesList.add(prop);
    }
    ServiceProperties.processServices(servicesList);
}

From source file:com.rhythm.louie.server.LouieProperties.java

License:Apache License

private static void processServiceLayers(Element layers, ServiceProperties props) {
    for (Element layer : layers.getChildren()) {
        String layerName = layer.getName().toLowerCase();
        switch (layerName) {
        case LAYER:
            props.addLayer(new CustomServiceLayer(layer.getAttributeValue("class")));
            break;
        case LAYER_DAO:
            props.addLayer(AnnotatedServiceLayer.DAO);
            break;
        case LAYER_CACHE:
            props.addLayer(AnnotatedServiceLayer.CACHE);
            break;
        case LAYER_ROUTER:
            props.addLayer(AnnotatedServiceLayer.ROUTER);
            break;
        case LAYER_REMOTE:
            String server = layer.getAttributeValue(SERVER);
            String host = layer.getAttributeValue(HOST);
            String gateway = layer.getAttributeValue(GATEWAY);
            String port = layer.getAttributeValue(PORT);
            if (server != null) {
                props.addLayer(new RemoteServiceLayer(server));
            } else if (host != null && gateway != null && port != null) {
                props.addLayer(new RemoteServiceLayer(host, gateway, Integer.parseInt(port)));
            } else {
                String defaultServer = ServiceProperties.getDefaultRemoteServer();
                if (defaultServer == null) {
                    LoggerFactory.getLogger(LouieProperties.class).error(
                            "Failed to configure remote layer for service {}. Check configs.", props.getName());
                }//from www  . j av  a2s.co m
                props.addLayer(new RemoteServiceLayer(defaultServer));
            }
            break;
        default:
            LoggerFactory.getLogger(LouieProperties.class).warn("Unkown layer:{}", layerName);
        }
    }
}

From source file:com.rhythm.louie.server.LouieProperties.java

License:Apache License

private static void processCustomProperties(Element customElem) {
    for (Element customProp : customElem.getChildren()) {
        String propName = customProp.getName();
        CustomProperty custom = new CustomProperty(propName);
        for (Element child : customProp.getChildren()) {
            custom.setProperty(child.getName(), child.getTextTrim());
        }/*from  w ww.ja v  a 2  s .co  m*/
        customProperties.put(propName, custom);
    }
}

From source file:com.rhythm.louie.server.TaskSchedulerProperties.java

License:Apache License

public static void processProperties(Element scheduler) {
    for (Element child : scheduler.getChildren()) {
        String elemName = child.getName().toLowerCase();
        String elemValue = child.getTextTrim();
        switch (elemName) {
        case POOL_SIZE:
            threadPoolSize = Integer.parseInt(elemValue);
            break;
        case JNDI:
            jndiKey = elemValue;//ww  w  .  j a v  a  2s .c  o m
            break;
        default:
            LoggerFactory.getLogger(LouieProperties.class).warn("Unexpected scheduler property  {}:{}",
                    elemName, elemValue);
            break;
        }
    }
}

From source file:com.rometools.modules.base.io.CustomTagParser.java

License:Open Source License

@Override
public Module parse(final Element element, final Locale locale) {
    final CustomTags module = new CustomTagsImpl();
    final ArrayList<CustomTag> tags = new ArrayList<CustomTag>();
    final List<Element> elements = element.getChildren();
    final Iterator<Element> it = elements.iterator();
    while (it.hasNext()) {
        final Element child = it.next();
        if (child.getNamespace().equals(NS)) {
            final String type = child.getAttributeValue("type");
            try {
                if (type == null) {
                    continue;
                } else if (type.equals("string")) {
                    tags.add(new CustomTagImpl(child.getName(), child.getText()));
                } else if (type.equals("int")) {
                    tags.add(new CustomTagImpl(child.getName(), new Integer(child.getTextTrim())));
                } else if (type.equals("float")) {
                    tags.add(new CustomTagImpl(child.getName(), new Float(child.getTextTrim())));
                } else if (type.equals("intUnit")) {
                    tags.add(new CustomTagImpl(child.getName(), new IntUnit(child.getTextTrim())));
                } else if (type.equals("floatUnit")) {
                    tags.add(new CustomTagImpl(child.getName(), new FloatUnit(child.getTextTrim())));
                } else if (type.equals("date")) {
                    try {
                        tags.add(new CustomTagImpl(child.getName(),
                                new ShortDate(GoogleBaseParser.SHORT_DT_FMT.parse(child.getTextTrim()))));
                    } catch (final ParseException e) {
                        LOG.warn("Unable to parse date type on " + child.getName(), e);
                    }/*from  w w w.  j  av  a  2s.c  om*/
                } else if (type.equals("dateTime")) {
                    try {
                        tags.add(new CustomTagImpl(child.getName(),
                                GoogleBaseParser.LONG_DT_FMT.parse(child.getTextTrim())));
                    } catch (final ParseException e) {
                        LOG.warn("Unable to parse date type on " + child.getName(), e);
                    }
                } else if (type.equals("dateTimeRange")) {
                    try {
                        tags.add(new CustomTagImpl(child.getName(),
                                new DateTimeRange(
                                        GoogleBaseParser.LONG_DT_FMT.parse(
                                                child.getChild("start", CustomTagParser.NS).getText().trim()),
                                        GoogleBaseParser.LONG_DT_FMT.parse(
                                                child.getChild("end", CustomTagParser.NS).getText().trim()))));
                    } catch (final Exception e) {
                        LOG.warn("Unable to parse date type on " + child.getName(), e);
                    }
                } else if (type.equals("url")) {
                    try {
                        tags.add(new CustomTagImpl(child.getName(), new URL(child.getTextTrim())));
                    } catch (final MalformedURLException e) {
                        LOG.warn("Unable to parse URL type on " + child.getName(), e);
                    }
                } else if (type.equals("boolean")) {
                    tags.add(
                            new CustomTagImpl(child.getName(), new Boolean(child.getTextTrim().toLowerCase())));
                } else if (type.equals("location")) {
                    tags.add(new CustomTagImpl(child.getName(), new CustomTagImpl.Location(child.getText())));
                } else {
                    throw new Exception("Unknown type: " + type);
                }
            } catch (final Exception e) {
                LOG.warn("Unable to parse type on " + child.getName(), e);
            }
        }
    }
    module.setValues(tags);
    return module;
}

From source file:com.rometools.modules.base.io.GoogleBaseParser.java

License:Open Source License

@Override
public Module parse(final Element element, final Locale locale) {
    final HashMap<String, PropertyDescriptor> tag2pd = new HashMap<String, PropertyDescriptor>();
    final GoogleBaseImpl module = new GoogleBaseImpl();

    try {//from   w ww  .j  a  va 2 s .  co m
        for (final PropertyDescriptor pd : pds) {
            final String tagName = GoogleBaseParser.PROPS2TAGS.getProperty(pd.getName());

            if (tagName == null) {
                LOG.debug("Property: {} doesn't have a tag mapping.", pd.getName());
            } else {
                tag2pd.put(tagName, pd);
            }
        }
    } catch (final Exception e) {
        throw new RuntimeException("Exception building tag to property mapping. ", e);
    }

    final List<Element> children = element.getChildren();
    final Iterator<Element> it = children.iterator();

    while (it.hasNext()) {
        final Element child = it.next();

        if (child.getNamespace().equals(GoogleBaseParser.NS)) {
            final PropertyDescriptor pd = tag2pd.get(child.getName());

            if (pd != null) {
                try {
                    handleTag(child, pd, module);
                } catch (final Exception e) {
                    LOG.warn("Unable to handle tag: " + child.getName(), e);
                    e.printStackTrace();
                }
            }
        }
    }

    return module;
}

From source file:com.rometools.modules.photocast.io.Parser.java

License:Open Source License

@Override
public Module parse(final Element element, final Locale locale) {
    if (element.getName().equals("channel") || element.getName().equals("feed")) {
        return new PhotocastModuleImpl();
    } else if (element.getChild("metadata", Parser.NS) == null
            && element.getChild("image", Parser.NS) == null) {
        return null;
    }//from   w w w  .j a  v  a2 s  .  c  o  m
    final PhotocastModule pm = new PhotocastModuleImpl();
    final List<Element> children = element.getChildren();
    final Iterator<Element> it = children.iterator();
    while (it.hasNext()) {
        final Element e = it.next();
        if (!e.getNamespace().equals(Parser.NS)) {
            continue;
        }
        if (e.getName().equals("photoDate")) {
            try {
                pm.setPhotoDate(Parser.PHOTO_DATE_FORMAT.parse(e.getText()));
            } catch (final Exception ex) {
                LOG.warn("Unable to parse photoDate: " + e.getText(), ex);
            }
        } else if (e.getName().equals("cropDate")) {
            try {
                pm.setCropDate(Parser.CROP_DATE_FORMAT.parse(e.getText()));
            } catch (final Exception ex) {
                LOG.warn("Unable to parse cropDate: " + e.getText(), ex);
            }
        } else if (e.getName().equals("thumbnail")) {
            try {
                pm.setThumbnailUrl(new URL(e.getText()));
            } catch (final Exception ex) {
                LOG.warn("Unable to parse thumnail: " + e.getText(), ex);
            }
        } else if (e.getName().equals("image")) {
            try {
                pm.setImageUrl(new URL(e.getText()));
            } catch (final Exception ex) {
                LOG.warn("Unable to parse image: " + e.getText(), ex);
            }
        } else if (e.getName().equals("metadata")) {
            String comments = "";
            PhotoDate photoDate = null;
            if (e.getChildText("PhotoDate") != null) {
                try {
                    photoDate = new PhotoDate(Double.parseDouble(e.getChildText("PhotoDate")));
                } catch (final Exception ex) {
                    LOG.warn("Unable to parse PhotoDate: " + e.getText(), ex);
                }
            }
            if (e.getChildText("Comments") != null) {
                comments = e.getChildText("Comments");
            }
            pm.setMetadata(new Metadata(photoDate, comments));
        }
    }
    return pm;
}

From source file:com.rometools.rome.io.impl.ModuleParsers.java

License:Open Source License

private boolean hasElementsFrom(final Element root, final Namespace namespace) {
    boolean hasElements = false;
    for (final Element child : root.getChildren()) {
        final Namespace childNamespace = child.getNamespace();
        if (namespace.equals(childNamespace)) {
            hasElements = true;//w w w  .  j av  a 2s.  c o  m
            break;
        }
    }
    return hasElements;
}

From source file:com.rometools.rome.io.impl.RSS091UserlandGenerator.java

License:Open Source License

@Override
protected void checkChannelConstraints(final Element eChannel) throws FeedException {

    checkNotNullAndLength(eChannel, "title", 1, 100);
    checkNotNullAndLength(eChannel, "description", 1, 500);
    checkNotNullAndLength(eChannel, "link", 1, 500);
    checkNotNullAndLength(eChannel, "language", 2, 5);
    checkLength(eChannel, "rating", 20, 500);
    checkLength(eChannel, "copyright", 1, 100);
    checkLength(eChannel, "pubDate", 1, 100);
    checkLength(eChannel, "lastBuildDate", 1, 100);
    checkLength(eChannel, "docs", 1, 500);
    checkLength(eChannel, "managingEditor", 1, 100);
    checkLength(eChannel, "webMaster", 1, 100);

    final Element skipHours = eChannel.getChild("skipHours");

    if (skipHours != null) {

        final List<Element> hours = skipHours.getChildren();
        for (final Element hour : hours) {

            final int value = Integer.parseInt(hour.getText().trim());

            if (isHourFormat24()) {
                if (value < 1 || value > 24) {
                    throw new FeedException("Invalid hour value " + value + ", it must be between 1 and 24");
                }/*from w  w w. j av  a2 s .c om*/
            } else {
                if (value < 0 || value > 23) {
                    throw new FeedException("Invalid hour value " + value + ", it must be between 0 and 23");
                }
            }

        }

    }

}

From source file:com.speedment.codgen.example.uml.TransformDelegator.java

License:Open Source License

default <M> Stream<M> children(Generator gen, Element model, java.lang.Class<M> type) {
    return gen.metaOn(model.getChildren(), type).map(Meta::getResult);
}