Package org.dom4j

Examples of org.dom4j.Element


        }
    }

    public Document createDocument() {
        Document document = DocumentHelper.createDocument();
        Element timefinderEl = document.addElement("timefinder"/*, "xmlnsValue"*/);
        timefinderEl.addAttribute("version", "4.1");

        writeElement(settings, timefinderEl);

        for (Dao<? extends DBInterface> dao : pool.getDaos()) {
            Element el = timefinderEl.addElement("objects");
            el.addAttribute("class", dao.getType().getName());

            for (DBInterface object : dao.getAll()) {
                if (object == null)
                    throw new NullPointerException("You should persist null objects!");

                if (Person.class.isAssignableFrom(dao.getType())) {
                    Element tmp = writeGeneric((Person) object, el, dao.getType());
                    writeElement((Person) object, tmp);
                } else if (Event.class.isAssignableFrom(dao.getType())) {
                    Element tmp = writeGeneric((Event) object, el, dao.getType());
                    writeElement((Event) object, tmp);
                } else if (Feature.class.isAssignableFrom(dao.getType())) {
                    writeGeneric((Feature) object, el, dao.getType());
                } else if (Location.class.isAssignableFrom(dao.getType())) {
                    Element tmp = writeGeneric((Location) object, el, dao.getType());
                    writeElement((Location) object, tmp);
                } else
                    throw new UnsupportedOperationException("Only person, feature, "
                            + "event and location are supported, but was:" + object.getClass());
            }
View Full Code Here


        return document;
    }

    private void writeElement(DataPoolSettings settings, Element timefinderEl) {
        Element settingsEl = timefinderEl.addElement("settings");
        writeElements(settingsEl, settings, SETTINGS_PROPERTIES);
    }
View Full Code Here

    private void writeElement(Person person, Element element) {
        writeElements(element, person, PERSON_PROPERTIES);
        if (person.getEvents().size() == 0)
            return;

        Element newElement = element.addElement("eventsMap");
        for (Entry<Event, Role> entry : person.getEventsMap().entrySet()) {
            newElement.addElement("event").
                    addAttribute("ref", "" + entry.getKey().getId()).
                    addAttribute("role", entry.getValue().toString());
        }
    }
View Full Code Here

        writeFeatures(element, location);

        if (location.getEvents().size() == 0)
            return;

        Element newElement = element.addElement("events");
        for (Event ev : location.getEvents()) {
            newElement.addElement("event").addAttribute("ref", "" + ev.getId());
        }
    }
View Full Code Here

    public Element writeConstraints(Element element, ConstraintObject co) {
        if (co.getConstraints().size() == 0)
            return element;

        Element newElement = element.addElement("constraints");
        for (Constraint constr : co.getConstraints()) {
            Element el = newElement.addElement(constr.getClass().getSimpleName());
            el.addAttribute("weight", "" + constr.getWeight());
            if (constr instanceof RasterConstraint) {
                RasterConstraint rc = (RasterConstraint) constr;
                Writing w = getWriting(BitSet.class);
                el.setText(w.toString(rc.getRaster().getForbidden()));
                el.addAttribute("length", "" + rc.getRaster().getForbidden().getLength());
            } else if (constr instanceof EventOrderConstraint) {
                EventOrderConstraint eoc = (EventOrderConstraint) constr;
                el.addElement("event").addAttribute("ref", "" + eoc.getEvent().getId());
                for (Event ev : eoc.getBefores()) {
                    el.addElement("beforeEvent").addAttribute("ref", "" + ev.getId());
                }
                for (Event ev : eoc.getFollows()) {
                    el.addElement("followsEvent").addAttribute("ref", "" + ev.getId());
                }
            } else if (constr instanceof PersonITCRasterConstraint) {
                PersonITCRasterConstraint pc = (PersonITCRasterConstraint) constr;
                el.addElement("person").addAttribute("ref", "" + pc.getPerson().getId());
            } else if (constr instanceof MinGapsConstraint) {
                MinGapsConstraint mgc = (MinGapsConstraint) constr;
                el.addAttribute("countEarly", "" + mgc.isCountEarly());
                for (Event ev : mgc.getEvents()) {
                    el.addElement("event").addAttribute("ref", "" + ev.getId());
                }
            } else if (constr instanceof DifferentDayConstraint) {
                DifferentDayConstraint ddc = (DifferentDayConstraint) constr;
                for (Event ev : ddc.getEvents()) {
                    el.addElement("event").addAttribute("ref", "" + ev.getId());
                }
            } else
                throw new UnsupportedOperationException("Constraint export/import not yet implemented for class " + constr.getClass().getName());

        }
View Full Code Here

    public Element writeFeatures(Element element, FeatureContainer fc) {
        if (fc.getFeatures().size() == 0)
            return element;

        Element newElement = element.addElement("features");
        for (Feature feature : fc.getFeatures()) {
            newElement.addElement("feature").addAttribute("ref", "" + feature.getId());
        }
        return element;
    }
View Full Code Here

            Document document = saxReader.read(reader);

            List list = document.getRootElement().elements();
            Iterator iter = list.iterator();
            while (iter.hasNext()) {
                Element element = (Element) iter.next();
                if ("settings".equals(element.getName())) {
                    readElements(element, settings);
                } else if ("objects".equals(element.getName())) {
                    Class clazz = Class.forName(element.attributeValue("class"));
                    readGeneric(clazz, element);
                }
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
View Full Code Here

        }
    }

    private void readGeneric(Class<DBInterface> clazz, Element rootElement) {
        for (Object objEl : rootElement.elements()) {
            Element objElement = (Element) objEl;
            if (!"object".equals(objElement.getName()))
                throw new UnsupportedOperationException("sub element should be object");

            long id = getId(objElement);
            Object obj = getObject(id, clazz);
            readElements(objElement, obj);
View Full Code Here

    }

    private Element readElements(Element rootElement, Object object) {
        Map<String, Method> methods = Helper.findSetterMethods(object.getClass());
        for (Object objectEl : rootElement.elements()) {
            Element element = (Element) objectEl;
            Method m = methods.get(element.getName());
            if (m == null)
                throw new NullPointerException("Cannot find setter for element:" + element.getName());
            try {
                Class setterType = m.getParameterTypes()[0];
                Object result = getResult(object, setterType, element);
                m.invoke(object, result);
            } catch (Exception ex) {
                throw new RuntimeException("Exception while parsing ("
                        + element.getName() + ") to "
                        + object.getClass().getName() + " " + object, ex);
            }
        }

        return rootElement;
View Full Code Here

                if ("constraints".equals(element.getName())) {
                    return (T) readConstraints(element);
                } else if ("eventsMap".equals(element.getName())) {
                    Map map = FastMap.newInstance();
                    for (Object evEl : element.elements()) {
                        Element evElement = (Element) evEl;
                        long ref = getRef(evElement);
                        Role role = Role.valueOf(evElement.attributeValue("role"));
                        map.put(getObject(ref, Event.class), role);
                    }
                    return (T) map;
                }
            } else if (Event.class.isAssignableFrom(obj.getClass())) {
View Full Code Here

TOP

Related Classes of org.dom4j.Element

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.