Search in sources :

Example 11 with SerializationException

use of org.apache.pivot.serialization.SerializationException in project pivot by apache.

the class JSONSerializer method readValue.

private Object readValue(Reader reader, Type typeArgument, String key) throws IOException, SerializationException {
    Object object = null;
    skipWhitespaceAndComments(reader);
    if (c == -1) {
        throw new SerializationException("Unexpected end of input stream.");
    }
    if (c == 'n') {
        object = readNullValue(reader);
    } else if (c == '"' || c == '\'') {
        object = readStringValue(reader, typeArgument, key);
    } else if (c == '+' || c == '-' || Character.isDigit(c)) {
        object = readNumberValue(reader, typeArgument, key);
    } else if (c == 't' || c == 'f') {
        object = readBooleanValue(reader, typeArgument, key);
    } else if (c == '[') {
        object = readListValue(reader, typeArgument, key);
    } else if (c == '{') {
        object = readMapValue(reader, typeArgument);
    } else {
        throw new SerializationException("Unexpected character in input stream: '" + (char) c + "'");
    }
    return object;
}
Also used : SerializationException(org.apache.pivot.serialization.SerializationException)

Example 12 with SerializationException

use of org.apache.pivot.serialization.SerializationException in project pivot by apache.

the class XMLSerializer method readObject.

public Element readObject(Reader reader) throws SerializationException {
    Utils.checkNull(reader, "reader");
    // Parse the XML stream
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    xmlInputFactory.setProperty("javax.xml.stream.isCoalescing", true);
    Element document = null;
    try {
        XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(reader);
        Element current = null;
        while (xmlStreamReader.hasNext()) {
            int event = xmlStreamReader.next();
            switch(event) {
                case XMLStreamConstants.CHARACTERS:
                    {
                        if (!xmlStreamReader.isWhiteSpace()) {
                            TextNode textNode = new TextNode(xmlStreamReader.getText());
                            // Notify listeners
                            if (xmlSerializerListeners != null) {
                                xmlSerializerListeners.readTextNode(this, textNode);
                            }
                            if (current != null) {
                                current.add(textNode);
                            }
                        }
                        break;
                    }
                case XMLStreamConstants.START_ELEMENT:
                    {
                        // Create the element
                        String prefix = xmlStreamReader.getPrefix();
                        if (prefix != null && prefix.length() == 0) {
                            prefix = null;
                        }
                        String localName = xmlStreamReader.getLocalName();
                        Element element = new Element(prefix, localName);
                        // Get the element's namespaces
                        for (int i = 0, n = xmlStreamReader.getNamespaceCount(); i < n; i++) {
                            String namespacePrefix = xmlStreamReader.getNamespacePrefix(i);
                            String namespaceURI = xmlStreamReader.getNamespaceURI(i);
                            if (namespacePrefix == null) {
                                element.setDefaultNamespaceURI(namespaceURI);
                            } else {
                                element.getNamespaces().put(namespacePrefix, namespaceURI);
                            }
                        }
                        // Get the element's attributes
                        for (int i = 0, n = xmlStreamReader.getAttributeCount(); i < n; i++) {
                            String attributePrefix = xmlStreamReader.getAttributePrefix(i);
                            if (attributePrefix != null && attributePrefix.length() == 0) {
                                attributePrefix = null;
                            }
                            String attributeLocalName = xmlStreamReader.getAttributeLocalName(i);
                            String attributeValue = xmlStreamReader.getAttributeValue(i);
                            element.getAttributes().add(new Element.Attribute(attributePrefix, attributeLocalName, attributeValue));
                        }
                        if (current == null) {
                            document = element;
                        } else {
                            current.add(element);
                        }
                        // Notify listeners
                        if (xmlSerializerListeners != null) {
                            xmlSerializerListeners.beginElement(this, element);
                        }
                        current = element;
                        break;
                    }
                case XMLStreamConstants.END_ELEMENT:
                    {
                        // Notify listeners
                        if (xmlSerializerListeners != null) {
                            xmlSerializerListeners.endElement(this);
                        }
                        // Move up the stack
                        if (current != null) {
                            current = current.getParent();
                        }
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }
    } catch (XMLStreamException exception) {
        throw new SerializationException(exception);
    }
    return document;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) SerializationException(org.apache.pivot.serialization.SerializationException) XMLStreamException(javax.xml.stream.XMLStreamException) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 13 with SerializationException

use of org.apache.pivot.serialization.SerializationException in project pivot by apache.

the class SwingDemo method createPivotFrame.

private static void createPivotFrame() {
    // Create the internal frame that will contain the Pivot components
    JInternalFrame internalFrame = new JInternalFrame("Pivot Components");
    desktop.add(internalFrame);
    // Create the display host
    ApplicationContext.DisplayHost displayHost = new ApplicationContext.DisplayHost();
    internalFrame.add(displayHost);
    // Add the display to the display list
    displays.add(displayHost.getDisplay());
    // Load the Pivot window
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    Window window;
    try {
        window = (Window) bxmlSerializer.readObject(SwingDemo.class.getResource("pivot_window.bxml"));
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    } catch (SerializationException exception) {
        throw new RuntimeException(exception);
    }
    // Open the Pivot window on the display
    window.open(displayHost.getDisplay());
    // Open and select the internal frame
    internalFrame.setLocation(240, 100);
    internalFrame.setSize(480, 360);
    internalFrame.setVisible(true);
    internalFrame.setResizable(true);
    try {
        internalFrame.setSelected(true);
    } catch (PropertyVetoException exception) {
        throw new RuntimeException(exception);
    }
}
Also used : Window(org.apache.pivot.wtk.Window) PropertyVetoException(java.beans.PropertyVetoException) ApplicationContext(org.apache.pivot.wtk.ApplicationContext) SerializationException(org.apache.pivot.serialization.SerializationException) IOException(java.io.IOException) JInternalFrame(javax.swing.JInternalFrame) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Example 14 with SerializationException

use of org.apache.pivot.serialization.SerializationException in project pivot by apache.

the class RESTDemoServlet method doGet.

@SuppressWarnings("resource")
@Override
protected Object doGet(Path path) throws QueryException {
    if (path.getLength() != 1) {
        throw new QueryException(Query.Status.BAD_REQUEST);
    }
    // Read the value from the temp file
    File directory = new File(System.getProperty("java.io.tmpdir"));
    File file = new File(directory, path.get(0));
    if (!file.exists()) {
        throw new QueryException(Query.Status.NOT_FOUND);
    }
    Object value;
    try {
        JSONSerializer jsonSerializer = new JSONSerializer();
        value = jsonSerializer.readObject(new FileInputStream(file));
    } catch (IOException exception) {
        throw new QueryException(Query.Status.INTERNAL_SERVER_ERROR);
    } catch (SerializationException exception) {
        throw new QueryException(Query.Status.INTERNAL_SERVER_ERROR);
    }
    return value;
}
Also used : QueryException(org.apache.pivot.web.QueryException) SerializationException(org.apache.pivot.serialization.SerializationException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) JSONSerializer(org.apache.pivot.json.JSONSerializer)

Example 15 with SerializationException

use of org.apache.pivot.serialization.SerializationException in project pivot by apache.

the class EnumBeanTest method main.

public static void main(String[] args) {
    BXMLSerializer bxmlSerializer = new BXMLSerializer();
    try {
        EnumBean enumBean = (EnumBean) bxmlSerializer.readObject(EnumBeanTest.class, "enum_bean.bxml");
        System.out.println("Bean read OK - " + enumBean);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (SerializationException e) {
        e.printStackTrace();
    }
    EnumBean enumBean = new EnumBean();
    BeanAdapter ba = new BeanAdapter(enumBean);
    ba.put("orientationField", Orientation.HORIZONTAL);
    dumpField(enumBean, ba);
    ba.put("orientationField", "vertical");
    dumpField(enumBean, ba);
    ba.put("orientation", Orientation.HORIZONTAL);
    dumpSetter(enumBean, ba);
    ba.put("orientation", Orientation.VERTICAL);
    dumpSetter(enumBean, ba);
    ba.put("orientation", null);
    dumpSetter(enumBean, ba);
// Force an error to check the IllegalArgumentException message
// ba.put("orientation", Vote.APPROVE);
}
Also used : SerializationException(org.apache.pivot.serialization.SerializationException) IOException(java.io.IOException) BeanAdapter(org.apache.pivot.beans.BeanAdapter) BXMLSerializer(org.apache.pivot.beans.BXMLSerializer)

Aggregations

SerializationException (org.apache.pivot.serialization.SerializationException)49 IOException (java.io.IOException)28 BXMLSerializer (org.apache.pivot.beans.BXMLSerializer)14 URL (java.net.URL)9 JSONSerializer (org.apache.pivot.json.JSONSerializer)9 ArrayList (org.apache.pivot.collections.ArrayList)8 Component (org.apache.pivot.wtk.Component)7 File (java.io.File)6 List (org.apache.pivot.collections.List)6 QueryException (org.apache.pivot.web.QueryException)6 ComponentMouseButtonListener (org.apache.pivot.wtk.ComponentMouseButtonListener)6 InputStream (java.io.InputStream)5 Sequence (org.apache.pivot.collections.Sequence)5 Button (org.apache.pivot.wtk.Button)5 ButtonPressListener (org.apache.pivot.wtk.ButtonPressListener)5 Mouse (org.apache.pivot.wtk.Mouse)5 PushButton (org.apache.pivot.wtk.PushButton)5 MalformedURLException (java.net.MalformedURLException)4 ScriptException (javax.script.ScriptException)4 TextInput (org.apache.pivot.wtk.TextInput)4