// Serialize the bean using the specified namespace prefix & uri public String serialize(Object bean) throws IntrospectionException, IllegalAccessException { // Use the class name as the name of the root element String className = bean.getClass().getName(); String rootElementName = null; if (bean.getClass().isAnnotationPresent(ObjectXmlAlias.class)) { AnnotatedElement annotatedElement = bean.getClass(); ObjectXmlAlias aliasAnnotation = annotatedElement.getAnnotation(ObjectXmlAlias.class); rootElementName = aliasAnnotation.value(); } // Use the package name as the namespace URI Package pkg = bean.getClass().getPackage(); nsURI = pkg.getName(); // Remove a trailing semi-colon (;) if present (i.e. if the bean is an array) className = StringUtils.deleteTrailingChar(className, ';'); StringBuffer sb = new StringBuffer(className); String objectName = sb.delete(0, sb.lastIndexOf(".") + 1).toString(); domDocument = createDomDocument(objectName); document = domDocument.getDocument(); Element root = document.getDocumentElement(); // Parse the bean elements getBeanElements(root, rootElementName, className, bean); StringBuffer xml = new StringBuffer(); if (prettyPrint) xml.append(domDocument.serialize(lineSeperator, indentChars, includeXmlProlog)); else xml.append(domDocument.serialize(includeXmlProlog)); if (!includeTypeInfo) { int index = xml.indexOf(root.getNodeName()); xml.delete(index - 1, index + root.getNodeName().length() + 2); xml.delete(xml.length() - root.getNodeName().length() - 4, xml.length()); } return xml.toString(); }
public static void main(String[] args) throws Exception { Class clazz = Class.forName("com.mtl.spring.aop.helloworld.ArithmeticCalculatorLoggingProxy"); Object object = clazz.newInstance(); Method[] methods = clazz.getMethods(); StringBuffer sb = new StringBuffer(); sb.append("public class ") .append(object.getClass().getCanonicalName()) .append("{\n") .append("Object object ") .append("\n"); Constructor[] constructors = clazz.getConstructors(); for (Constructor constructor : constructors) { sb.append("\npublic ").append(constructor.getName()).append("("); Class<?> p[] = constructor.getParameterTypes(); for (int j = 0; j < p.length; ++j) { sb.append(p[j].getName() + " arg" + j); if (j < p.length - 1) { sb.delete(sb.length() - 1, sb.length()); } } sb.append(") {").append("\n\n"); sb.append("user.role.cotain(\"/admin/add\") {"); sb.append("\n System.currentTimeMillis();\n"); sb.append("this.object = ").append("arg0"); sb.append("}"); sb.append(" \nSystem.currentTimeMillis();\n"); sb.append("\n}"); } sb.append("\n\n}"); System.out.println(sb); for (Method method : methods) { System.out.println(method.getName()); } }
private ClassLoaderStrategy getClassLoaderStrategy(String fullyQualifiedClassName) throws Exception { try { // Get just the package name StringBuffer sb = new StringBuffer(fullyQualifiedClassName); sb.delete(sb.lastIndexOf("."), sb.length()); currentPackage = sb.toString(); // Retrieve the Java classpath from the system properties String cp = System.getProperty("java.class.path"); String sepChar = System.getProperty("path.separator"); String[] paths = StringUtils.pieceList(cp, sepChar.charAt(0)); ClassLoaderStrategy cl = ClassLoaderUtil.getClassLoader(ClassLoaderUtil.FILE_SYSTEM_CLASS_LOADER, new String[] {}); // Iterate through paths until class with the specified name is found String classpath = StringUtils.replaceChar(currentPackage, '.', File.separatorChar); for (int i = 0; i < paths.length; i++) { Class[] classes = cl.getClasses(paths[i] + File.separatorChar + classpath, currentPackage); for (int j = 0; j < classes.length; j++) { if (classes[j].getName().equals(fullyQualifiedClassName)) { return ClassLoaderUtil.getClassLoader( ClassLoaderUtil.FILE_SYSTEM_CLASS_LOADER, new String[] {paths[i]}); } } } throw new Exception("Class could not be found."); } catch (Exception e) { System.err.println("Exception creating class loader strategy."); System.err.println(e.getMessage()); throw e; } }
/** Receive notification of character data inside an element. */ @Override public void characters(char ch[], int start, int len) { while (len > 0 && Character.isWhitespace(ch[start])) { ++start; --len; } while (len > 0 && Character.isWhitespace(ch[start + len - 1])) { --len; } if (len > 0) { if (_chars.length() > 0) { _chars.append(' '); } _chars.append(ch, start, len); } }
/** Converts a Record into a String representation */ public String toString() { StringBuffer sb = new StringBuffer(); sb.append(name); if (sb.length() < 8) sb.append("\t"); if (sb.length() < 16) sb.append("\t"); sb.append("\t"); if (Options.check("BINDTTL")) sb.append(TTL.format(ttl)); else sb.append(ttl); sb.append("\t"); if (dclass != DClass.IN || !Options.check("noPrintIN")) { sb.append(DClass.string(dclass)); sb.append("\t"); } sb.append(Type.string(type)); String rdata = rrToString(); if (!rdata.equals("")) { sb.append("\t"); sb.append(rdata); } return sb.toString(); }
static String dirForFqcn(String fqcn) { int last_dot = fqcn.lastIndexOf('.'); StringBuffer sb = new StringBuffer(fqcn.substring(0, last_dot + 1)); for (int i = 0, len = sb.length(); i < len; ++i) if (sb.charAt(i) == '.') sb.setCharAt(i, '/'); return sb.toString(); }
/** Receive notification of the end of an element. */ @Override public void endElement(String uri, String l, String q) { /* * 1. If current element is a String, update its value from the string buffer. * 2. Add the element to parent. */ ElementInfo element = _stack.remove(_stack.size() - 1); _logger.fine("endElement " + element); if (element.type == null) { _logger.warning("Element " + element.name + " not created "); return; } else if (_chars.length() > 0) { try { injectProperty(element.data, String.class, _chars.toString(), null, null); } catch (Exception x) { if (!_lenient) { throw new BeanAssemblyException( "Failed to set characters to object " + element.type.getName(), x); } else { _logger.warning("Failed to set characters to parent " + element.data); } } } _chars.setLength(0); _logger.fine( "<<ElementInfo: " + element.type.getName() + " in " + element + "\n @as is " + element.inst.get("@as") + "\n @id is " + element.inst.get("@id")); if (List.class.isAssignableFrom(element.data.getClass()) && element.name.endsWith("...")) { List<?> list = (List<?>) element.data; Object array = Array.newInstance(element.type, list.size()); for (int i = 0; i < list.size(); ++i) { Array.set(array, i, list.get(i)); } element.data = array; } String id = element.inst.get("@id"); if (id != null) { // locally stored object - not added to the parent _local.put(id, element); } else if (!_stack.isEmpty()) { // inject into the parent as a property ElementInfo parent = _stack.get(_stack.size() - 1); _logger.fine("Parent is " + parent.data.getClass().getName()); try { String as = element.inst.get("@as"); if (as != null) { injectProperty( parent.data, element.type, element.data, Strings.toCamelCase(as, '-', false), element.args.complete()); } else { injectProperty(parent.data, element.type, element.data, null, element.args.complete()); } } catch (Exception x) { if (!_lenient) { throw new BeanAssemblyException( "Failed to set value " + element.data + " to parent " + parent.data, x); } else { _logger.log( Level.WARNING, "Failed to set value " + element.data + " to parent " + parent.data, x); } } } _top = element.data; }