1 16 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.FileOutputStream ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.util.Properties ; 26 27 import javax.xml.parsers.DocumentBuilderFactory ; 28 import javax.xml.transform.Result ; 29 import javax.xml.transform.Source ; 30 import javax.xml.transform.Transformer ; 31 import javax.xml.transform.TransformerFactory ; 32 import javax.xml.transform.dom.DOMResult ; 33 import javax.xml.transform.dom.DOMSource ; 34 import javax.xml.transform.sax.SAXResult ; 35 import javax.xml.transform.sax.SAXSource ; 36 import javax.xml.transform.stream.StreamResult ; 37 import javax.xml.transform.stream.StreamSource ; 38 39 import org.xml.sax.InputSource ; 40 import org.xml.sax.helpers.DefaultHandler ; 41 42 67 public class TransformThread implements Runnable 68 { 69 public final static int STREAM = 0; 71 public final static int SAX = 1; 72 public final static int DOM = 2; 73 public final static String [] flavorNames = 74 new String [] { "Stream", "SAX", "DOM" }; 75 76 private static int SOURCE_FLAVOR = STREAM; 78 81 private static int RESULT_FLAVOR = STREAM; 82 85 private static boolean USE_XSLTC = false; 86 88 89 private final static int NUM_THREADS = 2; 91 private static TransformThread INSTANCES[] = null; 92 protected Thread m_thread = null; 93 94 private final static int NUM_TRANSFORMATIONS = 2; 96 97 private final static String XML_IN_BASE = "foo"; 99 private final static String XML_EXT = ".xml"; 100 private final static String XSL_IN_BASE = "foo"; 101 private final static String XSL_EXT = ".xsl"; 102 private final static String FILE_OUT_BASE = "foo_"; 103 private final static String FILE_OUT_EXT = ".out"; 104 105 private int m_thrdNum = -1; 107 108 private InputStream [] m_inStream = null; 109 110 private Source [] m_inSource = null; 111 private Result [] m_outResult = null; 112 113 private Transformer m_transformer = null; 116 117 120 public TransformThread(int thrdNum) 121 { 122 m_thrdNum = thrdNum; 123 124 m_inStream = new InputStream [NUM_TRANSFORMATIONS]; 125 m_inSource = new Source [NUM_TRANSFORMATIONS]; 126 m_outResult = new Result [NUM_TRANSFORMATIONS]; 127 128 try 129 { 130 initSource(); 131 initResult(); 132 133 final String xslSourceFileName = XSL_IN_BASE + m_thrdNum + XSL_EXT; 135 final String xslSourceURI = (new File (xslSourceFileName)).toURL().toString(); 136 StreamSource xslSource = new StreamSource (xslSourceFileName); 137 xslSource.setSystemId(xslSourceURI); 138 139 m_transformer = 141 TransformerFactory.newInstance().newTransformer(xslSource); 142 m_thread = new Thread (this); 143 } 144 catch (Throwable e) 145 { 146 e.printStackTrace(); 147 System.exit(1); 148 } 149 } 150 151 154 private void initResult() 155 { 156 try 157 { 158 for (int i = 0; i < NUM_TRANSFORMATIONS; i++) 159 { 160 switch (RESULT_FLAVOR) 161 { 162 case STREAM : 163 OutputStream outStream = 164 new FileOutputStream (FILE_OUT_BASE + "thread_" 165 + m_thrdNum + "_transformation_" + i + FILE_OUT_EXT); 166 167 m_outResult[i] = new StreamResult (outStream); 168 break; 169 170 case SAX : 171 DefaultHandler defaultHandler = new DefaultHandler (); 172 m_outResult[i] = new SAXResult (defaultHandler); 173 break; 174 175 case DOM : 176 m_outResult[i] = new DOMResult (); 177 break; 178 } 179 } 180 } 181 catch (Exception e) 182 { 183 e.printStackTrace(); 184 System.exit(1); 185 } 186 } 187 188 191 private void initSource() 192 { 193 try 194 { 195 for (int i = 0; i < NUM_TRANSFORMATIONS; i++) 196 { 197 final String sourceXMLURI = (new File (XML_IN_BASE + i + XML_EXT)).toURL().toString(); 199 200 m_inStream[i] = new FileInputStream (XML_IN_BASE + i + XML_EXT); 202 203 switch (SOURCE_FLAVOR) 204 { 205 case STREAM : 206 m_inSource[i] = new StreamSource (m_inStream[i]); 207 break; 208 209 case SAX : 210 m_inSource[i] = new SAXSource (new InputSource (m_inStream[i])); 211 break; 212 213 case DOM : 214 try 215 { 216 DocumentBuilderFactory dfactory = 217 DocumentBuilderFactory.newInstance(); 218 219 dfactory.setNamespaceAware(true); 222 m_inSource[i] = 223 new DOMSource (dfactory.newDocumentBuilder().parse(m_inStream[i])); 224 } 225 catch (Exception e) 226 { 227 e.printStackTrace(); 228 } 229 break; 230 } 231 232 if (m_inSource[i] != null) 233 { 234 m_inSource[i].setSystemId(sourceXMLURI); 238 } 239 } 240 } 241 catch (Exception e) 242 { 243 e.printStackTrace(); 244 System.exit(1); 245 } 246 } 247 248 251 public void run() 252 { 253 try 254 { 255 for (int i = 0; i < NUM_TRANSFORMATIONS; i++) 258 { 259 m_transformer.transform(m_inSource[i], m_outResult[i]); 260 } 261 } 262 catch (Exception e) 263 { 264 e.printStackTrace(); 265 System.exit(1); 266 } 267 } 268 269 271 private static void initThreads() 272 { 273 INSTANCES = new TransformThread[NUM_THREADS]; 274 275 for (int count = 0; count < NUM_THREADS; count++) 276 { 277 INSTANCES[count] = new TransformThread(count); 278 } 279 } 280 281 284 private static void initSystemProperties() 285 { 286 if (USE_XSLTC) 287 { 288 String key = "javax.xml.transform.TransformerFactory"; 292 String value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl"; 293 Properties props = System.getProperties(); 294 props.put(key, value); 295 System.setProperties(props); 296 } 297 } 298 299 303 public static void main(String argv[]) 304 { 305 try 306 { 307 initSystemProperties(); 308 initThreads(); 309 310 for (int count = 0; count < NUM_THREADS; count++) 311 { 312 INSTANCES[count].m_thread.start(); 313 } 314 } 315 catch (Throwable e) 316 { 317 e.printStackTrace(); 318 System.exit(1); 319 } 320 } 321 } 322 | Popular Tags |