001: /*
002: * Created on Jun 6, 2003
003: *
004: * To change the template for this generated file go to
005: * Window>Preferences>Java>Code Generation>Code and Comments
006: */
007: package org.xdev.base.core;
008:
009: /**
010: * @author AYegorov
011: *
012: * To change the template for this generated type comment go to
013: * Window>Preferences>Java>Code Generation>Code and Comments
014: */
015: import org.xdev.base.core.object.*;
016: import org.xdev.base.log.*;
017:
018: import java.util.*;
019: import java.util.regex.Matcher;
020: import java.util.regex.Pattern;
021: import java.io.*;
022:
023: import java.lang.reflect.*;
024:
025: import org.jdom.*;
026: import org.jdom.input.*;
027: import org.jdom.output.*;
028:
029: import org.apache.log4j.*;
030:
031: public class BASE {
032: private static Logger logger = Logger.getLogger("system");
033:
034: public static Object newObjectFromClass(String className,
035: Class[] parameterClasses, Object[] parameterValues)
036: throws Exception {
037: Class cls = Class.forName(className);
038: Constructor c = cls.getConstructor(parameterClasses);
039:
040: cls = null;
041:
042: return c.newInstance(parameterValues);
043:
044: }
045:
046: public static String getValueFromObject(Object obj) {
047: String value = "";
048:
049: if (obj instanceof Configuration) {
050: value = ((Configuration) obj).getProperty("value");
051: } else if (obj instanceof ArrayList) {
052: java.lang.StringBuffer buffer = new java.lang.StringBuffer();
053: ArrayList list = (ArrayList) obj;
054: int size = list.size();
055: for (int i = 0; i < size; i++) {
056: Object o = list.get(i);
057: buffer.append("Value ").append(i).append(":");
058: if (o != null) {
059: buffer.append(o.toString());
060: } else {
061: buffer.append("");
062: }
063: buffer.append("\n");
064: }
065: value = buffer.toString();
066: buffer = null;
067: list = null;
068: } else if (obj instanceof HashMap) {
069: value = ((HashMap) obj).toString();
070: } else if (obj instanceof Exception) {
071: java.lang.StringBuffer buffer = new java.lang.StringBuffer();
072: try {
073: Exception ex = (Exception) obj;
074: ByteArrayOutputStream out = new ByteArrayOutputStream();
075: ex.printStackTrace(new PrintStream(out));
076: byte[] bytes = out.toByteArray();
077: for (int i = 0; i < bytes.length; i++) {
078: buffer.append((char) bytes[i]);
079: }
080: out.close();
081: bytes = null;
082: out = null;
083: } catch (Exception ex) {
084: }
085: value = buffer.toString();
086: buffer = null;
087:
088: } else if (obj instanceof File) {
089: try {
090: BufferedReader reader = new BufferedReader(
091: new FileReader((File) obj));
092:
093: String line = null;
094:
095: StringBuffer buffer = new StringBuffer();
096:
097: while ((line = reader.readLine()) != null) {
098: buffer.append(line).append("\n");
099: }
100:
101: value = buffer.toString();
102: } catch (Exception ex) {
103: LoggerWriter.log(ex, Level.ERROR_INT, BASE.class);
104: }
105:
106: } else if (obj != null) {
107: value = obj.toString();
108: }
109: obj = null;
110:
111: return value;
112: }
113:
114: public static File getFile(String path, Class cls) {
115: java.net.URL url = cls.getResource(path);
116: File f = null;
117: if (url != null) {
118: f = new File(url.getPath());
119: } else {
120: f = getFile(path);
121: }
122: url = null;
123:
124: return f;
125: }
126:
127: public static File getFile(String path) {
128: String classPath = System.getProperty("java.class.path");
129:
130: String div = System.getProperty("path.separator");
131: path = getFormattedPath(path);
132: StringTokenizer tokens = new StringTokenizer(classPath, div);
133: File f = null;
134: while (tokens.hasMoreTokens()) {
135: String fullPath = tokens.nextToken() + path;
136: //System.out.println("Trying resource at path: "+fullPath);
137: f = new File(fullPath);
138: if (f.exists()) {
139: //System.out.println("Resource has been found: "+f.getPath());
140: break;
141: }
142: }
143:
144: return f;
145: }
146:
147: public static String getFormattedPath(String path) {
148: String slash = System.getProperty("file.separator");
149: if (!path.startsWith(slash)) {
150: String tempSlash = null;
151: if (slash.equals("/")) {
152: tempSlash = "\\";
153: } else if (slash.equals("\\")) {
154: tempSlash = "/";
155: }
156: if (path.startsWith(tempSlash)) {
157: path = slash + path.substring(1, path.length());
158: } else {
159: path = slash + path;
160: }
161: }
162:
163: return path;
164: }
165:
166: public static String getFormattedXml(String xml) {
167: SAXBuilder builder = null;
168: XMLOutputter out = null;
169: Document doc = null;
170:
171: try {
172: builder = new SAXBuilder();
173:
174: out = new XMLOutputter();
175: out.setTextTrim(true);
176: out.setTrimAllWhite(true);
177:
178: doc = builder.build(new StringReader(xml));
179:
180: xml = out.outputString(doc);
181: } catch (Exception ex) {
182: logger.error(ex);
183: } finally {
184: builder = null;
185: out = null;
186: doc = null;
187: }
188:
189: return xml;
190: }
191:
192: public static boolean partialMatch(String exp, String text) {
193: boolean pass = false;
194:
195: try {
196: Pattern p = Pattern.compile(exp);
197: Matcher m = p.matcher(text);
198:
199: pass = m.find();
200: } catch (Exception ex) {
201: pass = false;
202: }
203:
204: return pass;
205: }
206: }
|