Example usage for org.dom4j Element elementIterator

List of usage examples for org.dom4j Element elementIterator

Introduction

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

Prototype

Iterator<Element> elementIterator(QName qName);

Source Link

Document

Returns an iterator over the elements contained in this element which match the given fully qualified name.

Usage

From source file:com.zimbra.perf.chart.XMLChartConfig.java

License:Open Source License

/**
 * Loads chart settings from the specified XML file.
 *
 * @throws IOException//from   w  w w .  j a va  2  s  .  c o m
 *             if there was an error reading the file
 * @throws DocumentException
 *             if there was an error parsing the file
 * @throws XmlParseException
 * @throws IllegalArgumentException
 *             if any attribute has invalid value
 */
public static List<ChartSettings> load(File xmlFile) throws IOException, DocumentException, XmlParseException {
    List<ChartSettings> charts = new ArrayList<ChartSettings>();
    Document document;
    try (FileInputStream fis = new FileInputStream(xmlFile)) {
        document = W3cDomUtil.parseXMLToDom4jDocUsingSecureProcessing(fis);
    }
    Element chartsElem = document.getRootElement();
    if (!chartsElem.getName().equals(E_CHARTS)) {
        throw new DocumentException("Missing <" + E_CHARTS + "> root element");
    }
    for (Iterator iter = chartsElem.elementIterator(E_CHART); iter.hasNext();) {
        Element chartElem = (Element) iter.next();
        String chartTitle = getAttr(chartElem, A_CHART_TITLE);
        String category = getAttr(chartElem, A_CHART_CATEGORY, "unknown");
        String outfile = getAttr(chartElem, A_CHART_OUTFILE);

        // inheritable attributes
        String xAxis = getInheritedAttr(chartElem, A_CHART_XAXIS, ChartSettings.DEFAULT_CHART_XAXIS);
        String yAxis = getInheritedAttr(chartElem, A_CHART_YAXIS, "");
        boolean allowLogScale = getInheritedAttrBoolean(chartElem, A_CHART_ALLOW_LOG_SCALE,
                ChartSettings.DEFAULT_CHART_ALLOW_LOG_SCALE);
        boolean plotZero = getInheritedAttrBoolean(chartElem, A_CHART_PLOT_ZERO,
                ChartSettings.DEFAULT_CHART_PLOT_ZERO);
        if (!allowLogScale)
            plotZero = true;
        int width = getInheritedAttrInt(chartElem, A_CHART_WIDTH, ChartSettings.DEFAULT_CHART_WIDTH);
        int height = getInheritedAttrInt(chartElem, A_CHART_HEIGHT, ChartSettings.DEFAULT_CHART_HEIGHT);
        String outDoc = getAttr(chartElem, A_CHART_DOCUMENT, null);
        String topPlotStr = getAttr(chartElem, A_CHART_TOP_PLOTS, null);
        int topPlots = -1;
        if (topPlotStr != null)
            topPlots = Integer.parseInt(topPlotStr);
        topPlotStr = getAttr(chartElem, A_CHART_TOP_PLOTS_TYPE, "max");

        ChartSettings.TopPlotsType topPlotsType = ChartSettings.TopPlotsType.valueOf(topPlotStr.toUpperCase());

        ChartSettings chart = new ChartSettings(chartTitle, category, outfile, xAxis, yAxis, allowLogScale,
                plotZero, width, height, outDoc, topPlots, topPlotsType);

        for (Iterator plotIter = chartElem.elementIterator(E_PLOT); plotIter.hasNext();) {
            Element plotElem = (Element) plotIter.next();
            String dataCol = getAttr(plotElem, A_PLOT_DATA_COLUMN, null);

            // inheritable attributes
            String legend = getInheritedAttr(plotElem, A_PLOT_LEGEND, null);
            String infile = getInheritedAttr(plotElem, A_PLOT_INFILE, null);

            boolean showRaw = getInheritedAttrBoolean(plotElem, A_PLOT_SHOW_RAW,
                    PlotSettings.DEFAULT_PLOT_SHOW_RAW);
            boolean showMovingAvg = getInheritedAttrBoolean(plotElem, A_PLOT_SHOW_MOVING_AVG,
                    PlotSettings.DEFAULT_PLOT_SHOW_MOVING_AVG);
            int movingAvgPoints = getInheritedAttrInt(plotElem, A_PLOT_MOVING_AVG_POINTS,
                    PlotSettings.DEFAULT_PLOT_MOVING_AVG_POINTS);
            double multiplier = getInheritedAttrDouble(plotElem, A_PLOT_MULTIPLIER,
                    PlotSettings.DEFAULT_PLOT_MULTIPLIER);
            double divisor = getInheritedAttrDouble(plotElem, A_PLOT_DIVISOR,
                    PlotSettings.DEFAULT_PLOT_DIVISOR);

            boolean nonNegative = getInheritedAttrBoolean(plotElem, A_PLOT_NON_NEGATIVE,
                    PlotSettings.DEFAULT_PLOT_NON_NEGATIVE);
            boolean percentTime = getInheritedAttrBoolean(plotElem, A_PLOT_PERCENT_TIME,
                    PlotSettings.DEFAULT_PLOT_PERCENT_TIME);
            String dataFunction = getAttr(plotElem, A_PLOT_DATA_FUNCTION,
                    PlotSettings.DEFAULT_PLOT_DATA_FUNCTION);
            String aggFunction = getAttr(plotElem, A_PLOT_AGGREGATE_FUNCTION,
                    PlotSettings.DEFAULT_PLOT_AGGREGATE_FUNCTION);
            String ratioTop = getAttr(plotElem, A_PLOT_RATIO_TOP, null);
            String ratioBottom = getAttr(plotElem, A_PLOT_RATIO_BOTTOM, null);

            if ((ratioTop == null && ratioBottom != null) || (ratioTop != null && ratioBottom == null)) {
                throw new DocumentException("Both ratioTop/ratioBottom need to be specified");
            }
            if ((ratioTop == null && dataCol == null) || (ratioTop != null && dataCol != null)) {
                throw new DocumentException("Specify either ratio or data");
            }
            boolean optional = getInheritedAttrBoolean(plotElem, A_PLOT_OPTIONAL,
                    PlotSettings.DEFAULT_PLOT_OPTIONAL);

            PlotSettings plot = new PlotSettings(legend, infile, dataCol, showRaw, showMovingAvg,
                    movingAvgPoints, multiplier, divisor, nonNegative, percentTime, dataFunction, aggFunction,
                    optional, ratioTop, ratioBottom);
            chart.addPlot(plot);
        }
        for (Iterator plotIter = chartElem.elementIterator(E_GROUP_PLOT); plotIter.hasNext();) {
            Element plotElem = (Element) plotIter.next();
            String dataCol = getAttr(plotElem, A_PLOT_DATA_COLUMN);

            // inheritable attributes
            String infile = getInheritedAttr(plotElem, A_PLOT_INFILE, null);

            boolean showRaw = getInheritedAttrBoolean(plotElem, A_PLOT_SHOW_RAW,
                    PlotSettings.DEFAULT_PLOT_SHOW_RAW);
            boolean showMovingAvg = getInheritedAttrBoolean(plotElem, A_PLOT_SHOW_MOVING_AVG,
                    PlotSettings.DEFAULT_PLOT_SHOW_MOVING_AVG);
            int movingAvgPoints = getInheritedAttrInt(plotElem, A_PLOT_MOVING_AVG_POINTS,
                    PlotSettings.DEFAULT_PLOT_MOVING_AVG_POINTS);
            double multiplier = getInheritedAttrDouble(plotElem, A_PLOT_MULTIPLIER,
                    PlotSettings.DEFAULT_PLOT_MULTIPLIER);
            double divisor = getInheritedAttrDouble(plotElem, A_PLOT_DIVISOR,
                    PlotSettings.DEFAULT_PLOT_DIVISOR);

            boolean nonNegative = getInheritedAttrBoolean(plotElem, A_PLOT_NON_NEGATIVE,
                    PlotSettings.DEFAULT_PLOT_NON_NEGATIVE);
            boolean percentTime = getInheritedAttrBoolean(plotElem, A_PLOT_PERCENT_TIME,
                    PlotSettings.DEFAULT_PLOT_PERCENT_TIME);
            String dataFunction = getAttr(plotElem, A_PLOT_DATA_FUNCTION,
                    PlotSettings.DEFAULT_PLOT_DATA_FUNCTION);
            String aggFunction = getAttr(plotElem, A_PLOT_AGGREGATE_FUNCTION,
                    PlotSettings.DEFAULT_PLOT_AGGREGATE_FUNCTION);
            String groupBy = getAttr(plotElem, A_PLOT_GROUP_BY);
            String ignore = getAttr(plotElem, A_PLOT_IGNORE, null);

            boolean optional = getInheritedAttrBoolean(plotElem, A_PLOT_OPTIONAL,
                    PlotSettings.DEFAULT_PLOT_OPTIONAL);

            GroupPlotSettings plot = new GroupPlotSettings(groupBy, ignore, infile, dataCol, showRaw,
                    showMovingAvg, movingAvgPoints, multiplier, divisor, nonNegative, percentTime, dataFunction,
                    aggFunction, optional);
            chart.addPlot(plot);
        }
        charts.add(chart);
    }
    return charts;
}

From source file:Controller.Controller.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, SQLException, NoSuchAlgorithmException {
    response.setContentType("text/html;charset=UTF-8");

    FlightDAO otherDAO = new FlightDAO();
    ResultSet rs = null;/*from w  w w .  j  a  v  a 2s.c om*/
    String service = request.getParameter("service");
    if (service == null) {
        service = "-1";
    }
    final String userManager = "cp_user_manager.jsp?current_page=user_manager";

    RequestDispatcher rd;
    if (service.equalsIgnoreCase("add_new_location")) {
        String location = request.getParameter("location");
        try {
            SAXReader reader = new SAXReader();
            String webAppPath = getServletContext().getRealPath("/");
            Document document = reader.read(webAppPath + "/xml/Locations.xml");
            Element rootElement = document.getRootElement();
            Node node = document.selectSingleNode("/Locations/Location[not(../Location/LocationId > Id)]");
            String id = node.valueOf("LocationId");
            System.out.println(id);
        } catch (DocumentException ex) {
            System.out.println("Add New Location Failed!");
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }
    } else if (service.equalsIgnoreCase("user_manager")) {
        rd = request.getRequestDispatcher(userManager);
        rd.forward(request, response);
        return;

    } else if (service.equalsIgnoreCase("login")) {
        String username1 = request.getParameter("username");
        String password1 = request.getParameter("password");
        SAXReader reader = new SAXReader();
        Document document;
        boolean found = false;
        String webAppPath = getServletContext().getRealPath("/");
        try {
            document = reader.read(webAppPath + "/xml/Customers.xml");
            Element root = document.getRootElement();
            for (Iterator i = root.elementIterator("Customer"); i.hasNext();) {
                Element elt = (Element) i.next();
                String username = elt.element("Name").getText();
                String password = elt.element("Password").getText();
                if (username.equalsIgnoreCase(username1) && password.equalsIgnoreCase(password1)) {
                    String role = elt.element("Role").getText();
                    String userid = elt.element("CustomerId").getText();
                    HttpSession session = request.getSession(true);
                    session.setAttribute("role", role);
                    session.setAttribute("username", username);
                    session.setAttribute("userid", userid);
                    rd = request.getRequestDispatcher("index.jsp");
                    rd.forward(request, response);
                    found = true;
                }
            }
            if (!found) {
                rd = request.getRequestDispatcher("login.jsp?errorCode=1");
                rd.forward(request, response);
            }
        } catch (DocumentException ex) {
            System.out.println("Login Failed!");
            Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        }

    } else if (service.equalsIgnoreCase("logout")) {
        String errorCode = "3";//(String) request.getParameter("errorCode");
        HttpSession session = request.getSession();
        if (session != null) {
            session.invalidate();
        }
        rd = request.getRequestDispatcher("login.jsp?errorCode=" + errorCode);
        rd.forward(request, response);

    } else {
        response.sendRedirect("notification.jsp?errorCode=2");
    }
}

From source file:DAO.CustomerDAO.java

public Customer getCustomerFromId(String webAppPath, int Id) {
    SAXReader reader = new SAXReader();
    Document document;//from  w ww. j a v a2  s .co m
    try {
        document = reader.read(webAppPath + "/xml/Customers.xml");
        Element root = document.getRootElement();
        for (Iterator i = root.elementIterator("Customer"); i.hasNext();) {
            Element elt = (Element) i.next();
            int customerId = Integer.parseInt(elt.element("CustomerId").getText());
            if (customerId == Id) {
                Customer customer = new Customer();
                customer.setCustomerId(customerId);
                customer.setEmail(elt.element("Email").getText());
                customer.setName(elt.element("Name").getText());
                customer.setPhone(elt.element("Phone").getText());
                customer.setRole(Integer.parseInt(elt.element("Role").getText()));
                return customer;
            }
        }
    } catch (DocumentException ex) {
        System.out.println("getCustomerFromId failed!");
    }
    return null;
}

From source file:DAO.FlightDAO.java

public Flight getFlightFromId(String webAppPath, int Id) {
    SAXReader reader = new SAXReader();
    Document document;/*from   w  ww .j av a2  s . c  o  m*/
    try {
        document = reader.read(webAppPath + "/xml/Flights.xml");
        Element root = document.getRootElement();
        LocationDAO locationDAO = new LocationDAO();
        PlaneDAO planeDAO = new PlaneDAO();
        for (Iterator i = root.elementIterator("Flight"); i.hasNext();) {
            Element elt = (Element) i.next();
            if (Id == Integer.parseInt(elt.element("FlightId").getText())) {
                Flight flight = new Flight();
                flight.setFlightId(Integer.parseInt(elt.element("FlightId").getText()));
                flight.setOrigin(Integer.parseInt(elt.element("Origin").getText()));
                flight.setDestination(Integer.parseInt(elt.element("Destination").getText()));
                flight.setFee(elt.element("Fee").getText());
                flight.setPlane(Integer.parseInt(elt.element("Plane").getText()));
                flight.setDepartureDate(elt.element("DepartureTime").getText());
                flight.setArrivalDate(elt.element("ArrivalTime").getText());
                flight.setOriginName(locationDAO.getLocationFromId(webAppPath, flight.getOrigin()).getName());
                flight.setDestinationName(
                        locationDAO.getLocationFromId(webAppPath, flight.getDestination()).getName());
                flight.setPlaneName(planeDAO.getPlaneFromId(webAppPath, flight.getPlane()).getModel());
                return flight;
            }
        }
    } catch (DocumentException ex) {
        System.out.println("fetchFlights failed!");
    }
    System.out.println("Done!");
    return null;
}

From source file:DAO.FlightDAO.java

public ArrayList<Flight> fetchFlights(String webAppPath) {
    ArrayList<Flight> arr = new ArrayList<Flight>();
    SAXReader reader = new SAXReader();
    Document document;/*from   w  w  w.  j  av a 2  s. c  om*/
    try {
        document = reader.read(webAppPath + "/xml/Flights.xml");
        Element root = document.getRootElement();
        LocationDAO locationDAO = new LocationDAO();
        PlaneDAO planeDAO = new PlaneDAO();
        for (Iterator i = root.elementIterator("Flight"); i.hasNext();) {
            Element elt = (Element) i.next();
            Flight flight = new Flight();
            flight.setFlightId(Integer.parseInt(elt.element("FlightId").getText()));
            flight.setOrigin(Integer.parseInt(elt.element("Origin").getText()));
            flight.setDestination(Integer.parseInt(elt.element("Destination").getText()));
            flight.setFee(elt.element("Fee").getText());
            flight.setPlane(Integer.parseInt(elt.element("Plane").getText()));
            flight.setDepartureDate(elt.element("DepartureTime").getText());
            flight.setArrivalDate(elt.element("ArrivalTime").getText());
            flight.setOriginName(locationDAO.getLocationFromId(webAppPath, flight.getOrigin()).getName());
            flight.setDestinationName(
                    locationDAO.getLocationFromId(webAppPath, flight.getDestination()).getName());
            flight.setPlaneName(planeDAO.getPlaneFromId(webAppPath, flight.getPlane()).getModel());
            arr.add(flight);
        }
    } catch (DocumentException ex) {
        System.out.println("fetchFlights failed!");
    }
    System.out.println("Done!");
    return arr;
}

From source file:DAO.LocationDAO.java

public Location getLocationFromId(String webAppPath, int Id) {
    SAXReader reader = new SAXReader();
    Document document;/* w  w  w.  jav a  2 s .c  o  m*/
    try {
        document = reader.read(webAppPath + "/xml/Locations.xml");
        Element root = document.getRootElement();
        for (Iterator i = root.elementIterator("Location"); i.hasNext();) {
            Element elt = (Element) i.next();
            int locationId = Integer.parseInt(elt.element("LocationId").getText());
            if (locationId == Id) {
                Location location = new Location();
                location.setLocationId(locationId);
                location.setName(elt.element("Name").getText());
                return location;
            }
        }
    } catch (DocumentException ex) {
        System.out.println("getLocationFromId failed!");
    }
    return null;
}

From source file:DAO.LocationDAO.java

public ArrayList<Location> fetchLocations(String webAppPath) {
    ArrayList<Location> arr = new ArrayList<Location>();
    SAXReader reader = new SAXReader();
    Document document;//from   w w w  . j av  a  2s.  co m
    try {
        document = reader.read(webAppPath + "/xml/Locations.xml");
        Element root = document.getRootElement();
        LocationDAO locationDAO = new LocationDAO();
        for (Iterator i = root.elementIterator("Location"); i.hasNext();) {
            Element elt = (Element) i.next();
            Location location = new Location();
            location.setLocationId(Integer.parseInt(elt.element("LocationId").getText()));
            location.setName(elt.element("Name").getText());
            arr.add(location);
        }
    } catch (DocumentException ex) {
        System.out.println("fetchLocations failed!");
    }
    System.out.println("Done!");
    return arr;
}

From source file:DAO.LocationDAO.java

public boolean addNewLocation(String webAppPath, String location) {
    try {/*from  w  w  w  .  ja v a  2s  . c om*/
        SAXReader reader = new SAXReader();
        Document document = reader.read(webAppPath + "/xml/Locations.xml");
        Element root = document.getRootElement();
        int max = 0;
        for (Iterator i = root.elementIterator("Location"); i.hasNext();) {
            Element elt = (Element) i.next();
            int cur = Integer.parseInt(elt.element("LocationId").getText());
            if (cur > max) {
                max = cur;
            }
        }

        Integer newLocationId = max + 1;
        Element newLocation = root.addElement("Location");
        newLocation.addElement("LocationId").setText(newLocationId.toString());
        newLocation.addElement("Name").setText(location);
        root.appendAttributes(newLocation);
        try {
            FileOutputStream fos = new FileOutputStream(webAppPath + "/xml/Locations.xml");
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter xmlwriter = new XMLWriter(fos, format);
            xmlwriter.write(document);
            System.out.println("dead");
            xmlwriter.close();
        } catch (Exception ex) {
            System.out.println("Failed!");
            ex.printStackTrace();
        }
        //Node node = document.selectSingleNode("/Locations/Location[not(../Location/LocationId > LocationId)]");
        //String id = node.valueOf("LocationId");
        //System.out.println(id);
        return true;
    } catch (DocumentException ex) {
        System.out.println("Add New Location Failed!");
        Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }
}

From source file:DAO.PlaneDAO.java

public Plane getPlaneFromId(String webAppPath, int Id) {
    SAXReader reader = new SAXReader();
    Document document;/*  ww w  .  j  a  va 2s  .  com*/
    try {
        document = reader.read(webAppPath + "/xml/Planes.xml");
        Element root = document.getRootElement();
        for (Iterator i = root.elementIterator("Plane"); i.hasNext();) {
            Element elt = (Element) i.next();
            int PlaneId = Integer.parseInt(elt.element("PlaneId").getText());
            if (PlaneId == Id) {
                Plane plane = new Plane();
                plane.setPlaneId(PlaneId);
                plane.setModel(elt.element("Model").getText());
                plane.setCapacity(Integer.parseInt(elt.element("Capacity").getText()));
                return plane;
            }
        }
    } catch (DocumentException ex) {
        System.out.println("getLocationFromId failed!");
    }
    return null;
}

From source file:DAO.PlaneDAO.java

public ArrayList<Plane> fetchPlanes(String webAppPath) {
    ArrayList<Plane> arr = new ArrayList<Plane>();
    SAXReader reader = new SAXReader();
    Document document;//from   w  ww.  j  a va  2 s.  c  o m
    try {
        document = reader.read(webAppPath + "/xml/Planes.xml");
        Element root = document.getRootElement();
        PlaneDAO planeDAO = new PlaneDAO();
        for (Iterator i = root.elementIterator("Plane"); i.hasNext();) {
            Element elt = (Element) i.next();
            Plane plane = new Plane();
            plane.setPlaneId(Integer.parseInt(elt.element("PlaneId").getText()));
            plane.setModel(elt.element("Model").getText());
            plane.setCapacity(Integer.parseInt(elt.element("Capacity").getText()));
            arr.add(plane);
        }
    } catch (DocumentException ex) {
        System.out.println("fetchPlanes failed!");
    }
    System.out.println("Done!");
    return arr;
}