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
}
}
}
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;
}
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);
}
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);
}
}
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);
}
Aggregations