use of org.apache.pivot.serialization.SerializationException in project pivot by apache.
the class JSONSerializer method readListValue.
@SuppressWarnings("unchecked")
private Object readListValue(Reader reader, Type typeArgument, String key) throws IOException, SerializationException {
Sequence<Object> sequence = null;
Type itemType = null;
if (typeArgument == Object.class) {
// Return the default sequence and item types
sequence = new ArrayList<>();
itemType = Object.class;
} else {
// Determine the item type from generic parameters
Type parentType = typeArgument;
while (parentType != null) {
if (parentType instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) parentType;
Class<?> rawType = (Class<?>) parameterizedType.getRawType();
if (Sequence.class.isAssignableFrom(rawType)) {
itemType = parameterizedType.getActualTypeArguments()[0];
}
break;
}
Class<?> classType = (Class<?>) parentType;
Type[] genericInterfaces = classType.getGenericInterfaces();
for (int i = 0; i < genericInterfaces.length; i++) {
Type genericInterface = genericInterfaces[i];
if (genericInterface instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) genericInterface;
Class<?> interfaceType = (Class<?>) parameterizedType.getRawType();
if (Sequence.class.isAssignableFrom(interfaceType)) {
itemType = parameterizedType.getActualTypeArguments()[0];
if (itemType instanceof TypeVariable<?>) {
itemType = Object.class;
}
break;
}
}
}
if (itemType != null) {
break;
}
parentType = classType.getGenericSuperclass();
}
if (itemType == null) {
throw new SerializationException("Could not determine sequence item type.");
}
// Instantiate the sequence type
Class<?> sequenceType;
if (typeArgument instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) typeArgument;
sequenceType = (Class<?>) parameterizedType.getRawType();
} else {
sequenceType = (Class<?>) typeArgument;
}
try {
sequence = (Sequence<Object>) sequenceType.newInstance();
} catch (InstantiationException exception) {
throw new RuntimeException(exception);
} catch (IllegalAccessException exception) {
throw new RuntimeException(exception);
}
}
// Notify the listeners
if (jsonSerializerListeners != null) {
jsonSerializerListeners.beginSequence(this, sequence);
}
// Move to the next character after '['
c = reader.read();
skipWhitespaceAndComments(reader);
while (c != -1 && c != ']') {
sequence.add(readValue(reader, itemType, key));
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: '" + (char) c + "'");
}
}
}
// Move to the next character after ']'
c = reader.read();
// Notify the listeners
if (jsonSerializerListeners != null) {
jsonSerializerListeners.endSequence(this);
}
return sequence;
}
use of org.apache.pivot.serialization.SerializationException in project pivot by apache.
the class JSONSerializer method readString.
private String readString(Reader reader) throws IOException, SerializationException {
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) {
// so silently ignore them
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();
return stringBuilder.toString();
}
use of org.apache.pivot.serialization.SerializationException in project pivot by apache.
the class BXMLSerializer method setStaticProperty.
private static void setStaticProperty(Object object, Class<?> propertyClass, final String propertyName, final Object value) throws SerializationException {
Class<?> objectType = object.getClass();
String propertyNameUpdated = Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
Object valueToAssign = value;
Method setterMethod = null;
if (valueToAssign != null) {
setterMethod = getStaticSetterMethod(propertyClass, propertyNameUpdated, objectType, valueToAssign.getClass());
}
if (setterMethod == null) {
Method getterMethod = getStaticGetterMethod(propertyClass, propertyNameUpdated, objectType);
if (getterMethod != null) {
Class<?> propertyType = getterMethod.getReturnType();
setterMethod = getStaticSetterMethod(propertyClass, propertyNameUpdated, objectType, propertyType);
if (valueToAssign instanceof String) {
valueToAssign = BeanAdapter.coerce((String) valueToAssign, propertyType, propertyNameUpdated);
}
}
}
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);
}
}
use of org.apache.pivot.serialization.SerializationException in project pivot by apache.
the class BXMLSerializer method getEngineByExtension.
/**
* Get a script engine instance for the given (file) extension.
* <p> Two things happen for a new script engine: set the global bindings to our
* {@link #namespace} so that any existing global definitions get set, and the
* {@link #NASHORN_COMPAT_SCRIPT} is run to ensure compatibility with the "Rhino"
* script engine (pre-Java-8).
* <p> Note: an engine found by this method will also be added to the {@link #scriptEngines}
* map indexed by all its supported language names.
*
* @param extension Any script language extension supported by the current JVM.
* @return Either an existing engine for that extension, or a new one found by the
* {@link #scriptEngineManager} and then cached (in the {@link #scriptEnginesExts} map).
*/
private ScriptEngine getEngineByExtension(String extension) throws SerializationException {
String extensionKey = extension.toLowerCase();
ScriptEngine engine = scriptEnginesExts.get(extensionKey);
if (engine != null) {
return engine;
}
engine = scriptEngineManager.getEngineByExtension(extension);
if (engine == null) {
throw new SerializationException("Unable to find scripting engine for" + " extension " + extension + ".");
}
// NOTE: this might not be right for Rhino engine, but works for Nashorn
engine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
if (engine.getFactory().getNames().contains("javascript")) {
try {
engine.eval(NASHORN_COMPAT_SCRIPT);
} catch (ScriptException se) {
throw new SerializationException("Unable to execute Nashorn compatibility script:", se);
}
}
scriptEnginesExts.put(extensionKey, engine);
// Also put this engine into the "languages" map by the language(s) it supports
for (String language : engine.getFactory().getNames()) {
String languageKey = language.toLowerCase();
if (!scriptEngines.containsKey(languageKey)) {
scriptEngines.put(languageKey, engine);
}
}
return engine;
}
use of org.apache.pivot.serialization.SerializationException in project pivot by apache.
the class BXMLSerializer method newEngineByName.
private ScriptEngine newEngineByName(String scriptLanguage) throws SerializationException {
ScriptEngine engine = scriptEngineManager.getEngineByName(scriptLanguage);
if (engine == null) {
throw new SerializationException("Unable to find scripting engine for" + " language \"" + scriptLanguage + "\".");
}
// NOTE: this might not be right for Rhino engine, but works for Nashorn
engine.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
if (engine.getFactory().getNames().contains("javascript")) {
try {
engine.eval(NASHORN_COMPAT_SCRIPT);
} catch (ScriptException se) {
throw new SerializationException("Unable to execute Nashorn compatibility script:", se);
}
}
return engine;
}
Aggregations