⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlserialization.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
      if (array > 1) {        result.setAttribute(ATT_ARRAY, Integer.toString(array));      }      // backwards compatible: 0 -> no array ("no"), 1 -> 1-dim. array ("yes")      else {        if (!booleanToString(array == 1).equals(ATT_ARRAY_DEFAULT))          result.setAttribute(ATT_ARRAY, booleanToString(array == 1));      }            if (!booleanToString(isnull).equals(ATT_NULL_DEFAULT))        result.setAttribute(ATT_NULL, booleanToString(isnull));            return result;   }      /**    * if the class of the given object (or one of its ancestors) is stored in     * the classname override hashtable, then the override name is returned     * otherwise the classname of the given object.    *     * @param o          the object to check for overriding its classname    * @return           if overridden then the classname stored in the hashtable,    *                   otherwise the classname of the given object    * @see              #m_ClassnameOverride    */   protected String overrideClassname(Object o) {      Enumeration    enm;      String         result;      Class          currentCls;           result = o.getClass().getName();      // check overrides      enm    = m_ClassnameOverride.keys();      while (enm.hasMoreElements()) {         currentCls = (Class) enm.nextElement();         if (currentCls.isInstance(o)) {           result = (String) m_ClassnameOverride.get(currentCls);           break;         }      }            return result;   }      /**    * if the given classname is stored in the classname override hashtable,     * then the override name is returned otherwise the given classname.    * <b>Note:</b> in contrast to <code>overrideClassname(Object)</code> does    * this method only look for exact name matches. The other method checks    * whether the class of the given object is a subclass of any of the stored    * overrides.      *     * @param classname  the classname to check for overriding    * @return           if overridden then the classname stored in the hashtable,    *                   otherwise the given classname    * @see              #m_ClassnameOverride    * @see              #overrideClassname(Object)    */   protected String overrideClassname(String classname) {      Enumeration    enm;      String         result;      Class          currentCls;           result = classname;      // check overrides      enm    = m_ClassnameOverride.keys();      while (enm.hasMoreElements()) {         currentCls = (Class) enm.nextElement();         if (currentCls.getName().equals(classname)) {           result = (String) m_ClassnameOverride.get(currentCls);           break;         }      }            return result;   }      /**    * returns a property descriptor if possible, otherwise <code>null</code>    *     * @param className the name of the class to get the descriptor for    * @param displayName the name of the property    * @return the descriptor if available, otherwise <code>null</code>    */   protected PropertyDescriptor determineDescriptor(String className, String displayName) {      PropertyDescriptor      result;            result = null;            try {         result = new PropertyDescriptor(displayName, Class.forName(className));      }      catch (Exception e) {         result = null;      }            return result;   }      /**    * adds the given primitive to the DOM structure.    * @param parent the parent of this object, e.g. the class this object is a member of    * @param o the primitive to describe in XML    * @param name the name of the primitive    * @return the node that was created    * @throws Exception if the DOM creation fails    */   protected Element writeBooleanToXML(Element parent, boolean o, String name) throws Exception {     Element      node;     // for debugging only     if (DEBUG)        trace(new Throwable(), name);          m_CurrentNode = parent;          node = addElement(parent, name, Boolean.TYPE.getName(), true);     node.appendChild(node.getOwnerDocument().createTextNode(new Boolean(o).toString()));          return node;   }      /**    * adds the given primitive to the DOM structure.    * @param parent the parent of this object, e.g. the class this object is a member of    * @param o the primitive to describe in XML    * @param name the name of the primitive    * @return the node that was created    * @throws Exception if the DOM creation fails    */   protected Element writeByteToXML(Element parent, byte o, String name) throws Exception {     Element      node;          // for debugging only     if (DEBUG)        trace(new Throwable(), name);          m_CurrentNode = parent;          node = addElement(parent, name, Byte.TYPE.getName(), true);     node.appendChild(node.getOwnerDocument().createTextNode(new Byte(o).toString()));          return node;   }      /**    * adds the given primitive to the DOM structure.    * @param parent the parent of this object, e.g. the class this object is a member of    * @param o the primitive to describe in XML    * @param name the name of the primitive    * @return the node that was created    * @throws Exception if the DOM creation fails    */   protected Element writeCharToXML(Element parent, char o, String name) throws Exception {     Element      node;          // for debugging only     if (DEBUG)        trace(new Throwable(), name);          m_CurrentNode = parent;          node = addElement(parent, name, Character.TYPE.getName(), true);     node.appendChild(node.getOwnerDocument().createTextNode(new Character(o).toString()));          return node;   }      /**    * adds the given primitive to the DOM structure.    * @param parent the parent of this object, e.g. the class this object is a member of    * @param o the primitive to describe in XML    * @param name the name of the primitive    * @return the node that was created    * @throws Exception if the DOM creation fails    */   protected Element writeDoubleToXML(Element parent, double o, String name) throws Exception {     Element      node;          // for debugging only     if (DEBUG)        trace(new Throwable(), name);          m_CurrentNode = parent;          node = addElement(parent, name, Double.TYPE.getName(), true);     node.appendChild(node.getOwnerDocument().createTextNode(new Double(o).toString()));          return node;   }      /**    * adds the given primitive to the DOM structure.    * @param parent the parent of this object, e.g. the class this object is a member of    * @param o the primitive to describe in XML    * @param name the name of the primitive    * @return the node that was created    * @throws Exception if the DOM creation fails    */   protected Element writeFloatToXML(Element parent, float o, String name) throws Exception {     Element      node;          // for debugging only     if (DEBUG)        trace(new Throwable(), name);          m_CurrentNode = parent;          node = addElement(parent, name, Float.TYPE.getName(), true);     node.appendChild(node.getOwnerDocument().createTextNode(new Float(o).toString()));          return node;   }      /**    * adds the given primitive to the DOM structure.    * @param parent the parent of this object, e.g. the class this object is a member of    * @param o the primitive to describe in XML    * @param name the name of the primitive    * @return the node that was created    * @throws Exception if the DOM creation fails    */   protected Element writeIntToXML(Element parent, int o, String name) throws Exception {     Element      node;          // for debugging only     if (DEBUG)        trace(new Throwable(), name);          m_CurrentNode = parent;          node = addElement(parent, name, Integer.TYPE.getName(), true);     node.appendChild(node.getOwnerDocument().createTextNode(new Integer(o).toString()));          return node;   }      /**    * adds the given primitive to the DOM structure.    * @param parent the parent of this object, e.g. the class this object is a member of    * @param o the primitive to describe in XML    * @param name the name of the primitive    * @return the node that was created    * @throws Exception if the DOM creation fails    */   protected Element writeLongToXML(Element parent, long o, String name) throws Exception {     Element      node;          // for debugging only     if (DEBUG)        trace(new Throwable(), name);          m_CurrentNode = parent;     node = addElement(parent, name, Long.TYPE.getName(), true);     node.appendChild(node.getOwnerDocument().createTextNode(new Long(o).toString()));          return node;   }      /**    * adds the given primitive to the DOM structure.    * @param parent the parent of this object, e.g. the class this object is a member of    * @param o the primitive to describe in XML    * @param name the name of the primitive    * @return the node that was created    * @throws Exception if the DOM creation fails    */   protected Element writeShortToXML(Element parent, short o, String name) throws Exception {     Element      node;          // for debugging only     if (DEBUG)        trace(new Throwable(), name);          m_CurrentNode = parent;          node = addElement(parent, name, Short.TYPE.getName(), true);     node.appendChild(node.getOwnerDocument().createTextNode(new Short(o).toString()));          return node;   }      /**    * checks whether the innermost class is a primitive class (handles     * multi-dimensional arrays)    * @param c        the array class to inspect    * @return         whether the array consists of primitive elements    */   protected boolean isPrimitiveArray(Class c) {     if (c.getComponentType().isArray())       return isPrimitiveArray(c.getComponentType());    else       return c.getComponentType().isPrimitive();   }      /**    * adds the given Object to a DOM structure.     * (only public due to reflection).<br>    * <b>Note:</b> <code>overrideClassname(Object)</code> is not invoked in case of    * arrays, since the array class could be a superclass, whereas the elements of    * the array can be specialized subclasses. In case of an array the method     * <code>overrideClassname(String)</code> is invoked, which searches for an     * exact match of the classname in the override hashtable.    *     * @param parent the parent of this object, e.g. the class this object is a member of    * @param o the Object to describe in XML    * @param name the name of the object    * @return the node that was created    * @throws Exception if the DOM creation fails    * @see #overrideClassname(Object)    * @see #overrideClassname(String)    * @see #m_ClassnameOverride    */   public Element writeToXML(Element parent, Object o, String name) throws Exception {      String               classname;      Element              node;      Hashtable            memberlist;      Enumeration          enm;      Object               member;      String               memberName;      Method               method;      PropertyDescriptor   desc;      boolean              primitive;      int                  array;      int                  i;      Object               obj;      String               tmpStr;      node = null;            // for debugging only      if (DEBUG)         trace(new Throwable(), name);      // special handling of null-objects      if (o == null) {        node = addElement(parent, name, "" + null, false, 0, true);        return node;      }            // used for overriding the classname      obj = null;            // get information about object      array = 0;      if (o.getClass().isArray())        array = Utils.getArrayDimensions(o);      if (array > 0) {        classname = Utils.getArrayClass(o.getClass()).getName();        primitive = isPrimitiveArray(o.getClass());       }      else {         // try to get property descriptor to determine real class         // (for primitives the getClass() method returns the corresponding Object-Class!)         desc = null;         if (parent != null)            desc = determineDescriptor(parent.getAttribute(ATT_CLASS), name);                  if (desc != null)            primitive = desc.getPropertyType().isPrimitive();          else            primitive = o.getClass().isPrimitive();          // for primitives: retrieve primitive type, otherwise the object's real          // class. For non-primitives we can't use the descriptor, since that         // might only return an interface as class!         if (primitive) {            classname = desc.getPropertyType().getName();         }         else {            obj       = o;            classname = o.getClass().getName();         }      }            // fix class/primitive if parent is array of primitives, thanks to       // reflection the elements of the array are objects and not primitives!      if (    (parent != null)            && (!parent.getAttribute(ATT_ARRAY).equals(""))           && (!parent.getAttribute(ATT_ARRAY).equals(VAL_NO))           && (stringToBoolean(parent.getAttribute(ATT_PRIMITIVE))) ) {         primitive = true;         classname = parent.getAttribute(ATT_CLASS);         obj       = null;      }      // perhaps we need to override the classname      if (obj != null)        classname = overrideClassname(obj);         // for non-arrays      else        classname = overrideClassname(classname);   // for arrays            // create node for current object      node = addElement(parent, name, classname, primitive, array);            // array? -> save as child with 'name="<index>"'      if (array > 0) {         for (i = 0; i < Array.getLength(o); i++) {            invokeWriteToXML(node, Array.get(o, i), Integer.toString(i));         }      }      // non-array      else {         // primitive? -> only toString()         if (primitive) {            node.appendChild(node.getOwnerDocument().createTextNode(o.toString()));         }         // object         else {            // process recursively members of this object             memberlist = getDescriptors(o);            // if no get/set methods -> we assume it has String-Constructor            if (memberlist.size() == 0) {              if (!o.toString().equals("")) {        	tmpStr = o.toString();        	// these five entities are recognized by every XML processor        	// see http://www.xml.com/pub/a/2001/03/14/trxml10.html        	tmpStr = tmpStr.replaceAll("&", "&amp;")        	               .replaceAll("\"", "&quot;")        	               .replaceAll("'", "&apos;")

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -