Search in sources :

Example 6 with Element

use of org.apache.pivot.xml.Element in project pivot by apache.

the class XMLSerializerTest method basicTest.

@Test
public void basicTest() throws IOException, SerializationException {
    XMLSerializer xmlSerializer = new XMLSerializer();
    Element root = xmlSerializer.readObject(getClass().getResourceAsStream("sample.xml"));
    assertEquals(root.getName(), "root");
    Element a = XML.getElement(root, "a");
    assertEquals(a.getName(), "a");
    assertEquals(a.getElementDictionary().get("id"), "x");
    Element b = XML.getElement(root, "a/b");
    assertEquals(b.getName(), "b");
    assertEquals(b.getElementDictionary().get("id"), "y");
    b = XML.getElement(a, "b");
    assertEquals(b.getName(), "b");
    assertEquals(b.getElementDictionary().get("id"), "y");
    List<Element> cs = XML.getElements(root, "a/b", "c");
    assertEquals(cs.getLength(), 1);
    List<Element> fs = XML.getElements(root, "d/e", "f");
    assertEquals(fs.getLength(), 4);
    Element e = XML.getElement(root, "d/e");
    Element f = XML.getElement(e, "f");
    assertEquals(f.getName(), "f");
    Element g = XML.getElement(e, "g");
    assertEquals(g.getName(), "g");
    String ft = XML.getText(root, "d/e/f");
    assertEquals(ft, "1");
    String gt = XML.getText(root, "d/e/g");
    assertEquals(gt, "4");
    assertNull(XML.getElement(root, "a/b/n"));
    assertNull(XML.getText(root, "a/b/n"));
    assertEquals(XML.getElements(root, "a/b", "n").getLength(), 0);
    assertEquals(XML.getText(root, "d/foo:h"), "Hello");
    List<Element> is = XML.getElements(e, "is", "i");
    assertEquals(is.getLength(), 3);
    assertEquals(XML.getText(root, "d[0]/e[0]/f[2]"), "3");
    assertEquals(XML.getText(root, "d[0]/e[1]"), null);
}
Also used : XMLSerializer(org.apache.pivot.xml.XMLSerializer) Element(org.apache.pivot.xml.Element) Test(org.junit.Test)

Example 7 with Element

use of org.apache.pivot.xml.Element in project pivot by apache.

the class RSSFeedDemo method initialize.

@Override
public void initialize(Map<String, Object> namespace, URL location, Resources resources) {
    feedListView = (ListView) namespace.get("feedListView");
    cardPane = (CardPane) namespace.get("cardPane");
    statusLabel = (Label) namespace.get("statusLabel");
    feedListView.getComponentMouseButtonListeners().add(new ComponentMouseButtonListener() {

        private int index = -1;

        @Override
        public boolean mouseClick(Component component, Mouse.Button button, int x, int y, int count) {
            if (count == 1) {
                index = feedListView.getItemAt(y);
            } else if (count == 2 && feedListView.getItemAt(y) == index) {
                Element itemElement = (Element) feedListView.getListData().get(index);
                String link = XML.getText(itemElement, "link");
                Desktop desktop = Desktop.getDesktop();
                try {
                    desktop.browse(new URL(link).toURI());
                } catch (MalformedURLException exception) {
                    throw new RuntimeException(exception);
                } catch (URISyntaxException exception) {
                    throw new RuntimeException(exception);
                } catch (IOException exception) {
                    System.out.println("Unable to open " + link + " in default browser.");
                }
            }
            return false;
        }
    });
    GetQuery getQuery = new GetQuery("feeds.dzone.com", "/javalobby/frontpage");
    getQuery.setSerializer(new XMLSerializer());
    getQuery.getParameters().put("format", "xml");
    getQuery.execute(new TaskAdapter<>(new TaskListener<Object>() {

        @Override
        public void taskExecuted(Task<Object> task) {
            Element root = (Element) task.getResult();
            feedListView.setListData(XML.getElements(root, "channel", "item"));
            cardPane.setSelectedIndex(1);
        }

        @Override
        public void executeFailed(Task<Object> task) {
            statusLabel.setText(task.getFault().toString());
        }
    }));
}
Also used : MalformedURLException(java.net.MalformedURLException) XMLSerializer(org.apache.pivot.xml.XMLSerializer) Task(org.apache.pivot.util.concurrent.Task) Element(org.apache.pivot.xml.Element) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URL(java.net.URL) Mouse(org.apache.pivot.wtk.Mouse) Desktop(java.awt.Desktop) GetQuery(org.apache.pivot.web.GetQuery) TaskListener(org.apache.pivot.util.concurrent.TaskListener) ComponentMouseButtonListener(org.apache.pivot.wtk.ComponentMouseButtonListener) Component(org.apache.pivot.wtk.Component)

Aggregations

Element (org.apache.pivot.xml.Element)7 TextNode (org.apache.pivot.xml.TextNode)5 XMLSerializer (org.apache.pivot.xml.XMLSerializer)3 Color (java.awt.Color)2 Font (java.awt.Font)2 Test (org.junit.Test)2 Desktop (java.awt.Desktop)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 ArrayList (org.apache.pivot.collections.ArrayList)1 HashMap (org.apache.pivot.collections.HashMap)1 Task (org.apache.pivot.util.concurrent.Task)1 TaskListener (org.apache.pivot.util.concurrent.TaskListener)1 GetQuery (org.apache.pivot.web.GetQuery)1 Component (org.apache.pivot.wtk.Component)1 ComponentMouseButtonListener (org.apache.pivot.wtk.ComponentMouseButtonListener)1 Mouse (org.apache.pivot.wtk.Mouse)1 Node (org.apache.pivot.xml.Node)1