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

📄 propertyutil.java

📁 javaEE 原代码 javaEE 原代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
					String tmp = formatString(obj);
					ret = new Double(tmp);
				} else {
					exception = true;
				}
			} else if (targetType.equals(TYPE_SHORT_OBJ)) { // convert to Long
				if (sourceType.equals(TYPE_STRING)) {
					String tmp = (String) obj;
					if (tmp == null || tmp.length() < 1) {
						tmp = "0";
					}

					ret = new Short(tmp);
				} else if (sourceType.equals(TYPE_SHORT)) {
					String tmp = formatString(obj);
					ret = new Short(tmp);
				} else {
					exception = true;
				}
			} else {
				// exception = true;

			}
		} catch (NumberFormatException ne) {
			throw new SysException("APP-1004", "APP", "录入数据异常", this.getClass()
					.getName()
					+ ":objTypeConvert()", null, ne, sourceType + " to "
					+ targetType);
		}

		if (exception) {
			throw new SysException("SYS-0900", "SYS", "网络繁忙,请稍后再试!", this
					.getClass().getName()
					+ ":objTypeConvert()", null, null, "无效数据转换方式:" + sourceType
					+ " to " + targetType);
		}

		return ret;
	}

	/**
	 * 获得超类属性,可以获得n级超类的属性(最顶层类为java.lang.Object)
	 * 
	 * @param cls
	 * @return
	 */
	private Field[] getExtendsFields(Class cls) {
		Field[] ret = null;
		if (cls == null) {
			return ret;
		}
		Field[] cur = cls.getDeclaredFields();
		Class supClass = cls.getSuperclass();
		Field[] sup = null;
		if (supClass != null) {
			if (!supClass.getName().equals("java.lang.Object")) {
				// Class tmp = supClass.getSuperclass();
				sup = getExtendsFields(supClass);
			}
		}

		List list = new ArrayList();
		if (sup != null) {
			for (int i = 0; i < sup.length; i++) {
				list.add(sup[i]);
			}
		}
		if (cur != null) {
			for (int i = 0; i < cur.length; i++) {
				list.add(cur[i]);
			}
		}

		if (list.size() > 0) {
			ret = new Field[list.size()];
			for (int i = 0; i < list.size(); i++) {
				ret[i] = (Field) list.get(i);
			}
		}

		return ret;
	}

	/**
	 * 集合类型转化 Iterator to Set
	 * 
	 * @param it
	 *            Iterator对象
	 * @return
	 */
	public static Set iteratorToSet(Iterator it) {
		Set ret = new HashSet();

		if (it != null) {
			while (it.hasNext()) {
				ret.add(it.next());
			}
		}

		return ret;
	}

	/**
	 * 字符串转化成集合 String to Set
	 * 
	 * @param source
	 *            用符号分割的字符串
	 * @param symbol
	 *            使用的符号
	 * @return HashSet
	 */
	public static Set StringToSet(String source, String symbol) {
		Set ret = new HashSet();

		if ((source == null) || (source.length() < 1)) {
			return ret;
		}

		if ((symbol == null) || (symbol.length() < 1)) {
			return ret;
		}

		int ban = 0;
		String temp = source;
		ban = temp.indexOf(symbol);

		while (ban > -1) {
			ret.add(temp.substring(0, ban));
			temp = temp.substring(ban + 1, temp.length());
			ban = temp.indexOf(symbol);
		}

		ret.add(temp);

		return ret;
	}

	/**
	 * 将持久层对象转换成XML(只对对象中String,double,Date,long,Set 5种数据类型进行转换)
	 * 
	 * @param obj
	 *            持久层对象
	 * @return
	 */
	public String objectToXml(Object obj) throws SysException {
		StringBuffer ret = new StringBuffer("");

		if (obj == null) {
			return ret.toString();
		}

		Class cls = obj.getClass();
		// Field[] fields = cls.getDeclaredFields();
		Field[] fields = getExtendsFields(cls);
		int len = fields.length;
		ret.append("<" + getClassName(cls) + ">");

		String fieldName = "";
		String fieldType = "";

		Object tmpVal = null;

		try {
			for (int i = 0; i < len; i++) {
				fieldName = fields[i].getName();
				fieldType = fields[i].getType().getName();

				if (fieldType.equals(TYPE_STRING)
						|| fieldType.equals(TYPE_DOUBLE)
						|| fieldType.equals(TYPE_LONG)
						|| fieldType.equals(TYPE_DATE)
						|| fieldType.equals(TYPE_LONG_OBJ)) {
					ret.append("<");
					ret.append(fieldName.toLowerCase());
					ret.append(">");

					tmpVal = getPropertyValue(fieldName, null, null, obj);
					ret.append(formatString(tmpVal));

					ret.append("</");
					ret.append(fieldName.toLowerCase());
					ret.append(">");
				} else if (fieldType.equals(TYPE_SET)
						|| fieldType.equals(TYPE_HASHSET)) {
					ret.append("<items>");
					tmpVal = getPropertyValue(fieldName, null, null, obj);

					Set set = (Set) tmpVal;

					if (set != null) {
						Iterator it = set.iterator();

						while (it.hasNext()) {
							ret.append(objectToXml(it.next()));
						}
					}

					ret.append("</items>");
				}
			}
		} catch (NoSuchMethodException e) {
			throw new SysException("SYS-0014", "SYS", "网络繁忙,请稍后再试!", this
					.getClass().getName()
					+ ":objectToObject()", null, e, "值对象缺少基本方法:" + fieldName);
		} catch (InvocationTargetException e) {
			throw new SysException("SYS-0015", "SYS", "网络繁忙,请稍后再试!", this
					.getClass().getName()
					+ ":objectToObject()", null, e);
		} catch (IllegalAccessException e) {
			throw new SysException("SYS-0016", "SYS", "网络繁忙,请稍后再试!", this
					.getClass().getName()
					+ ":objectToObject()", null, e);
		}

		ret.append("</" + getClassName(cls) + ">");

		return ret.toString();
	}

	/**
	 * 数组转化成用符号分隔的字符串
	 * 
	 * @param array
	 *            源数组
	 * @param symbol
	 *            使用的分隔符
	 * @return
	 */
	public static String ArrayToString(String[] array, String symbol) {
		StringBuffer ret = new StringBuffer();

		if (array != null) {
			for (int i = 0; i < array.length; i++) {
				ret.append(array[i]);
				ret.append(symbol);
			}
		}

		ret.deleteCharAt(ret.length() - 1);

		return ret.toString();
	}

	public String getClassName(Class cls) {
		String ret = cls.getName();
		int ban = ret.lastIndexOf(".");

		if (ban > 0) {
			ret = ret.substring(ban + 1, ret.length());
		}

		return ret;
	}

	/**
	 * 格式化字符串
	 * 
	 * @param obj
	 * @return
	 */
	public String formatString(Object obj) {
		String ret = "";

		if (obj == null) {
			return ret;
		}

		if (obj instanceof String) {
			String o = (String) obj;
			ret = formatString(o);
		} else if (obj instanceof Double) {
			Double o = (Double) obj;
			ret = formatString(o.doubleValue());
		} else if (obj instanceof Long) {
			Long o = (Long) obj;
			ret = formatString(o.longValue());
		} else if (obj instanceof Date) {
			Date o = (Date) obj;
			ret = formatString(o);
		} else if (obj instanceof Short) {
			Short o = (Short) obj;
			ret = formatString(o.shortValue());
		} else {
			ret = null;
		}

		return ret;
	}

	public String formatString(String str) {
		String ret = "";

		if (str != null) {
			ret = str;
		}

		return ret;
	}

	public String formatString(long lon) {
		return String.valueOf(lon);
	}

	public String formatString(double doub) {
		return String.valueOf(doub);
	}

	public String formatString(Date date) {
		String ret = "";

		if (date != null) {
			ret = DateUtil.toString(date, true);
		}

		return ret;
	}

	/**
	 * 截取显示字符串
	 * 
	 * @param source
	 *            要显示的字符串
	 * @param leng
	 *            显示长度(以ASC码长度为单位)
	 * @return string
	 */
	public String subString(String source, int leng) {
		String ret = null;

		if (source == null) {
			ret = " ";
		}

		try {
			StringBuffer buffer = new StringBuffer();
			int count = 0;
			String tmp = null;
			String asc = null;

			for (int i = 0; i < source.length(); i++) {
				tmp = source.substring(i, i + 1);
				asc = new String(tmp.getBytes(), "ASCII");
				count = count + asc.length();

				if (count > leng) { // 超过限制

					break;
				} else {
					buffer.append(tmp);
				}
			}

			if (count > leng) {
				ret = buffer.toString() + "…";
			} else {
				ret = buffer.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		// ret = ResponseUtils.filter(ret);

		return ret;
	}

	public void setValue(Object target, String filedname, Object tmpVal)
			throws SysException {
		if (target == null || filedname == null || tmpVal == null) {
			return;
		}
		Class cTar = target.getClass();
		Class[] paramType = new Class[1];
		Object[] paramValue = new Object[1];
		String fTypeTar = null; // target field type

		Method setMethod = null;

		try {
			Field tmpField = cTar.getDeclaredField(filedname);
			fTypeTar = tmpField.getType().getName();
			paramType[0] = tmpField.getType();
			if (tmpVal != null) {
				if (fTypeTar.equals("java.lang.String")) {
					paramValue[0] = tmpVal;
				} else { // need type convert
					paramValue[0] = objTypeConvert(fTypeTar,
							"java.lang.String", tmpVal);
				}

				/** get the setXXX() method from target obj */
				setMethod = cTar.getMethod(getSetMethodName(filedname),
						paramType);

				/**
				 * invoker the setXXX() method,complete property copy
				 */
				setMethod.invoke(target, paramValue);
				// System.out.print(filedname + " = " + tmpVal + ";");
			}

			/** get the property value from http request */

		} catch (NoSuchMethodException e) {
			throw new SysException("SYS-0014", "SYS", "网络繁忙,请稍后再试!", this
					.getClass().getName()
					+ ":requestToObject()", null, e, "值对象缺少基本方法:" + filedname);
		} catch (InvocationTargetException e) {
			throw new SysException("SYS-0015", "SYS", "网络繁忙,请稍后再试!", this
					.getClass().getName()
					+ ":requestToObject()", null, e, "property<" + filedname
					+ "> value<" + tmpVal + ">");
		} catch (IllegalAccessException e) {
			throw new SysException("SYS-0016", "SYS", "网络繁忙,请稍后再试!", this
					.getClass().getName()
					+ ":requestToObject()", null, e);
		} catch (NullPointerException upe) {

			throw upe;
		} catch (SecurityException e) {
			throw new SysException("SYS-0014", "SYS", "网络繁忙,请稍后再试!", this
					.getClass().getName()
					+ ":requestToObject()", null, e, "值对象缺少基本方法:" + filedname);
		} catch (NoSuchFieldException e) {
			throw new SysException("SYS-0014", "SYS", "网络繁忙,请稍后再试!", this
					.getClass().getName()
					+ ":requestToObject()", null, e, "值对象缺少基本属性:" + filedname);
		}
	}

	public static void main(String[] args) {
		try {
			PropertyUtil pc = new PropertyUtil();
			/**
			 * PropertyUtil pc = new PropertyUtil(); String a = "喀<>搭街坊立刻机安康大家发射点解放";
			 * String b = "kla<table>lasfksafasdfalksdjfalsk"; String c =
			 * "可&ksd
			 * <tr>搭aa街dfas坊建13234立sdf"; String d = "12<%=1%>1按时地方2ds大师傅kj按时大sdh";
			 * System.out.println(pc.subString(a, 20));
			 * System.out.println(pc.subString(b, 20));
			 * System.out.println(pc.subString(c, 20));
			 * System.out.println(pc.subString(d, 20));
			 */
			// Long t = new Long(1);
			// System.out.println(t.getClass().getName());
			// Object obj = pc.objTypeConvert(pc.TYPE_STRING, pc.TYPE_LONG_OBJ,
			// new Long(1));
			// System.out.println(obj);
			// System.out.println(pc.objectToXml(new WbQyb()));
			User user = new User();
			pc.setValue(user, "loginname", null);
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
		}
	}

	public String toString(Object obj) throws SysException {
		StringBuffer ret = new StringBuffer("");

		if (obj == null) {
			return ret.toString();
		}

		Class cls = obj.getClass();
		Field[] fields = getExtendsFields(cls);
		int len = fields.length;
		// ret.append("<" + getClassName(cls) + ">");

		String fieldName = "";
		String fieldType = "";

		Object tmpVal = null;

		try {
			for (int i = 0; i < len; i++) {
				fieldName = fields[i].getName();
				fieldType = fields[i].getType().getName();

				if (fieldType.equals(TYPE_STRING)
						|| fieldType.equals(TYPE_DOUBLE)
						|| fieldType.equals(TYPE_LONG)
						|| fieldType.equals(TYPE_DATE)
						|| fieldType.equals(TYPE_LONG_OBJ)) {
					ret.append(fieldName.toLowerCase() + ":");

					tmpVal = getPropertyValue(fieldName, null, null, obj);
					ret.append(formatString(tmpVal) + "; ");

				} else if (fieldType.equals(TYPE_SET)
						|| fieldType.equals(TYPE_HASHSET)) {
					ret.append("<items>");
					tmpVal = getPropertyValue(fieldName, null, null, obj);

					Set set = (Set) tmpVal;

					if (set != null) {
						Iterator it = set.iterator();

						while (it.hasNext()) {
							ret.append(objectToXml(it.next()));
						}
					}

					ret.append("</items>");
				}
			}
		} catch (NoSuchMethodException e) {
			throw new SysException("SYS-0014", "SYS", "网络繁忙,请稍后再试!", this
					.getClass().getName()
					+ ":objectToObject()", null, e, "值对象缺少基本方法:" + fieldName);
		} catch (InvocationTargetException e) {
			throw new SysException("SYS-0015", "SYS", "网络繁忙,请稍后再试!", this
					.getClass().getName()
					+ ":objectToObject()", null, e);
		} catch (IllegalAccessException e) {
			throw new SysException("SYS-0016", "SYS", "网络繁忙,请稍后再试!", this
					.getClass().getName()
					+ ":objectToObject()", null, e);
		}
		System.out.println(ret.toString());
		return ret.toString();
	}

}

⌨️ 快捷键说明

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