Package org.apache.pivot.serialization

Examples of org.apache.pivot.serialization.SerializationException


                            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(Thread.currentThread().getContextClassLoader(),
                                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;
                                sequence.add(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) {
                    System.err.println(exception);
                    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(Thread.currentThread().getContextClassLoader(),
                    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) == '/') {
                            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
                            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) {
                            exception.printStackTrace();
                        } 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


                }
            }
        }

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

        // Invoke the setter
        try {
            setterMethod.invoke(null, object, value);
        } 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

        Object object = null;

        skipWhitespaceAndComments(reader);

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

        if (c == 'n') {
            object = readNull(reader);
        } else if (c == '"' || c == '\'') {
            object = readString(reader, type);
        } else if (c == '+' || c == '-' || Character.isDigit(c)) {
            object = readNumber(reader, type);
        } else if (c == 't' || c == 'f') {
            object = readBoolean(reader, type);
        } else if (c == '[') {
            object = readList(reader, type);
        } else if (c == '{') {
            object = readMap(reader, type);
        } else {
            throw new SerializationException("Unexpected character in input stream.");
        }

        return object;
    }
View Full Code Here

                            closed = (c == '/');
                        }
                    }

                    if (!closed) {
                        throw new SerializationException("Unexpected end of input stream.");
                    }

                    if (c != -1) {
                        c = reader.read();
                    }
                } else {
                    throw new SerializationException("Unexpected character in input stream.");
                }
            }
        }
    }
View Full Code Here

        int n = nullString.length();
        int i = 0;

        while (c != -1 && i < n) {
            if (nullString.charAt(i) != c) {
                throw new SerializationException("Unexpected character in input stream.");
            }

            c = reader.read();
            i++;
        }

        if (i < n) {
            throw new SerializationException("Incomplete null value in input stream.");
        }

        return null;
    }
View Full Code Here

    }

    private Object readString(Reader reader, Type type)
        throws IOException, SerializationException {
        if (!(type instanceof Class<?>)) {
            throw new SerializationException("Cannot convert string to " + type + ".");
        }

        StringBuilder stringBuilder = new StringBuilder();

        // Use the same delimiter to close the string
        int t = c;

        // Move to the next character after the delimiter
        c = reader.read();

        while (c != -1 && c != t) {
            if (!Character.isISOControl(c)) {
                if (c == '\\') {
                    c = reader.read();

                    if (c == 'b') {
                        c = '\b';
                    } else if (c == 'f') {
                        c = '\f';
                    } else if (c == 'n') {
                        c = '\n';
                    } else if (c == 'r') {
                        c = '\r';
                    } else if (c == 't') {
                        c = '\t';
                    } else if (c == 'u') {
                        StringBuilder unicodeBuilder = new StringBuilder();
                        while (unicodeBuilder.length() < 4) {
                            c = reader.read();
                            unicodeBuilder.append((char)c);
                        }

                        String unicode = unicodeBuilder.toString();
                        c = (char)Integer.parseInt(unicode, 16);
                    } else {
                        if (!(c == '\\'
                            || c == '/'
                            || c == '\"'
                            || c == '\''
                            || c == t)) {
                            throw new SerializationException("Unsupported escape sequence in input stream.");
                        }
                    }
                }

                stringBuilder.append((char)c);
            }

            c = reader.read();
        }

        if (c != t) {
            throw new SerializationException("Unterminated string in input stream.");
        }

        // Move to the next character after the delimiter
        c = reader.read();
View Full Code Here

    }

    private Object readNumber(Reader reader, Type type)
        throws IOException, SerializationException {
        if (!(type instanceof Class<?>)) {
            throw new SerializationException("Cannot convert number to " + type + ".");
        }

        Number number = null;

        StringBuilder stringBuilder = new StringBuilder();
View Full Code Here

    }

    private Object readBoolean(Reader reader, Type type)
        throws IOException, SerializationException {
        if (!(type instanceof Class<?>)) {
            throw new SerializationException("Cannot convert number to " + type + ".");
        }

        String text = (c == 't') ? "true" : "false";
        int n = text.length();
        int i = 0;

        while (c != -1 && i < n) {
            if (text.charAt(i) != c) {
                throw new SerializationException("Unexpected character in input stream.");
            }

            c = reader.read();
            i++;
        }

        if (i < n) {
            throw new SerializationException("Incomplete boolean value in input stream.");
        }

        return BeanAdapter.coerce(Boolean.parseBoolean(text), (Class<?>)type);
    }
View Full Code Here

            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 next character after ']'
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.