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

📄 marshaller.java

📁 OR Mapping工具
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
					// if no dateFormatStrings 					// assume number of seconds in date and fall below...					setDate (obj, m, attValue, context);				               	} else { // int or bool etc --                	//Class nativeClass = getAClass (paramClassType);	                Object forCons = attValue;	                Class initClass = null;	                if (jf.isDate ()) {	                    forCons = new Long(attValue);	                    initClass = java.lang.Long.TYPE;	                } else	                    initClass = string;	                Class primClass = getAClass (this.getFieldType(paramClassType));					/*System.out.println ("Getting constructor for " + initClass.getName ()										+ " with args " + forCons +"  primClass="+primClass.getName ()										+ " isDate="+jf.isDate()); */	                Object primObj = primClass.getDeclaredConstructor(new Class[]{initClass}).newInstance(new Object[]{forCons});	                m.invoke(obj, new Object[]{primObj});            	}			}        }    }	protected void setDate (Object o, Method m, String value, UnmarshalContext context) throws Exception {		Timestamp result = getDate (value, context);		m.invoke (o, new Object[]{result});	}	// apply date formats and try to get	protected Timestamp getDate (String value, UnmarshalContext context) {		SimpleDateFormat sdf = context.getDateFormat ();		Timestamp result = null;		if (dateFormatStrings != null) {			for (int i=0; i < dateFormatStrings.length && result == null; i++) {				if (dateFormatStrings[i].length () == value.length ()) {					sdf.applyPattern (dateFormatStrings[i]);					try {						result = new Timestamp (sdf.parse(value).getTime ());					} catch (Exception e) { } // nothing for now ..				}			}		}		if (result == null) { // still nothing try integer			result = new Timestamp (Long.parseLong (value));		}		return result;	}	/** helper method to get a java.lang.Class object given a type (including primitives	 * like int.class)	 */    protected Class getAClass (String className) throws ClassNotFoundException {        int dot_index = className.lastIndexOf(".");        if (dot_index == -1) { // native class or string            if (className.equals ("String"))                return String.class;            else if (className.equals ("int"))                return int.class;			else if (className.equals ("long"))                return long.class;            else if (className.equals ("boolean"))                return boolean.class;            else if (className.equals ("char"))				return char.class;            else if (className.equals ("double"))                return double.class;            else if (className.equals ("Date") || className.equals ("Timestamp"))                return Class.forName("java.sql.Timestamp");            else // try native classes                return Class.forName("java.lang." + className);        }        else { //use class loader            return Class.forName(className);        }    }    private void printMethods (Class c) {        Method [] allMethods = c.getDeclaredMethods();        for (int i = 0; i < allMethods.length; i++) {            Method m = allMethods[i];            System.out.print (m.getReturnType() + " " + m.getName() + " (");            Class []params = m.getParameterTypes();            if (params != null && params.length > 0) {                System.out.print(params[0].toString());                for (int j = 1; j < params.length; j++)                    System.out.print(", " + params[j].toString());            }            System.out.println (")");        }    }    /* get a method for a class and parameter - so given class foo.Peron     * try to get a Method object for methodName (paramClassName) in Class c     * @param c the class to find the method in     * @param methodName the method to call     * @param paramClassName     * @return a Method to invoke on an instance of this class     */    protected Method getASetMethod (Class c, String methodName, String paramClassName) throws Exception {		return getASetMethod (c, methodName, getAClass (paramClassName));	}    protected Method getASetMethod (Class c, String methodName, Class paramClass) throws Exception {            // printMethods (c); //  debug line for reflection            Method m = null;			Class origClass = c;            while (m == null) {                try {					//   Class paramClass = getAClass (paramClassName);                    m = c.getDeclaredMethod(methodName, new Class[] {paramClass});                } catch (Exception e) {                    c = c.getSuperclass();                    if (c == null) {						System.out.println ("Unable to find: " + methodName + " ("											+paramClass.getName () +")in class: " 											+ origClass.getName());						throw e;					}                }            }            return m;    }    /* set a field in this object using the current elementNode     * eg. a parsed <name>colonel exception</name> on Person     * try to find the node name in desc list     * and the type of the data - then call setName (String) on the passed object     *     * @param thisNode the current element     * @param obj the current object     * @param desc the descriptor for the current object (contains attList and elementList)     * @param context the current state of unmarshalling (with ID IDREF, hashtables - etc)     *     */    protected void setElement (XmlNode thisNode, Object obj, XmlDescriptor desc, UnmarshalContext context) throws Exception {        String elName = thisNode.getName ();   //getTagName();        JField jf = (JField)desc.elementList.get(elName);        if (jf == null)            throw new FieldNotFoundException ("ephman.abra.tools.nofield", 											  new Object[]{elName, desc.className});		if (jf.isArray ()) return ; // will do seperately..        Class thisClass = obj.getClass();        Object value = null;        Method m = null;        if (jf.isCollection()) {            m = getASetMethod (thisClass, "addTo" + jf.getGetSet(), jf.getObjectType());        } else {            m = getASetMethod (thisClass, "set" + jf.getGetSet(), jf.getObjectType());        }        if (jf instanceof JCompositeField) {            XmlDescriptor fieldDesc = (XmlDescriptor)this.classList.get (jf.getObjectType());            value = unmarshal (thisNode, context, fieldDesc);        }        else {			value = getPrimitive (thisNode, jf, context);                    }        m.invoke(obj, new Object[]{value});		//		if (jf.getObjectType ().equals ("char"))		//System.out.println ("was ok");    }	Object getPrimitive (XmlNode thisNode, JField jf, UnmarshalContext context) 		throws Exception {		try {			Object value = null;			Class primClass = getAClass (getFieldType(jf.getObjectType()));			//			Node primVal = thisNode.getFirstChild();			String nodeValue = thisNode.getText ();			if (Checks.exists (nodeValue)) {				if (jf.isDate () && dateFormatStrings != null) { // use dformats instead of int					value = getDate (nodeValue, context);				} else {					Object forCons = nodeValue; //primVal.getNodeValue();					Class initClass = null;					if (jf.isDate ()) {						forCons = new Long((String)forCons);						initClass = java.lang.Long.TYPE;					} else						initClass = string;										Constructor con = primClass.getDeclaredConstructor(new Class[]{initClass});					value = con.newInstance(new Object[]{forCons});				}			}			else if (jf.getObjectType().equals("String"))				value = "";			else if (jf.getObjectType ().equals ("double")) 				return new Double (Double.NaN);			else if (jf.getObjectType ().equals ("char")) {				//	System.out.println ("Null Character");				value = new Character (' ');			}  				return value;		} catch (Exception e) {			throw new AbraException ("ephman.abra.tools.badprimitive", new Object []{jf.getJavaName ()}, e);		}	}		    /* look through a nodes element list and recurse on all children     *     * @param children the Node list that contains this nodes child elements     * @param obj the current object     * @param desc the descriptor for the current object (contains attList and elementList)     * @param context the current state of unmarshalling (with ID IDREF, hashtables - etc)     *     */	 protected void recurseOnChildList (Vector children, Object obj, XmlDescriptor desc, UnmarshalContext context) throws	  Exception {		 if (children == null) return ;		 for (int i = 0; i < children.size (); i++ )			 try {				 setElement ((XmlNode)children.elementAt (i), obj, desc, context);			 } catch (FieldNotFoundException e) {				 if (context.ignoreMissingFields)					 System.err.println (e.getMessage ());				 else throw e;			 }	 }	/*	  protected void recurseOnChildList (NodeList children, Object obj, XmlDescriptor desc, UnmarshalContext context) throws	  Exception {	  	  for (int i = 0; i < children.getLength (); i++ )	  if (children.item (i) instanceof Element) {	  try {	  setElement ((Element)children.item (i), obj, desc, context);	  } catch (FieldNotFoundException e) {	  if (context.ignoreMissingFields)	  System.err.println (e.getMessage ());	  else throw e;	  }	  }	  }*/    /** get a native type primary class name (so for 'int' return 'java.lang.Integer'     *     *     */    protected String getFieldType (String fieldType) {        if (fieldType.equals ("String"))            return "java.lang.String";        else if (fieldType.equals("int"))            return "java.lang.Integer";		else if (fieldType.equals("long"))            return "java.lang.Long";        else if (fieldType.equals("boolean"))            return "java.lang.Boolean";        else if (fieldType.equals("char"))			return "java.lang.Character";        else if (fieldType.equals("double"))            return "java.lang.Double";        else if (fieldType.equals("float"))            return "java.lang.Float";        else if (fieldType.equals("Date") || fieldType.equals("Timestamp"))            return "java.sql.Timestamp";        else return "";    }    // not sure what to do with this ??    protected void applyRules (Element e, String mapName) throws		ParserConfigurationException,		FileNotFoundException,		IOException,		SAXException,		SchemaException {    }	protected  void applyRules (XmlNode thisNode, String mapFile) throws	    XmlException, 		IOException,		SchemaException { }	public static final String VERSION_NUMBER = "version_number"; // SAME_AS GenricFactoryBase}

⌨️ 快捷键说明

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