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;
}
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;
}
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);
}
}
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;
}
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);
}
Aggregations