test.xml
<class type='main.SerializationObject'> <name>Malcolm</name> <age>45</age> </class>
reflection
src/reflection/ReflectionHelper.java
package reflection; import java.lang.reflect.Field; public class ReflectionHelper { public static Object createIntance(String className){ try { return Class.forName(className).newInstance(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; } public static void setFieldValue(Object object, String fieldName, String value){ try { Field field = object.getClass().getDeclaredField(fieldName); field.setAccessible(true); if(field.getType().equals(String.class)){ field.set(object, value); } else if (field.getType().equals(int.class)){ field.set(object, Integer.decode(value)); } field.setAccessible(false); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } }
sax
src/sax/ReadXMLFileSAX.java
package sax; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.helpers.DefaultHandler; public class ReadXMLFileSAX { public static Object readXML(String xmlFile) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); //SaxHandler handler = new SaxHandler(); SaxEmptyHandler handler = new SaxEmptyHandler(); saxParser.parse(xmlFile, handler); return handler.getObject(); } catch (Exception e) { e.printStackTrace(); } return null; } }
src/sax/SaxEmptyHandler.java
package sax; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class SaxEmptyHandler extends DefaultHandler { private static String CLASSNAME = "class"; private boolean inElement = false; public void startDocument() throws SAXException { System.out.println("Start document"); } public void endDocument() throws SAXException { System.out.println("End document "); } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println("Start element: " + qName); if(qName != CLASSNAME) inElement = true; else System.out.println("Class name: " + attributes.getValue(0)); } public void endElement(String uri, String localName, String qName) throws SAXException { System.out.println("End element: " + qName); inElement = false; } public void characters(char ch[], int start, int length) throws SAXException { if(inElement) System.out.println("Process : " + new String(ch, start, length)); } public Object getObject(){ return null; } }
src/sax/SaxHandler.java
package sax; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import reflection.ReflectionHelper; public class SaxHandler extends DefaultHandler { private static String CLASSNAME = "class"; private String element = null; private Object object = null; public void startDocument() throws SAXException { System.out.println("Start document"); } public void endDocument() throws SAXException { System.out.println("End document "); } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if(qName != CLASSNAME){ element = qName; } else{ String className = attributes.getValue(0); System.out.println("Class name: " + className); object = ReflectionHelper.createIntance(className); } } public void endElement(String uri, String localName, String qName) throws SAXException { element = null; } public void characters(char ch[], int start, int length) throws SAXException { if(element != null){ String value = new String(ch, start, length); System.out.println(element + " = " + value); ReflectionHelper.setFieldValue(object, element, value); } } public Object getObject(){ return object; } }
main
src/main/SerializationObject.java
package main; import java.io.Serializable; public class SerializationObject extends Object implements Serializable { private static final long serialVersionUID = -3895203507200457732L; private String name; private int age; private Object test = new Object(); //private ObjectFactory test2 = new ObjectFactory(); public SerializationObject(){ this.name = "Nobody"; this.age = 0; } public SerializationObject(String name, int age){ this.setAge(age); this.setName(name); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString(){ return "Name: " + name + " age: " + age; } }
src/main/ObjectFactory.java
package main; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class ObjectFactory { public static Object readObject(String path) { try{ FileInputStream fileOut = new FileInputStream(path); ObjectInputStream in = new ObjectInputStream(fileOut); Object object = in.readObject(); in.close(); return object; }catch(IOException i){ i.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; } }
src/main/Main.java
package main; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import reflection.ReflectionHelper; import sax.ReadXMLFileSAX; public class Main { public static void main(String[] args){ writeToBinFile(); //readFromBinFile(); //workWithReflection(); //workWithFactory(); //workWithSAX(); } private static void workWithSAX() { SerializationObject object = (SerializationObject) ReadXMLFileSAX.readXML("test.xml"); if(object != null) System.out.append(object.toString() + '\n'); } private static void workWithFactory() { SerializationObject object = (SerializationObject) ObjectFactory.readObject("test.bin"); System.out.append(object.toString() + '\n'); } private static void workWithReflection() { SerializationObject object = (SerializationObject)ReflectionHelper.createIntance("main.SerializationObject"); System.out.append(object.toString() + '\n'); ReflectionHelper.setFieldValue(object, "name", "Kaylee"); ReflectionHelper.setFieldValue(object, "age", "25"); System.out.append(object.toString() + '\n'); } private static void readFromBinFile() { System.out.append("Read from bin file\n"); try{ FileInputStream fileOut = new FileInputStream("test.bin"); ObjectInputStream in = new ObjectInputStream(fileOut); SerializationObject object = (SerializationObject) in.readObject(); System.out.append(object.toString() + '\n'); in.close(); }catch(IOException i){ i.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } private static void writeToBinFile() { System.out.append("Write to bin file\n"); try{ SerializationObject object = new SerializationObject("Zoe", 31); FileOutputStream fileOut = new FileOutputStream("test.bin"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(object); out.close(); }catch(IOException i){ i.printStackTrace(); } } }
--
https://github.com/vitaly-chibrikov/tp_java_2013_02/tree/master/lecture7