Package org.apache.pivot.serialization

Examples of org.apache.pivot.serialization.SerializationException


                // The key is an undelimited string; it must adhere to Java
                // identifier syntax
                StringBuilder keyBuilder = new StringBuilder();

                if (!Character.isJavaIdentifierStart(c)) {
                    throw new SerializationException("Illegal identifier start character.");
                }

                while (c != -1
                    && c != ':' && !Character.isWhitespace(c)) {
                    if (!Character.isJavaIdentifierPart(c)) {
                        throw new SerializationException("Illegal identifier character.");
                    }

                    keyBuilder.append((char)c);
                    c = reader.read();
                }

                if (c == -1) {
                    throw new SerializationException("Unexpected end of input stream.");
                }

                key = keyBuilder.toString();
            }

            if (key == null
                || key.length() == 0) {
                throw new SerializationException("\"" + key + "\" is not a valid key.");
            }

            // Notify listeners
            if (jsonSerializerListeners != null) {
                jsonSerializerListeners.readKey(this, key);
            }

            skipWhitespaceAndComments(reader);

            if (c != ':') {
                throw new SerializationException("Unexpected character in input stream.");
            }

            // Move to the first character after ':'
            c = reader.read();

            if (valueType == null) {
                // The map is a bean instance; get the generic type of the property
                Type genericValueType = ((BeanAdapter)dictionary).getGenericType(key);

                if (genericValueType != null) {
                    // Set the value in the bean
                    dictionary.put(key, readValue(reader, genericValueType));
                } else {
                    // The property does not exist; ignore this value
                    readValue(reader, Object.class);
                }
            } else {
                dictionary.put(key, readValue(reader, valueType));
            }

            skipWhitespaceAndComments(reader);

            if (c == ',') {
                c = reader.read();
                skipWhitespaceAndComments(reader);
            } else if (c == -1) {
                throw new SerializationException("Unexpected end of input stream.");
            } else {
                if (c != '}') {
                    throw new SerializationException("Unexpected character in input stream.");
                }
            }
        }

        // Move to the first character after '}'
View Full Code Here


            if (number instanceof Float) {
                Float f = (Float)number;
                if (f.isNaN()
                    || f.isInfinite()) {
                    throw new SerializationException(number + " is not a valid value.");
                }
            } else if (number instanceof Double) {
                Double d = (Double)number;
                if (d.isNaN()
                    || d.isInfinite()) {
                    throw new SerializationException(number + " is not a valid value.");
                }
            }

            writer.append(number.toString());
        } else if (object instanceof Boolean) {
View Full Code Here

                        break;
                    }
                }
            }
        } catch (XMLStreamException exception) {
            throw new SerializationException(exception);
        }

        return document;
    }
View Full Code Here

            XMLStreamWriter xmlStreamWriter = output.createXMLStreamWriter(writer);
            xmlStreamWriter.writeStartDocument();
            writeElement(element, xmlStreamWriter);
            xmlStreamWriter.writeEndDocument();
        } catch (XMLStreamException exception) {
            throw new SerializationException(exception);
        }
    }
View Full Code Here

            if (node instanceof Element) {
                writeElement((Element)node, xmlStreamWriter);
            } else if (node instanceof TextNode) {
                writeTextNode((TextNode)node, xmlStreamWriter);
            } else {
                throw new SerializationException("Unsupported node type: "
                    + node.getClass().getName());
            }
        }

        if (element.getLength() > 0) {
View Full Code Here

                            break;
                        }
                    }
                }
            } catch (XMLStreamException exception) {
                throw new SerializationException(exception);
            }
        } catch (IOException exception) {
            logException();
            throw exception;
        } catch (SerializationException exception) {
View Full Code Here

        String piTarget = xmlStreamReader.getPITarget();
        String piData = xmlStreamReader.getPIData();

        if (piTarget.equals(LANGUAGE_PROCESSING_INSTRUCTION)) {
            if (language != null) {
                throw new SerializationException("Language already set.");
            }

            language = piData;
        }
    }
View Full Code Here

                        try {
                            Method addMethod = sequence.getClass().getMethod("add", String.class);
                            addMethod.invoke(sequence, text);
                        } catch (NoSuchMethodException exception) {
                            throw new SerializationException("Text content cannot be added to "
                                + sequence.getClass().getName() + ".", exception);
                        } catch (InvocationTargetException exception) {
                            throw new SerializationException(exception);
                        } catch (IllegalAccessException exception) {
                            throw new SerializationException(exception);
                        }
                    }

                    break;
                }

                case WRITABLE_PROPERTY:
                case LISTENER_LIST_PROPERTY:
                case SCRIPT: {
                    element.value = text;
                    break;
                }

                default: {
                    throw new SerializationException("Unexpected characters in "
                        + element.type + " element.");
                }
            }
        }
    }
View Full Code Here

        if (prefix != null
            && prefix.equals(BXML_PREFIX)) {
            // The element represents a BXML operation
            if (element == null) {
                throw new SerializationException("Invalid root element.");
            }

            if (localName.equals(INCLUDE_TAG)) {
                elementType = Element.Type.INCLUDE;
            } else if (localName.equals(SCRIPT_TAG)) {
                elementType = Element.Type.SCRIPT;
            } else if (localName.equals(DEFINE_TAG)) {
                elementType = Element.Type.DEFINE;
            } else if (localName.equals(REFERENCE_TAG)) {
                elementType = Element.Type.REFERENCE;
            } else {
                throw new SerializationException("Invalid element.");
            }

            name = "<" + prefix + ":" + localName + ">";
        } else {
            if (Character.isUpperCase(localName.charAt(0))) {
                int i = localName.indexOf('.');

                if (i != -1
                    && Character.isLowerCase(localName.charAt(i + 1))) {
                    // The element represents an attached property
                    elementType = Element.Type.WRITABLE_PROPERTY;
                    name = localName.substring(i + 1);

                    String propertyClassName = namespaceURI + "." + localName.substring(0, i);
                    try {
                        propertyClass = Class.forName(propertyClassName);
                    } catch (ClassNotFoundException exception) {
                        throw new SerializationException(exception);
                    }
                } else {
                    // The element represents a typed object
                    if (namespaceURI == null) {
                        throw new SerializationException("No XML namespace specified for "
                            + localName + " tag.");
                    }

                    elementType = Element.Type.INSTANCE;
                    name = "<" + ((prefix == null) ? "" : prefix + ":") + localName + ">";

                    String className = namespaceURI + "." + localName.replace('.', '$');

                    try {
                        Class<?> type = Class.forName(className);
                        value = newTypedObject(type);
                    } catch (ClassNotFoundException exception) {
                        throw new SerializationException(exception);
                    } catch (InstantiationException exception) {
                        throw new SerializationException(exception);
                    } catch (IllegalAccessException exception) {
                        throw new SerializationException(exception);
                    }
                }
            } else {
                // The element represents a property
                if (prefix != null) {
                    throw new SerializationException("Property elements cannot have a namespace prefix.");
                }

                if (element.value instanceof Dictionary<?, ?>) {
                    elementType = Element.Type.WRITABLE_PROPERTY;
                } else {
                    BeanAdapter beanAdapter = new BeanAdapter(element.value);

                    if (beanAdapter.isReadOnly(localName)) {
                        Class<?> propertyType = beanAdapter.getType(localName);
                        if (propertyType == null) {
                            throw new SerializationException("\"" + localName
                                + "\" is not a valid property of element "
                                + element.name + ".");
                        }

                        if (ListenerList.class.isAssignableFrom(propertyType)) {
                            elementType = Element.Type.LISTENER_LIST_PROPERTY;
                        } else {
                            elementType = Element.Type.READ_ONLY_PROPERTY;
                            value = beanAdapter.get(localName);
                            assert (value != null) : "Read-only properties cannot be null.";
                        }
                    } else {
                        elementType = Element.Type.WRITABLE_PROPERTY;
                    }
                }

                name = localName;
            }
        }

        // Create the element and process the attributes
        element = new Element(element, elementType, name, propertyClass, value);
        processAttributes();

        if (elementType == Element.Type.INCLUDE) {
            // Load the include
            if (!element.properties.containsKey(INCLUDE_SRC_ATTRIBUTE)) {
                throw new SerializationException(INCLUDE_SRC_ATTRIBUTE
                    + " attribute is required for " + BXML_PREFIX + ":" + INCLUDE_TAG
                    + " tag.");
            }

            String src = element.properties.get(INCLUDE_SRC_ATTRIBUTE);

            Resources resources = this.resources;
            if (element.properties.containsKey(INCLUDE_RESOURCES_ATTRIBUTE)) {
                resources = new Resources(resources,
                    element.properties.get(INCLUDE_RESOURCES_ATTRIBUTE));
            }

            String mimeType = null;
            if (element.properties.containsKey(INCLUDE_MIME_TYPE_ATTRIBUTE)) {
                mimeType = element.properties.get(INCLUDE_MIME_TYPE_ATTRIBUTE);
            }

            if (mimeType == null) {
                // Get the file extension
                int i = src.lastIndexOf(".");
                if (i != -1) {
                    String extension = src.substring(i + 1);
                    mimeType = fileExtensions.get(extension);
                }
            }

            if (mimeType == null) {
                throw new SerializationException("Cannot determine MIME type of include \""
                    + src + "\".");
            }

            boolean inline = false;
            if (element.properties.containsKey(INCLUDE_INLINE_ATTRIBUTE)) {
                inline = Boolean.parseBoolean(element.properties.get(INCLUDE_INLINE_ATTRIBUTE));
            }

            // Determine an appropriate serializer to use for the include
            Class<? extends Serializer<?>> serializerClass = mimeTypes.get(mimeType);

            if (serializerClass == null) {
                throw new SerializationException("No serializer associated with MIME type "
                    + mimeType + ".");
            }

            Serializer<?> serializer;
            try {
                serializer = newIncludeSerializer(serializerClass);
            } catch (InstantiationException exception) {
                throw new SerializationException(exception);
            } catch (IllegalAccessException exception) {
                throw new SerializationException(exception);
            }

            // Determine location from src attribute
            URL location;
            if (src.charAt(0) == '/') {
                ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
                location = classLoader.getResource(src.substring(1));
            } else {
                location = new URL(this.location, src);
            }

            // Set optional resolution properties
            if (serializer instanceof Resolvable) {
                Resolvable resolvable = (Resolvable)serializer;
                if (inline) {
                    resolvable.setNamespace(namespace);
                }

                resolvable.setLocation(location);
                resolvable.setResources(resources);
            }

            // Read the object
            InputStream inputStream = new BufferedInputStream(location.openStream());
            try {
                element.value = serializer.readObject(inputStream);
            } finally {
                inputStream.close();
            }
        } else if (element.type == Element.Type.REFERENCE) {
            // Dereference the value
            if (!element.properties.containsKey(REFERENCE_ID_ATTRIBUTE)) {
                throw new SerializationException(REFERENCE_ID_ATTRIBUTE
                    + " attribute is required for " + BXML_PREFIX + ":" + REFERENCE_TAG
                    + " tag.");
            }

            String id = element.properties.get(REFERENCE_ID_ATTRIBUTE);
            if (!namespace.containsKey(id)) {
                throw new SerializationException("A value with ID \"" + id + "\" does not exist.");
            }

            element.value = namespace.get(id);
        }
View Full Code Here

                        || value.contains(".")) {
                        throw new IllegalArgumentException("\"" + value + "\" is not a valid ID value.");
                    }

                    if (namespace.containsKey(value)) {
                        throw new SerializationException("ID " + value + " is already in use.");
                    }

                    if (element.type != Element.Type.INSTANCE
                        && element.type != Element.Type.INCLUDE) {
                        throw new SerializationException("An ID cannot be assigned to this element.");
                    }

                    element.id = value;
                } else {
                    throw new SerializationException(BXML_PREFIX + ":" + localName
                        + " is not a valid attribute.");
                }
            } else {
                boolean property = false;

                switch (element.type) {
                    case INCLUDE: {
                        property = (localName.equals(INCLUDE_SRC_ATTRIBUTE)
                            || localName.equals(INCLUDE_RESOURCES_ATTRIBUTE)
                            || localName.equals(INCLUDE_MIME_TYPE_ATTRIBUTE)
                            || localName.equals(INCLUDE_INLINE_ATTRIBUTE));
                        break;
                    }

                    case SCRIPT: {
                        property = (localName.equals(SCRIPT_SRC_ATTRIBUTE));
                        break;
                    }

                    case REFERENCE: {
                        property = (localName.equals(REFERENCE_ID_ATTRIBUTE));
                    }
                }

                if (property) {
                    element.properties.put(localName, value);
                } else {
                    String name;
                    Class<?> propertyClass = null;

                    if (Character.isUpperCase(localName.charAt(0))) {
                        // The attribute represents a static property or listener list
                        int j = localName.indexOf('.');
                        name = localName.substring(j + 1);

                        String namespaceURI = xmlStreamReader.getAttributeNamespace(i);
                        if (namespaceURI == null) {
                            namespaceURI = xmlStreamReader.getNamespaceURI("");
                        }

                        String propertyClassName = namespaceURI + "." + localName.substring(0, j);
                        try {
                            propertyClass = Class.forName(propertyClassName);
                        } catch (ClassNotFoundException exception) {
                            throw new SerializationException(exception);
                        }
                    } else {
                        // The attribute represents an instance property
                        name = localName;
                    }

                    if (value.startsWith(NAMESPACE_BINDING_PREFIX)
                        && value.endsWith(NAMESPACE_BINDING_SUFFIX)) {
                        // The attribute represents a namespace binding
                        if (propertyClass != null) {
                            throw new SerializationException("Namespace binding is not supported for static properties.");
                        }

                        namespaceBindingAttributes.add(new Attribute(element, name, propertyClass,
                            value.substring(2, value.length() - 1)));
                    } else {
                        // Resolve the attribute value
                        Attribute attribute = new Attribute(element, name, propertyClass, value);

                        if (value.length() > 0) {
                            if (value.charAt(0) == URL_PREFIX) {
                                value = value.substring(1);

                                if (value.length() > 0) {
                                    if (value.charAt(0) == URL_PREFIX) {
                                        attribute.value = value;
                                    } else {
                                        if (location == null) {
                                            throw new IllegalStateException("Base location is undefined.");
                                        }

                                        try {
                                            attribute.value = new URL(location, value);
                                        } catch (MalformedURLException exception) {
                                            throw new SerializationException(exception);
                                        }
                                    }
                                } else {
                                    throw new SerializationException("Invalid URL resolution argument.");
                                }
                            } else if (value.charAt(0) == RESOURCE_KEY_PREFIX) {
                                value = value.substring(1);

                                if (value.length() > 0) {
                                    if (value.charAt(0) == RESOURCE_KEY_PREFIX) {
                                        attribute.value = value;
                                    } else {
                                        if (resources != null
                                            && JSON.containsKey(resources, value)) {
                                            attribute.value = JSON.get(resources, value);
                                        } else {
                                            attribute.value = value;
                                        }
                                    }
                                } else {
                                    throw new SerializationException("Invalid resource resolution argument.");
                                }
                            } else if (value.charAt(0) == OBJECT_REFERENCE_PREFIX) {
                                value = value.substring(1);

                                if (value.length() > 0) {
                                    if (value.charAt(0) == OBJECT_REFERENCE_PREFIX) {
                                        attribute.value = value;
                                    } else {
                                        if (value.equals(BXML_PREFIX + ":" + null)) {
                                            attribute.value = null;
                                        } else {
                                            if (!JSON.containsKey(namespace, value)) {
                                                throw new SerializationException("Value \"" + value + "\" is not defined.");
                                            }

                                            attribute.value = JSON.get(namespace, value);
                                        }
                                    }
                                } else {
                                    throw new SerializationException("Invalid object resolution argument.");
                                }
                            }
                        }

                        element.attributes.add(attribute);
View Full Code Here

TOP

Related Classes of org.apache.pivot.serialization.SerializationException

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.