Package org.dom4j

Examples of org.dom4j.Element


    }

    public Collection readCollection(Element eventsElement, Class clazz) {
        List res = new ArrayList();
        for (Object obj : eventsElement.elements()) {
            Element element = (Element) obj;
            long ref = getRef(element);
            res.add(getObject(ref, clazz));
        }
        return res;
    }
View Full Code Here


    }

    public Collection readConstraints(Element constrElement) {
        List<Constraint> res = new ArrayList();
        for (Object obj : constrElement.elements()) {
            Element element = (Element) obj;
            Constraint constraint = null;

            if (RasterConstraint.class.getSimpleName().equals(element.getName())) {
                BitSet bs = getResult(obj, BitSet.class, element);
                int length = Integer.parseInt(element.attributeValue("length"));
                constraint = new RasterConstraint(new WeekRasterImpl(
                        new BitRasterImpl(bs, length)));

            } else if (EventOrderConstraint.class.getSimpleName().equals(element.getName())) {
                UnsupportedOperationException excFirstEl = new UnsupportedOperationException(
                        "First element in eventOrderConstraint must be the event!" + element.getPath());
                for (Object eocObj : element.elements()) {
                    Element eocEl = (Element) eocObj;
                    if ("event".equals(eocEl.getName())) {
                        Event ev = getObject(getRef(eocEl), Event.class);
                        constraint = new EventOrderConstraint(ev);
                    } else if ("beforeEvent".equals(eocEl.getName())) {
                        if (constraint == null)
                            throw excFirstEl;
                        Event ev = getObject(getRef(eocEl), Event.class);
                        ((EventOrderConstraint) constraint).addBefore(ev);
                    } else if ("followsEvent".equals(eocEl.getName())) {
                        if (constraint == null)
                            throw excFirstEl;
                        Event ev = getObject(getRef(eocEl), Event.class);
                        ((EventOrderConstraint) constraint).addFollow(ev);
                    } else
                        throw new UnsupportedOperationException("Element not supported in eventOrderConstraint:"
                                + eocEl.getPath());
                }
            } else if (PersonITCRasterConstraint.class.getSimpleName().equals(element.getName())) {
                Person p = getObject(getRef(element.element("person")), Person.class);
                constraint = new PersonITCRasterConstraint(p, settings);
View Full Code Here

    }

    public void save() throws IOException {
        File xmlFile = getSettingsFile();
        Document document = DocumentHelper.createDocument();
        Element settingsElement = document.addElement("settings");
        settingsElement.addAttribute("version", "4.1");

        for (Entry<String, Object> entry : settingsMap.entrySet()) {
            Class clazz = entry.getValue().getClass();
            Writing w = objWriting.getWriting(clazz);
            if (w == null)
                throw new UnsupportedOperationException("No writing implemented for class:" + clazz);

            settingsElement.addElement(entry.getKey()).
                    addAttribute("class", clazz.getName()).
                    setText(w.toString(entry.getValue()));
        }

        XMLWriter output = new XMLWriter(new FileOutputStream(xmlFile), new OutputFormat("  ", true));
View Full Code Here

            Document document = saxReader.read(new FileInputStream(xmlFile));

            List list = document.getRootElement().elements();
            Iterator iter = list.iterator();
            while (iter.hasNext()) {
                Element element = (Element) iter.next();
                try {
                    Class clazz = Class.forName(element.attributeValue("class"));
                    Parsing p = objParsing.getParsing(clazz);
                    Object obj = p.parse(element.getText());
                    setObject(element.getName(), obj);
                } catch (Exception ex) {
                    throw new UnsupportedOperationException("Couldn't read property:"
                            + element.getName() + " from settings file:" + xmlFile, ex);
                }
            }
        } catch (FileNotFoundException ex) {
            logger.warn("File not found. use default settings");
        } catch (Exception ex) {
View Full Code Here

    _catalinaLibdir = FileUtils.createFolder(_catalinaBase, "lib");
    _catalinaConf = FileUtils.createFolder(_catalinaBase, CATALINA_CONF_DIRNAME);

    // create default auth.xml
    Document auth = DocumentFactory.getInstance().createDocument();
    Element users = auth.addElement("users");
    users.addAttribute("allowanonymous", "true");
    Element designer = users.addElement("user");
    designer.addAttribute("name", "designer");
    designer.addAttribute("password", "wga");
    designer.addAttribute("mail", "designer@example.com");
    designer.addAttribute("aliases", "");
    Element managers = users.addElement("group");
    managers.addAttribute("name", "managers");
    managers.addAttribute("members", "designer");

    XMLWriter writer = null;
    try {
      writer = new XMLWriter(OutputFormat.createPrettyPrint());
      writer.setOutputStream(new FileOutputStream(new File(getWGABase().getLocation().toFile(), "auth.xml")));
View Full Code Here

    String linkTarget = computeDirLinkTarget(dirlinkFolder, target);
    SAXReader saxReader = new SAXReader();
    try {
      File dirlinkFile = new File(dirlink.getLocationURI().getPath());
      Document document = saxReader.read(dirlinkFile);
      Element ele = (Element)document.selectSingleNode("/dirlink/path");    //TODO use statics
      ele.addAttribute("location", linkTarget);                //TODO use statics
        XMLWriter output = new XMLWriter(new FileWriter(dirlinkFile));
        try {
          output.write( document );
        } finally {
          try {
View Full Code Here

          File catalinaBase = new File(WGADesignerPlugin.getDefault().getStateLocation().toFile(), "tomcat_current");
          File conf = new File(catalinaBase, "conf");
          File serverXML = new File(conf, "server.xml");
          SAXReader reader = new SAXReader();
          Document doc = reader.read(serverXML);
          Element serverElement = (Element) doc.selectSingleNode("/Server");
          String serverPort = serverElement.attributeValue("port", "8005");
          String shutdownCommand = serverElement.attributeValue("shutdown", "SHUTDOWN");
         
          // send shutdown command to specified port
          Socket socket = new Socket();
          socket.connect(new InetSocketAddress("127.0.0.1", Integer.parseInt(serverPort)), 1000);
          socket.setSoTimeout(5000);
View Full Code Here

   }

   public Element createElement(String name, String value)
         throws UnsupportedEncodingException
   {
      Element e = DocumentFactory.getInstance().createElement(name);
      if (value != null)
         e.addText(URLEncoder.encode(value, StringWrapper.DEFAULT_ENCODING));
      return e;
   }
View Full Code Here

      String[] values = new String[2];
      values[0] = "foo";
      values[1] = "bar";

      Element e = createElement("bag", null);
      e.addElement("element").addElement("str").addText(values[0]);
      e.addElement("element").addElement("str").addText(values[1]);
      wrapper.setElement(e);

      // String Array
      String[] converted = (String[]) wrapper.convert(String[].class);
      assert values.length == converted.length;
      assertEquals(values[0], converted[0]);
      assertEquals(values[1], converted[1]);

      // List
      List convertedList = (List) wrapper.convert(List.class);
      assert values.length == convertedList.size();
      assertEquals(values[0], convertedList.get(0));
      assertEquals(values[1], convertedList.get(1));

      // List<String> (Generic type)

      // Isn't there an easier way of getting a generic type than this??
      Type t = RemotingTest.class.getMethod("dummy").getGenericReturnType();

      List<String> stringList = (List<String>) wrapper.convert(t);
      assert values.length == stringList.size();
      assertEquals(values[0], stringList.get(0));
      assertEquals(values[1], stringList.get(1));

      // Set
      Set s = (Set) wrapper.convert(Set.class);
      assert values.length == s.size();
      assert s.contains(values[0]);
      assert s.contains(values[1]);

      // Queue
      Queue q = (Queue) wrapper.convert(Queue.class);
      assert values.length == q.size();
      assert q.contains(values[0]);
      assert q.contains(values[1]);

      // Concrete class
      ArrayList l = (ArrayList) wrapper.convert(ArrayList.class);
      assert values.length == l.size();
      assertEquals(values[0], l.get(0));
      assertEquals(values[1], l.get(1));

      try
      {
         // This should throw a ConversionException
         wrapper.convert(InvalidList.class);
         assert false;
      }
      catch (ConversionException ex)
      {
      }

      t = RemotingTest.class.getMethod("dummyInvalid").getGenericReturnType();
      try
      {
         // This should throw a ConversionException also
         wrapper.convert(t);
         assert false;
      }
      catch (ConversionException ex)
      {
      }

      int[] intValues = new int[2];
      intValues[0] = 44444;
      intValues[1] = 55555;

      e = createElement("bag", null);
      e.addElement("element").addElement("number").addText("" + intValues[0]);
      e.addElement("element").addElement("number").addText("" + intValues[1]);
      wrapper.setElement(e);

      // int Array
      int[] convertedInts = (int[]) wrapper.convert(int[].class);
      assert intValues.length == convertedInts.length;
View Full Code Here

   {
      MapWrapper wrapper = new MapWrapper();
      wrapper.setCallContext(new CallContext());

      // Construct a map with two elements, foo:aaaaa and bar:zzzzz
      Element e = DocumentFactory.getInstance().createElement("map");
      Element child = e.addElement("element");
      child.addElement("k").addElement("str").addText("foo");
      child.addElement("v").addElement("str").addText("aaaaa");
      child = e.addElement("element");
      child.addElement("k").addElement("str").addText("bar");
      child.addElement("v").addElement("str").addText("zzzzz");

      wrapper.setElement(e);

      // Conversion tests
      Map m = (Map) wrapper.convert(Map.class);
      assert m.containsKey("foo");
      assert m.containsKey("bar");
      assert "aaaaa".equals(m.get("foo"));
      assert "zzzzz".equals(m.get("bar"));

      m = (Map) wrapper.convert(HashMap.class);
      assert m.containsKey("foo");
      assert m.containsKey("bar");
      assert "aaaaa".equals(m.get("foo"));
      assert "zzzzz".equals(m.get("bar"));

      Type t = RemotingTest.class.getMethod("dummyMap").getGenericReturnType();
      m = (Map) wrapper.convert(t);
      assert m.containsKey("foo");
      assert m.containsKey("bar");
      assert "aaaaa".equals(m.get("foo"));
      assert "zzzzz".equals(m.get("bar"));

      try
      {
         // This should throw a ConversionException
         wrapper.convert(InvalidClass.class);
         assert false;
      }
      catch (ConversionException ex)
      {
      }

      try
      {
         // This should throw a ConversionException also
         wrapper.convert(InvalidMap.class);
         assert false;
      }
      catch (ConversionException ex)
      {
      }

      // ensure when we marshal/unmarshal the values in the map remain the same     
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      wrapper.marshal(out);
     
      SAXReader xmlReader = new SAXReader();
      Document doc = xmlReader.read( new ByteArrayInputStream(out.toByteArray()) );    
     
      Element root = doc.getRootElement();     
     
      MapWrapper other = new MapWrapper();
      other.setCallContext(new CallContext());
      other.setElement(root);
     
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.