📄 xmlserialization.java
字号:
.replaceAll("<", "<") .replaceAll(">", ">"); // in addition, replace some other entities as well tmpStr = tmpStr.replaceAll("\n", " ") .replaceAll("\r", " ") .replaceAll("\t", "	"); node.appendChild(node.getOwnerDocument().createTextNode(tmpStr)); } } else { enm = memberlist.keys(); while (enm.hasMoreElements()) { memberName = enm.nextElement().toString(); // in ignore list? if ( (m_Properties.isIgnored(memberName)) || (m_Properties.isIgnored(getPath(node) + "." + memberName)) || (m_Properties.isIgnored(o, getPath(node) + "." + memberName)) ) continue; // is it allowed? if (!m_Properties.isAllowed(o, memberName)) continue; desc = (PropertyDescriptor) memberlist.get(memberName); method = desc.getReadMethod(); member = method.invoke(o, (Object[]) null); invokeWriteToXML(node, member, memberName); } } } } return node; } /** * either invokes a custom method to write a specific property/class or the standard * method <code>writeToXML(Element,Object,String)</code> * * @param parent the parent XML node * @param o the object's content will be added as children to the given parent node * @param name the name of the object * @return the node that was created * @throws Exception if invocation or turning into XML fails */ protected Element invokeWriteToXML(Element parent, Object o, String name) throws Exception { Method method; Class[] methodClasses; Object[] methodArgs; boolean array; Element node; boolean useDefault; node = null; method = null; useDefault = false; m_CurrentNode = parent; // default, if null if (o == null) useDefault = true; try { if (!useDefault) { array = o.getClass().isArray(); // display name? if (m_CustomMethods.write().contains(name)) method = (Method) m_CustomMethods.write().get(o.getClass()); else // class? if ( (!array) && (m_CustomMethods.write().contains(o.getClass())) ) method = (Method) m_CustomMethods.write().get(o.getClass()); else method = null; useDefault = (method == null); } // custom if (!useDefault) { methodClasses = new Class[3]; methodClasses[0] = Element.class; methodClasses[1] = Object.class; methodClasses[2] = String.class; methodArgs = new Object[3]; methodArgs[0] = parent; methodArgs[1] = o; methodArgs[2] = name; node = (Element) method.invoke(this, methodArgs); } // standard else { node = writeToXML(parent, o, name); } } catch (Exception e) { if (DEBUG) e.printStackTrace(); if (m_CurrentNode != null) { System.out.println("Happened near: " + getPath(m_CurrentNode)); // print it only once! m_CurrentNode = null; } System.out.println("PROBLEM (write): " + name); throw (Exception) e.fillInStackTrace(); } return node; } /** * enables derived classes to due some pre-processing on the objects, that's * about to be serialized. Right now it only returns the object. * * @param o the object that is serialized into XML * @return the possibly altered object * @throws Exception if post-processing fails */ protected Object writePreProcess(Object o) throws Exception { return o; } /** * enables derived classes to add other properties to the DOM tree, e.g. * ones that do not apply to the get/set convention of beans. only implemented * with empty method body. * * @param o the object that is serialized into XML * @throws Exception if post-processing fails */ protected void writePostProcess(Object o) throws Exception { } /** * extracts all accesible properties from the given object * * @param o the object to turn into an XML representation * @return the generated DOM document * @throws Exception if XML generation fails */ public XMLDocument toXML(Object o) throws Exception { clear(); invokeWriteToXML(null, writePreProcess(o), VAL_ROOT); writePostProcess(o); return m_Document; } /** * returns a descriptor for a given objet by providing the name * * @param o the object the get the descriptor for * @param name the display name of the descriptor * @return the Descriptor, if found, otherwise <code>null</code> * @throws Exception if introsepction fails */ protected PropertyDescriptor getDescriptorByName(Object o, String name) throws Exception { PropertyDescriptor result; PropertyDescriptor[] desc; int i; result = null; desc = Introspector.getBeanInfo(o.getClass()).getPropertyDescriptors(); for (i = 0; i < desc.length; i++) { if (desc[i].getDisplayName().equals(name)) { result = desc[i]; break; } } return result; } /** * returns the associated class for the given name * * @param name the name of the class to return a Class object for * @return the class if it could be retrieved * @throws Exception if it class retrieval fails */ protected Class determineClass(String name) throws Exception { Class result; if (name.equals(Boolean.TYPE.getName())) result = Boolean.TYPE; else if (name.equals(Byte.TYPE.getName())) result = Byte.TYPE; else if (name.equals(Character.TYPE.getName())) result = Character.TYPE; else if (name.equals(Double.TYPE.getName())) result = Double.TYPE; else if (name.equals(Float.TYPE.getName())) result = Float.TYPE; else if (name.equals(Integer.TYPE.getName())) result = Integer.TYPE; else if (name.equals(Long.TYPE.getName())) result = Long.TYPE; else if (name.equals(Short.TYPE.getName())) result = Short.TYPE; else result = Class.forName(name); return result; } /** * returns an Object representing the primitive described by the given node. * Here we use a trick to return an object even though its a primitive: by * creating a primitive array with reflection of length 1, setting the * primtive value as real object and then returning the "object" at * position 1 of the array. * * @param node the node to return the value as "primitive" object * @return the primitive as "pseudo" object * @throws Exception if the instantiation of the array fails or any of the * String conversions fails */ protected Object getPrimitive(Element node) throws Exception { Object result; Object tmpResult; Class cls; cls = determineClass(node.getAttribute(ATT_CLASS)); tmpResult = Array.newInstance(cls, 1); if (cls == Boolean.TYPE) Array.set(tmpResult, 0, new Boolean(XMLDocument.getContent(node))); else if (cls == Byte.TYPE) Array.set(tmpResult, 0, new Byte(XMLDocument.getContent(node))); else if (cls == Character.TYPE) Array.set(tmpResult, 0, new Character(XMLDocument.getContent(node).charAt(0))); else if (cls == Double.TYPE) Array.set(tmpResult, 0, new Double(XMLDocument.getContent(node))); else if (cls == Float.TYPE) Array.set(tmpResult, 0, new Float(XMLDocument.getContent(node))); else if (cls == Integer.TYPE) Array.set(tmpResult, 0, new Integer(XMLDocument.getContent(node))); else if (cls == Long.TYPE) Array.set(tmpResult, 0, new Long(XMLDocument.getContent(node))); else if (cls == Short.TYPE) Array.set(tmpResult, 0, new Short(XMLDocument.getContent(node))); else throw new Exception("Cannot get primitive for class '" + cls.getName() + "'!"); result = Array.get(tmpResult, 0); return result; } /** * builds the primitive from the given DOM node. * * @param node the associated XML node * @return the primitive created from the XML description * @throws Exception if instantiation fails */ public boolean readBooleanFromXML(Element node) throws Exception { // for debugging only if (DEBUG) trace(new Throwable(), node.getAttribute(ATT_NAME)); m_CurrentNode = node; return ((Boolean) getPrimitive(node)).booleanValue(); } /** * builds the primitive from the given DOM node. * * @param node the associated XML node * @return the primitive created from the XML description * @throws Exception if instantiation fails */ public byte readByteFromXML(Element node) throws Exception { // for debugging only if (DEBUG) trace(new Throwable(), node.getAttribute(ATT_NAME)); m_CurrentNode = node; return ((Byte) getPrimitive(node)).byteValue(); } /** * builds the primitive from the given DOM node. * * @param node the associated XML node * @return the primitive created from the XML description * @throws Exception if instantiation fails */ public char readCharFromXML(Element node) throws Exception { // for debugging only if (DEBUG) trace(new Throwable(), node.getAttribute(ATT_NAME)); m_CurrentNode = node; return ((Character) getPrimitive(node)).charValue(); } /** * builds the primitive from the given DOM node. * * @param node the associated XML node * @return the primitive created from the XML description * @throws Exception if instantiation fails */ public double readDoubleFromXML(Element node) throws Exception { // for debugging only if (DEBUG) trace(new Throwable(), node.getAttribute(ATT_NAME)); m_CurrentNode = node; return ((Double) getPrimitive(node)).doubleValue(); } /** * builds the primitive from the given DOM node. * * @param node the associated XML node * @return the primitive created from the XML description * @throws Exception if instantiation fails */ public float readFloatFromXML(Element node) throws Exception { // for debugging only if (DEBUG) trace(new Throwable(), node.getAttribute(ATT_NAME)); m_CurrentNode = node; return ((Float) getPrimitive(node)).floatValue(); } /** * builds the primitive from the given DOM node. * * @param node the associated XML node * @return the primitive created from the XML description * @throws Exception if instantiation fails */ public int readIntFromXML(Element node) throws Exception { // for debugging only if (DEBUG) trace(new Throwable(), node.getAttribute(ATT_NAME)); m_CurrentNode = node; return ((Integer) getPrimitive(node)).intValue(); } /** * builds the primitive from the given DOM node. * * @param node the associated XML node * @return the primitive created from the XML description * @throws Exception if instantiation fails */ public long readLongFromXML(Element node) throws Exception { // for debugging only if (DEBUG) trace(new Throwable(), node.getAttribute(ATT_NAME)); m_CurrentNode = node; return ((Long) getPrimitive(node)).longValue(); } /** * builds the primitive from the given DOM node. * * @param node the associated XML node * @return the primitive created from the XML description * @throws Exception if instantiation fails */ public short readShortFromXML(Element node) throws Exception { // for debugging only if (DEBUG) trace(new Throwable(), node.getAttribute(ATT_NAME)); m_CurrentNode = node; return ((Short) getPrimitive(node)).shortValue(); } /** * adds the specific node to the object via a set method * * @param o the object to set a property * @param name the name of the object for which to set a property * (only for information reasons) * @param child the value of the property to add * @return the provided object, but augmented by the child * @throws Exception if something goes wrong */ public Object readFromXML(Object o, String name, Element child) throws Exception { Object result; Hashtable descriptors; PropertyDescriptor descriptor; String methodName; Method method; Object[] methodArgs; Object tmpResult; Class paramClass;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -