Search in sources :

Example 1 with XMLSerializer

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

the class XMLViewer method startup.

@Override
public void startup(Display display, Map<String, String> properties) throws Exception {
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    bxmlSerializer.getNamespace().put(APPLICATION_KEY, this);
    window = (Window) bxmlSerializer.readObject(XMLViewer.class, "xml_viewer.bxml");
    bxmlSerializer.bind(this);
    Label prompt = new Label("Drag or paste XML here");
    prompt.getStyles().put(Style.horizontalAlignment, HorizontalAlignment.CENTER);
    prompt.getStyles().put(Style.verticalAlignment, VerticalAlignment.CENTER);
    promptDecorator.setOverlay(prompt);
    treeView.getDecorators().add(promptDecorator);
    window.setTitle(WINDOW_TITLE);
    window.open(display);
    window.requestFocus();
    if (System.in.available() > 0) {
        XMLSerializer xmlSerializer = new XMLSerializer();
        try {
            setDocument(xmlSerializer.readObject(System.in));
        } catch (Exception exception) {
        // No-op
        }
    }
}
Also used : BXMLSerializer(org.apache.pivot.beans.BXMLSerializer) XMLSerializer(org.apache.pivot.xml.XMLSerializer) Label(org.apache.pivot.wtk.Label) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer) IOException(java.io.IOException)

Example 2 with XMLSerializer

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

the class XMLViewer method drop.

public DropAction drop(Manifest dragContent) {
    DropAction dropAction = null;
    try {
        FileList fileList = dragContent.getFileList();
        if (fileList.getLength() == 1) {
            File file = fileList.get(0);
            XMLSerializer xmlSerializer = new XMLSerializer();
            @SuppressWarnings("resource") FileInputStream fileInputStream = null;
            try {
                try {
                    fileInputStream = new FileInputStream(file);
                    setDocument(xmlSerializer.readObject(fileInputStream));
                } finally {
                    if (fileInputStream != null) {
                        fileInputStream.close();
                    }
                }
            } catch (Exception exception) {
                Prompt.prompt(exception.getMessage(), window);
            }
            window.setTitle(WINDOW_TITLE + " - " + file.getName());
            dropAction = DropAction.COPY;
        } else {
            Prompt.prompt("Drop of multiple files is not supported.", window);
        }
    } catch (IOException exception) {
        Prompt.prompt(exception.getMessage(), window);
    }
    return dropAction;
}
Also used : BXMLSerializer(org.apache.pivot.beans.BXMLSerializer) XMLSerializer(org.apache.pivot.xml.XMLSerializer) FileList(org.apache.pivot.io.FileList) DropAction(org.apache.pivot.wtk.DropAction) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 3 with XMLSerializer

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

the class XMLSerializerTest method equalsTest.

@Test
public void equalsTest() throws IOException, SerializationException {
    XMLSerializer xmlSerializer = new XMLSerializer();
    XMLSerializerListener xmlSerializerListener = new XMLSerializerListener() {

        @Override
        public void beginElement(XMLSerializer xmlSerializerArgument, Element element) {
            System.out.println("Begin element: " + element);
        }

        @Override
        public void endElement(XMLSerializer xmlSerializerArgument) {
            System.out.println("End element");
        }

        @Override
        public void readTextNode(XMLSerializer xmlSerializerArgument, TextNode textNode) {
            System.out.println("Read text node: " + textNode);
        }
    };
    xmlSerializer.getXMLSerializerListeners().add(xmlSerializerListener);
    Element root1 = xmlSerializer.readObject(getClass().getResourceAsStream("sample.xml"));
    assertNotNull(root1);
    xmlSerializer.getXMLSerializerListeners().remove(xmlSerializerListener);
    Element root2 = xmlSerializer.readObject(getClass().getResourceAsStream("sample.xml"));
    assertNotNull(root2);
}
Also used : XMLSerializer(org.apache.pivot.xml.XMLSerializer) XMLSerializerListener(org.apache.pivot.xml.XMLSerializerListener) Element(org.apache.pivot.xml.Element) TextNode(org.apache.pivot.xml.TextNode) Test(org.junit.Test)

Example 4 with XMLSerializer

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

the class XMLViewer method paste.

public void paste() {
    Manifest clipboardContent = Clipboard.getContent();
    if (clipboardContent != null && clipboardContent.containsText()) {
        String xml = null;
        XMLSerializer xmlSerializer = new XMLSerializer();
        try {
            xml = clipboardContent.getText();
            setDocument(xmlSerializer.readObject(new StringReader(xml)));
        } catch (Exception exception) {
            Prompt.prompt(exception.getMessage(), window);
        }
        window.setTitle(WINDOW_TITLE);
    }
}
Also used : BXMLSerializer(org.apache.pivot.beans.BXMLSerializer) XMLSerializer(org.apache.pivot.xml.XMLSerializer) StringReader(java.io.StringReader) Manifest(org.apache.pivot.wtk.Manifest) IOException(java.io.IOException)

Example 5 with XMLSerializer

use of org.apache.pivot.xml.XMLSerializer 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)

Aggregations

XMLSerializer (org.apache.pivot.xml.XMLSerializer)6 IOException (java.io.IOException)4 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)3 Element (org.apache.pivot.xml.Element)3 Test (org.junit.Test)2 Desktop (java.awt.Desktop)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 StringReader (java.io.StringReader)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 FileList (org.apache.pivot.io.FileList)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 DropAction (org.apache.pivot.wtk.DropAction)1 Label (org.apache.pivot.wtk.Label)1