Package org.apache.pivot.serialization

Examples of org.apache.pivot.serialization.SerializationException


                            break;
                        }
                    }
                }
            } catch (XMLStreamException exception) {
                throw new SerializationException(exception);
            }
        } catch (IOException exception) {
            logException(exception);
            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() + ": \"" + text + "\"", 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, true, classLoader);
                    } catch (Throwable 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, true, classLoader);
                        value = newTypedObject(type);
                    } catch (Throwable 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);
            if (src.charAt(0) == OBJECT_REFERENCE_PREFIX) {
                src = src.substring(1);
                if (src.length() > 0) {
                    if (!JSON.containsKey(namespace, src)) {
                        throw new SerializationException("Value \"" + src + "\" is not defined.");
                    }
                    String variableValue = JSON.get(namespace, src);
                    src = variableValue;
                }
            }

            Resources resourcesLocal = this.resources;
            if (element.properties.containsKey(INCLUDE_RESOURCES_ATTRIBUTE)) {
                resourcesLocal = new Resources(resourcesLocal,
                    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 locationLocal;
            if (src.charAt(0) == '/') {
                locationLocal = classLoader.getResource(src.substring(1));
            } else {
                locationLocal = new URL(this.location, src);
            }

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

                resolvable.setLocation(locationLocal);
                resolvable.setResources(resourcesLocal);
            }

            // Read the object
            InputStream inputStream = new BufferedInputStream(locationLocal.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));
                        break;
                    }

                    default: {
                        break;
                    }
                }

                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.isEmpty()) {
                            namespaceURI = xmlStreamReader.getNamespaceURI("");
                        }

                        String propertyClassName = namespaceURI + "." + localName.substring(0, j);
                        try {
                            propertyClass = Class.forName(propertyClassName, true, classLoader);
                        } catch (Throwable 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

                            Method getListenerListMethod;
                            try {
                                Class<?> type = element.value.getClass();
                                getListenerListMethod = type.getMethod(getListenerListMethodName);
                            } catch (NoSuchMethodException exception) {
                                throw new SerializationException(exception);
                            }

                            Object listenerList;
                            try {
                                listenerList = getListenerListMethod.invoke(element.value);
                            } catch (InvocationTargetException exception) {
                                throw new SerializationException(exception);
                            } catch (IllegalAccessException exception) {
                                throw new SerializationException(exception);
                            }

                            // Create an invocation handler for this listener
                            ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(language);
                            AttributeInvocationHandler handler =
                                new AttributeInvocationHandler(scriptEngine, attribute.name,
                                    (String)attribute.value);

                            Object listener = Proxy.newProxyInstance(classLoader,
                                new Class<?>[]{attribute.propertyClass}, handler);

                            // Add the listener
                            Class<?> listenerListClass = listenerList.getClass();
                            Method addMethod;
                            try {
                                addMethod = listenerListClass.getMethod("add", Object.class);
                            } catch (NoSuchMethodException exception) {
                                throw new RuntimeException(exception);
                            }

                            try {
                                addMethod.invoke(listenerList, listener);
                            } catch (IllegalAccessException exception) {
                                throw new SerializationException(exception);
                            } catch (InvocationTargetException exception) {
                                throw new SerializationException(exception);
                            }
                        } else {
                            // The attribute represents a static setter
                            setStaticProperty(element.value, attribute.propertyClass,
                                attribute.name, attribute.value);
                        }
                    }
                }

                if (element.parent != null) {
                    if (element.parent.type == Element.Type.WRITABLE_PROPERTY) {
                        // Set this as the property value; it will be applied later in the
                        // parent's closing tag
                        element.parent.value = element.value;
                    } else if (element.parent.value != null) {
                        // If the parent element has a default property, use it; otherwise, if the
                        // parent is a sequence, add the element to it
                        Class<?> parentType = element.parent.value.getClass();
                        DefaultProperty defaultProperty = parentType.getAnnotation(DefaultProperty.class);

                        if (defaultProperty == null) {
                            if (element.parent.value instanceof Sequence<?>) {
                                Sequence<Object> sequence = (Sequence<Object>)element.parent.value;
                                sequence.add(element.value);
                            } else {
                                throw new SerializationException(element.parent.value.getClass()
                                    + " is not a sequence.");
                            }
                        } else {
                            String defaultPropertyName = defaultProperty.value();
                            BeanAdapter beanAdapter = new BeanAdapter(element.parent.value);
                            Object defaultPropertyValue = beanAdapter.get(defaultPropertyName);

                            if (defaultPropertyValue instanceof Sequence<?>) {
                                Sequence<Object> sequence = (Sequence<Object>)defaultPropertyValue;
                                try {
                                    sequence.add(element.value);
                                } catch (UnsupportedOperationException uoe) {
                                    beanAdapter.put(defaultPropertyName, element.value);
                                }
                            } else {
                                beanAdapter.put(defaultPropertyName, element.value);
                            }
                        }
                    }
                }

                break;
            }

            case READ_ONLY_PROPERTY: {
                Dictionary<String, Object> dictionary;
                if (element.value instanceof Dictionary<?, ?>) {
                    dictionary = (Dictionary<String, Object>)element.value;
                } else {
                    dictionary = new BeanAdapter(element.value);
                }

                // Process attributes looking for instance property setters
                for (Attribute attribute : element.attributes) {
                    if (attribute.propertyClass != null) {
                        throw new SerializationException("Static setters are not supported"
                            + " for read-only properties.");
                    }

                    dictionary.put(attribute.name, attribute.value);
                }

                break;
            }

            case WRITABLE_PROPERTY: {
                if (element.propertyClass == null) {
                    Dictionary<String, Object> dictionary;
                    if (element.parent.value instanceof Dictionary) {
                        dictionary = (Dictionary<String, Object>)element.parent.value;
                    } else {
                        dictionary = new BeanAdapter(element.parent.value);
                    }

                    dictionary.put(element.name, element.value);
                } else {
                    if (element.parent == null) {
                        throw new SerializationException("Element does not have a parent.");
                    }

                    if (element.parent.value == null) {
                        throw new SerializationException("Parent value is null.");
                    }

                    setStaticProperty(element.parent.value, element.propertyClass,
                        element.name, element.value);
                }

                break;
            }

            case LISTENER_LIST_PROPERTY: {
                // Evaluate the script
                String script = (String)element.value;
                ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(language);
                if (scriptEngine == null) {
                    throw new SerializationException("Script engine for \"" + language + "\" not found.");
                }

                // Don't pollute the engine namespace with the listener functions
                scriptEngine.setBindings(new SimpleBindings(), ScriptContext.ENGINE_SCOPE);

                try {
                    scriptEngine.eval(script);
                } catch (ScriptException exception) {
                    reportException(exception, script);
                    break;
                }

                // Create the listener and add it to the list
                BeanAdapter beanAdapter = new BeanAdapter(element.parent.value);
                ListenerList<?> listenerList = (ListenerList<?>)beanAdapter.get(element.name);
                Class<?> listenerListClass = listenerList.getClass();

                java.lang.reflect.Type[] genericInterfaces = listenerListClass.getGenericInterfaces();
                Class<?> listenerClass = (Class<?>)genericInterfaces[0];

                ElementInvocationHandler handler = new ElementInvocationHandler(scriptEngine);

                Method addMethod;
                try {
                    addMethod = listenerListClass.getMethod("add", Object.class);
                } catch (NoSuchMethodException exception) {
                    throw new RuntimeException(exception);
                }

                Object listener = Proxy.newProxyInstance(classLoader,
                    new Class<?>[]{listenerClass}, handler);

                try {
                    addMethod.invoke(listenerList, listener);
                } catch (IllegalAccessException exception) {
                    throw new SerializationException(exception);
                } catch (InvocationTargetException exception) {
                    throw new SerializationException(exception);
                }

                break;
            }

            case SCRIPT: {
                String src = null;
                if (element.properties.containsKey(INCLUDE_SRC_ATTRIBUTE)) {
                    src = element.properties.get(INCLUDE_SRC_ATTRIBUTE);
                }

                if (src != null) {
                    int i = src.lastIndexOf(".");
                    if (i == -1) {
                        throw new SerializationException("Cannot determine type of script \""
                            + src + "\".");
                    }

                    String extension = src.substring(i + 1);
                    ScriptEngine scriptEngine = scriptEngineManager.getEngineByExtension(extension);

                    if (scriptEngine == null) {
                        throw new SerializationException("Unable to find scripting engine for"
                            + " extension " + extension + ".");
                    }

                    scriptEngine.setBindings(scriptEngineManager.getBindings(), ScriptContext.ENGINE_SCOPE);

                    try {
                        URL scriptLocation;
                        if (src.charAt(0) == '/') {
                            scriptLocation = classLoader.getResource(src.substring(1));
                        } else {
                            scriptLocation = new URL(location, src);
                        }

                        BufferedReader scriptReader = null;
                        try {
                            scriptReader = new BufferedReader(new InputStreamReader(scriptLocation.openStream()));
                            scriptEngine.eval(scriptReader);
                        } catch(ScriptException exception) {
                            reportException(exception);
                        } finally {
                            if (scriptReader != null) {
                                scriptReader.close();
                            }
                        }
                    } catch (IOException exception) {
                        throw new SerializationException(exception);
                    }
                }

                if (element.value != null) {
                    // Evaluate the script
                    String script = (String)element.value;
                    ScriptEngine scriptEngine = scriptEngineManager.getEngineByName(language);

                    if (scriptEngine == null) {
                        throw new SerializationException("Unable to find scripting engine for"
                            + " language \"" + language + "\".");
                    }

                    scriptEngine.setBindings(scriptEngineManager.getBindings(), ScriptContext.ENGINE_SCOPE);
View Full Code Here

            message += " in file " + location.getPath();
        }

        message += ":";

        reportException(new SerializationException(message, exception));
    }
View Full Code Here

        reportException(new SerializationException(message, exception));
    }

    private void reportException(ScriptException exception, String script) {
        reportException(new SerializationException("Failed to execute script:\n"+script, exception));
    }
View Full Code Here

                }
            }
        }

        if (setterMethod == null) {
            throw new SerializationException(propertyClass.getName()
                + "." + propertyNameUpdated
                + " is not valid static property."
            );
        }

        // Invoke the setter
        try {
            setterMethod.invoke(null, object, valueToAssign);
        } catch (Exception exception) {
            throw new SerializationException(exception);
        }
    }
View Full Code Here

            if (bindings.containsKey(methodName)) {
                Invocable invocable;
                try {
                    invocable = (Invocable)scriptEngine;
                } catch (ClassCastException exception) {
                    throw new SerializationException(exception);
                }

                result = invocable.invokeFunction(methodName, args);
            }
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.