1 10 11 package com.triactive.jdo.enhance; 12 13 import com.triactive.jdo.model.Types; 14 import com.triactive.jdo.util.XMLHelper; 15 import java.io.File ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.io.OutputStream ; 19 import java.io.FileOutputStream ; 20 import java.io.PrintWriter ; 21 import java.util.Arrays ; 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 import javax.jdo.spi.PersistenceCapable; 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.ParserConfigurationException ; 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.NodeList ; 33 import org.xml.sax.SAXException ; 34 35 36 53 54 public abstract class Enhancer 55 { 56 57 60 protected Enhancer() {} 61 62 protected int enhance(List classNames) throws Exception 63 { 64 65 ArrayList l = new ArrayList (classNames); 66 Iterator i = l.iterator(); 67 68 while (i.hasNext()) 69 { 70 if (isEnhanced((String )i.next())) 71 i.remove(); 72 } 73 74 if (l.isEmpty()) 75 return 0; 76 else 77 return callExternalEnhancer((String [])l.toArray(new String [l.size()])); 78 } 79 80 81 91 92 private boolean isEnhanced(String className) 93 { 94 Class c; 95 96 try 97 { 98 c = Class.forName(className); 99 } 100 catch (Throwable t) 101 { 102 107 return false; 109 } 110 111 return Types.isEnhancedClass(c); 112 } 113 114 115 121 protected abstract int callExternalEnhancer(String [] classNames) throws Exception ; 122 123 124 139 140 protected static List getOrderedClassNames(String [] metaDataFileNames) 141 throws IOException , ParserConfigurationException , SAXException 142 { 143 HashMap superclassesByClass = new HashMap (); 144 final DocumentBuilder db = XMLHelper.getDocumentBuilder(); 145 146 for (int i = 0; i < metaDataFileNames.length; ++i) 147 { 148 Element docElement = db.parse(new File (metaDataFileNames[i])).getDocumentElement(); 149 150 NodeList nodes = docElement.getElementsByTagName("class"); 151 152 for (int j = 0; j < nodes.getLength(); ++j) 153 { 154 Element clsElement = (Element )nodes.item(j); 155 String packageName = ((Element )clsElement.getParentNode()).getAttribute("name"); 156 String className = packageName + '.' + clsElement.getAttribute("name"); 157 String superclassName = clsElement.getAttribute("persistence-capable-superclass"); 158 159 if (superclassName.length() == 0) 160 superclassName = null; 161 else if (superclassName.indexOf('.') < 0) 162 superclassName = packageName + '.' + superclassName; 163 164 superclassesByClass.put(className, superclassName); 165 } 166 } 167 168 int numClasses = superclassesByClass.size(); 169 ArrayList orderedNames = new ArrayList (numClasses); 170 ArrayList sortedNames = new ArrayList (numClasses); 171 172 sortedNames.addAll(superclassesByClass.keySet()); 173 Collections.sort(sortedNames); 174 175 Iterator i = sortedNames.iterator(); 176 177 while (i.hasNext()) 178 addClass(orderedNames, (String )i.next(), superclassesByClass); 179 180 return orderedNames; 181 } 182 183 184 private static void addClass(List names, String className, Map superclassesByClass) 185 { 186 String superclassName = (String )superclassesByClass.get(className); 187 188 if (superclassName != null) 189 addClass(names, superclassName, superclassesByClass); 190 191 if (!names.contains(className)) 192 names.add(className); 193 } 194 195 196 199 protected static void writeClassListFile(String filename, List classNames) throws IOException 200 { 201 PrintWriter pw = new PrintWriter (new FileOutputStream (new File (filename)), true); 202 203 try 204 { 205 Iterator i = classNames.iterator(); 206 207 while (i.hasNext()) 208 pw.println(i.next()); 209 } 210 finally 211 { 212 pw.flush(); 213 pw.close(); 214 } 215 } 216 } 217 | Popular Tags |