| 1 |
package xtpipes; |
| 2 |
/* |
| 3 |
Xtpipes.java (2009-01-27-22:19) |
| 4 |
*/ |
| 5 |
// import xtpipes.util.InputObject; |
| 6 |
// import xtpipes.util.FileInfo; |
| 7 |
import java.net.URLConnection; |
| 8 |
import java.io.*; |
| 9 |
import java.lang.reflect.*; |
| 10 |
import java.util.HashMap; |
| 11 |
import java.util.Stack; |
| 12 |
import javax.xml.parsers.*; |
| 13 |
import javax.xml.transform.*; |
| 14 |
import javax.xml.transform.dom.DOMSource; |
| 15 |
import javax.xml.transform.stream.*; |
| 16 |
import org.w3c.dom.*; |
| 17 |
import org.xml.sax.*; |
| 18 |
import org.xml.sax.helpers.*; |
| 19 |
import java.net.URL; |
| 20 |
import java.lang.reflect.Constructor; |
| 21 |
import java.util.ArrayList; |
| 22 |
|
| 23 |
|
| 24 |
public class Xtpipes { |
| 25 |
private static HashMap <String,Object> map; |
| 26 |
private static boolean needScript; |
| 27 |
private static boolean returnDom; |
| 28 |
private static String result; |
| 29 |
static PrintWriter logWriter = new PrintWriter( System.err ); |
| 30 |
private static String inFile, |
| 31 |
inData; |
| 32 |
private static boolean exceptionErrs, messages; |
| 33 |
public static InputObject inputObject; |
| 34 |
private static String outFileName; |
| 35 |
private static PrintWriter outPrintWriter; |
| 36 |
private static boolean returnToFile = false; |
| 37 |
public static String scriptFile; |
| 38 |
private static String scriptMap; |
| 39 |
static String i_scriptDir; |
| 40 |
private static TransformerFactory fc; |
| 41 |
private static Transformer identityTransformer; |
| 42 |
private static SAXParserFactory saxFactory; |
| 43 |
private static DocumentBuilder domBuilder; |
| 44 |
private static Stack <XMLReader> saxReaderStack; |
| 45 |
private static Method method; |
| 46 |
private static String rootName; |
| 47 |
static boolean trace; |
| 48 |
private static String [] ml2xml = null; |
| 49 |
static Class<?> ml2xmlClassObj = null; |
| 50 |
public static String errMssg; |
| 51 |
|
| 52 |
public static void main(String args[]) throws Exception { |
| 53 |
map = new HashMap <String,Object> (); |
| 54 |
needScript = true; |
| 55 |
returnDom = false; |
| 56 |
result = null; |
| 57 |
inFile = null; |
| 58 |
inData = null; |
| 59 |
exceptionErrs = false; |
| 60 |
messages = false; |
| 61 |
outFileName = null; |
| 62 |
outPrintWriter = null; |
| 63 |
scriptFile = null; |
| 64 |
i_scriptDir = null; |
| 65 |
scriptMap = null; |
| 66 |
saxReaderStack = new Stack <XMLReader> (); |
| 67 |
rootName = null; |
| 68 |
trace = false; |
| 69 |
|
| 70 |
mainMethod(args); |
| 71 |
} |
| 72 |
|
| 73 |
public static void xtpipes(String [] args, |
| 74 |
OutputStream out, |
| 75 |
PrintWriter log) |
| 76 |
throws Exception { |
| 77 |
map = new HashMap <String,Object> (); |
| 78 |
needScript = true; |
| 79 |
returnDom = false; |
| 80 |
result = null; |
| 81 |
inFile = null; |
| 82 |
inData = null; |
| 83 |
exceptionErrs = false; |
| 84 |
messages = false; |
| 85 |
outFileName = null; |
| 86 |
outPrintWriter = null; |
| 87 |
scriptFile = null; |
| 88 |
i_scriptDir = null; |
| 89 |
scriptMap = null; |
| 90 |
saxReaderStack = new Stack <XMLReader> (); |
| 91 |
rootName = null; |
| 92 |
trace = false; |
| 93 |
|
| 94 |
outPrintWriter = new XtpipesPrintWriter( out, true ); |
| 95 |
logWriter = (log==null)? (new PrintWriter( System.err )) : log; |
| 96 |
mainMethod(args); |
| 97 |
outPrintWriter.flush(); |
| 98 |
} |
| 99 |
|
| 100 |
public static void xtpipes(String [] args, |
| 101 |
OutputStreamWriter out, |
| 102 |
PrintWriter log) |
| 103 |
throws Exception { |
| 104 |
map = new HashMap <String,Object> (); |
| 105 |
needScript = true; |
| 106 |
returnDom = false; |
| 107 |
result = null; |
| 108 |
inFile = null; |
| 109 |
inData = null; |
| 110 |
exceptionErrs = false; |
| 111 |
messages = false; |
| 112 |
outFileName = null; |
| 113 |
outPrintWriter = null; |
| 114 |
scriptFile = null; |
| 115 |
i_scriptDir = null; |
| 116 |
scriptMap = null; |
| 117 |
saxReaderStack = new Stack <XMLReader> (); |
| 118 |
rootName = null; |
| 119 |
trace = false; |
| 120 |
|
| 121 |
outPrintWriter = new XtpipesPrintWriter( out ); |
| 122 |
logWriter = (log==null)? (new PrintWriter( System.err )) : log; |
| 123 |
mainMethod(args); |
| 124 |
outPrintWriter.flush(); |
| 125 |
} |
| 126 |
|
| 127 |
public static void xtpipes(String [] args, |
| 128 |
XtpipesPrintWriter out, |
| 129 |
PrintWriter log) |
| 130 |
throws Exception { |
| 131 |
map = new HashMap <String,Object> (); |
| 132 |
needScript = true; |
| 133 |
returnDom = false; |
| 134 |
result = null; |
| 135 |
inFile = null; |
| 136 |
inData = null; |
| 137 |
exceptionErrs = false; |
| 138 |
messages = false; |
| 139 |
outFileName = null; |
| 140 |
outPrintWriter = null; |
| 141 |
scriptFile = null; |
| 142 |
i_scriptDir = null; |
| 143 |
scriptMap = null; |
| 144 |
saxReaderStack = new Stack <XMLReader> (); |
| 145 |
rootName = null; |
| 146 |
trace = false; |
| 147 |
|
| 148 |
outPrintWriter = out; |
| 149 |
logWriter = (log==null)? (new PrintWriter( System.err )) : log; |
| 150 |
mainMethod(args); |
| 151 |
outPrintWriter.flush(); |
| 152 |
} |
| 153 |
|
| 154 |
public static Document getDOM(String args[]) |
| 155 |
throws Exception { |
| 156 |
map = new HashMap <String,Object> (); |
| 157 |
needScript = true; |
| 158 |
returnDom = false; |
| 159 |
result = null; |
| 160 |
inFile = null; |
| 161 |
inData = null; |
| 162 |
exceptionErrs = false; |
| 163 |
messages = false; |
| 164 |
outFileName = null; |
| 165 |
outPrintWriter = null; |
| 166 |
scriptFile = null; |
| 167 |
i_scriptDir = null; |
| 168 |
scriptMap = null; |
| 169 |
saxReaderStack = new Stack <XMLReader> (); |
| 170 |
rootName = null; |
| 171 |
trace = false; |
| 172 |
|
| 173 |
returnDom = true; |
| 174 |
mainMethod(args); |
| 175 |
Document dom = null; |
| 176 |
if( result == null ){ |
| 177 |
System.err.println( |
| 178 |
"--- xtpipes warning --- getDOM without <return name=\"...\"> from 4xt file: " |
| 179 |
+ scriptFile ); |
| 180 |
} else { |
| 181 |
try{ |
| 182 |
byte [] bytes = result.getBytes("UTF-8"); |
| 183 |
InputStream is = new ByteArrayInputStream( bytes ); |
| 184 |
dom = domBuilder.parse (is); |
| 185 |
} catch ( org.xml.sax.SAXParseException e ){ |
| 186 |
if( Xtpipes.trace ){ |
| 187 |
Xtpipes.logWriter.println( |
| 188 |
"\n---------------------------------------------------\n" |
| 189 |
+ result + |
| 190 |
"\n---------------------------------------------------\n" ); |
| 191 |
} |
| 192 |
String s = ""; |
| 193 |
for( int n=0; n<args.length; n++ ){ |
| 194 |
if( args[n].charAt(0)!='-' ){ |
| 195 |
s += " input file: " + args[n] + "."; break; |
| 196 |
} |
| 197 |
else if( args[n].equals("-s") |
| 198 |
|| args[n].equals("-S") |
| 199 |
|| args[n].equals("-i") |
| 200 |
|| args[n].equals("-o") |
| 201 |
|| args[n].equals("-d") ){ n++; } |
| 202 |
} |
| 203 |
|
| 204 |
if( scriptFile != null ){ s += " script file: " + scriptFile; } |
| 205 |
instructionErr( null, |
| 206 |
"parsing error: " + e.getMessage() +s, 21 ); |
| 207 |
} catch ( Exception e ){ |
| 208 |
instructionErr( null, e.toString(), 5 ); |
| 209 |
} |
| 210 |
if( ml2xmlClassObj != null ){ |
| 211 |
Class<?> [] argTypes = { }; |
| 212 |
Method m = ml2xmlClassObj.getMethod( "closeFiles", argTypes ); |
| 213 |
Object parmValues[] = new Object[0]; |
| 214 |
m.invoke( null, parmValues ); |
| 215 |
} |
| 216 |
|
| 217 |
} |
| 218 |
return dom; |
| 219 |
} |
| 220 |
public static Document getDOM(String s, String args[]) |
| 221 |
throws Exception { |
| 222 |
map = new HashMap <String,Object> (); |
| 223 |
needScript = true; |
| 224 |
returnDom = false; |
| 225 |
result = null; |
| 226 |
inFile = null; |
| 227 |
inData = null; |
| 228 |
exceptionErrs = false; |
| 229 |
messages = false; |
| 230 |
outFileName = null; |
| 231 |
outPrintWriter = null; |
| 232 |
scriptFile = null; |
| 233 |
i_scriptDir = null; |
| 234 |
scriptMap = null; |
| 235 |
saxReaderStack = new Stack <XMLReader> (); |
| 236 |
rootName = null; |
| 237 |
trace = false; |
| 238 |
|
| 239 |
returnDom = true; |
| 240 |
inData = s; |
| 241 |
mainMethod(args); |
| 242 |
Document dom = null; |
| 243 |
if( result == null ){ |
| 244 |
System.err.println( |
| 245 |
"--- xtpipes warning --- getDOM without" |
| 246 |
+ " <return name=\"...\"> from 4xt file: " |
| 247 |
+ scriptFile ); |
| 248 |
} else { |
| 249 |
try{ |
| 250 |
byte [] bytes = result.getBytes("UTF-8"); |
| 251 |
InputStream is = new ByteArrayInputStream( bytes ); |
| 252 |
dom = domBuilder.parse (is); |
| 253 |
} catch ( org.xml.sax.SAXParseException e ){ |
| 254 |
instructionErr( null, "improper xml: " + e.getMessage() |
| 255 |
+ "\n code starts with: " |
| 256 |
+ result.substring(0, Math.min(100,result.length())) |
| 257 |
, 17 ); |
| 258 |
} catch ( Exception e ){ |
| 259 |
instructionErr( null, e.toString(), 6 ); |
| 260 |
} |
| 261 |
if( ml2xmlClassObj != null ){ |
| 262 |
Class<?> [] argTypes = { }; |
| 263 |
Method m = ml2xmlClassObj.getMethod( "closeFiles", argTypes ); |
| 264 |
Object parmValues[] = new Object[0]; |
| 265 |
m.invoke( null, parmValues ); |
| 266 |
} |
| 267 |
|
| 268 |
} |
| 269 |
return dom; |
| 270 |
} |
| 271 |
public static Document getDOM(String args[], PrintWriter log) |
| 272 |
throws Exception { |
| 273 |
logWriter = (log==null)? new PrintWriter( System.err ) : log; |
| 274 |
return getDOM(args); |
| 275 |
} |
| 276 |
public static Document getDOM(String s, String args[], PrintWriter log) |
| 277 |
throws Exception { |
| 278 |
logWriter = (log==null)? (new PrintWriter( System.err )) : log; |
| 279 |
return getDOM(s, args); |
| 280 |
} |
| 281 |
|
| 282 |
private static void mainMethod(String args[]) throws Exception { |
| 283 |
try{ |
| 284 |
String xtpipes_call = |
| 285 |
" xtpipes (2009-01-27-22:19)" |
| 286 |
+ "\n Command line options: " |
| 287 |
+ "\n java xtpipes [-trace] [-help] [-m] [-E] [-s script_file]" |
| 288 |
+ " [-S script_map]" |
| 289 |
+ "\n [-i script_dir] [-o out_file] " |
| 290 |
+ "\n [-x...ml2xml_arg...] " |
| 291 |
+ "(-d in_data | in_file)" |
| 292 |
+ "\n -m messages printing mode" |
| 293 |
+ "\n -E error messages into exception calls" |
| 294 |
+ "\n in_data XML data directly into the command line\n" |
| 295 |
; |
| 296 |
|
| 297 |
boolean help=false; |
| 298 |
for( int n=0; n<args.length; n++ ){ |
| 299 |
if( args[n] == null ){} |
| 300 |
else if( args[n].equals("") ){} |
| 301 |
else if( args[n].charAt(0)!='-' ){ inFile = args[n]; } |
| 302 |
else if( args[n].equals("-m") ){ |
| 303 |
messages = true; |
| 304 |
logWriter.println( |
| 305 |
"xtpipes (2009-01-27-22:19)" |
| 306 |
+ "\n java.version: " + System.getProperty("java.version") |
| 307 |
+ "\n java.class.path: " + System.getProperty("java.class.path") |
| 308 |
+ "\n os.name: " + System.getProperty("os.name") |
| 309 |
+ "\n user.home: " + System.getProperty("user.home") |
| 310 |
+ "\n user.dir: " + System.getProperty("user.dir") |
| 311 |
); |
| 312 |
for( int k=0; k<args.length; k++ ){ |
| 313 |
logWriter.println( " " + args[k] ); |
| 314 |
} |
| 315 |
|
| 316 |
} |
| 317 |
else if( args[n].equals("-s") ){ |
| 318 |
n++; |
| 319 |
if( n < args.length ){ scriptFile=args[n]; } |
| 320 |
else { |
| 321 |
System.err.println( |
| 322 |
"--- Error --- Missing field for -s argument" ); |
| 323 |
inFile = null; inData = null; break; |
| 324 |
} |
| 325 |
|
| 326 |
} |
| 327 |
else if( args[n].equals("-S") ){ |
| 328 |
n++; |
| 329 |
if( n < args.length ){ scriptMap=args[n]; } |
| 330 |
else { |
| 331 |
System.err.println( |
| 332 |
"--- Error --- Missing field for -S argument" ); |
| 333 |
inFile = null; inData = null; break; |
| 334 |
} |
| 335 |
|
| 336 |
} |
| 337 |
else if( args[n].equals("-i") ){ |
| 338 |
n++; |
| 339 |
if( n < args.length ){ |
| 340 |
i_scriptDir=args[n]; |
| 341 |
} else { |
| 342 |
System.err.println( |
| 343 |
"--- Error --- Missing field for -i argument" ); |
| 344 |
inFile = null; inData = null; break; |
| 345 |
} |
| 346 |
|
| 347 |
} |
| 348 |
else if( args[n].equals("-o") ){ |
| 349 |
n++; |
| 350 |
if( n < args.length ){ |
| 351 |
outFileName = args[n]; |
| 352 |
} else { |
| 353 |
System.err.println( |
| 354 |
"--- Error --- Missing field for -o argument" ); |
| 355 |
inFile = null; inData = null; break; |
| 356 |
} |
| 357 |
|
| 358 |
} |
| 359 |
else if( args[n].startsWith("-x") ){ |
| 360 |
if( args[n].substring(2).equals("") ){ |
| 361 |
if( ml2xml == null ){ ml2xml = new String[0]; } |
| 362 |
} else { |
| 363 |
if( ml2xml == null ){ |
| 364 |
ml2xml = new String[1]; |
| 365 |
} else { |
| 366 |
String [] m2x = new String [ml2xml.length + 1]; |
| 367 |
for(int cnt=0; cnt < ml2xml.length; cnt++){ |
| 368 |
m2x[cnt] = ml2xml[cnt]; |
| 369 |
} |
| 370 |
ml2xml = m2x; |
| 371 |
} |
| 372 |
ml2xml[ ml2xml.length - 1 ] = args[n].substring(2); |
| 373 |
} |
| 374 |
|
| 375 |
} |
| 376 |
else if( args[n].equals("-E") ){ |
| 377 |
exceptionErrs = true; |
| 378 |
} |
| 379 |
else if( args[n].equals("-d") ){ |
| 380 |
n++; |
| 381 |
if( n < args.length ){ |
| 382 |
inData = args[n]; |
| 383 |
} else { |
| 384 |
System.err.println( |
| 385 |
"--- Error --- Missing field for -d argument" ); |
| 386 |
inFile = null; inData = null; break; |
| 387 |
} |
| 388 |
|
| 389 |
} |
| 390 |
else if( args[n].equals("-trace") ){ trace=true; } |
| 391 |
else if( args[n].equals("-help") ){ help=true; } |
| 392 |
else { if( !exceptionErrs ){ |
| 393 |
for(int i=n+1; i<args.length; i++ ){ |
| 394 |
if( args[i].equals("-E") ){ exceptionErrs = true; } |
| 395 |
} } |
| 396 |
instructionErr( null, |
| 397 |
"Improper argument: " + args[n] + "\n" + xtpipes_call, 26 ); |
| 398 |
} |
| 399 |
|
| 400 |
} |
| 401 |
if( outPrintWriter == null ){ |
| 402 |
outPrintWriter = new XtpipesPrintWriter(System.out,true); |
| 403 |
} |
| 404 |
|
| 405 |
if( !returnDom ){ |
| 406 |
if( help || ((inFile == null) && (inData == null)) ){ |
| 407 |
System.err.println( xtpipes_call ); |
| 408 |
|
| 409 |
if( (inFile == null) && (inData == null) ){ |
| 410 |
System.exit(0); |
| 411 |
} } } |
| 412 |
|
| 413 |
new FileInfo(logWriter, i_scriptDir, trace); |
| 414 |
if( inFile != null ){ |
| 415 |
inputObject = new InputObject( inFile, logWriter ); |
| 416 |
if( inputObject.getInputStream() == null ){ |
| 417 |
instructionErr( null, "Could not find or open file: " + inFile, 28 ); |
| 418 |
} |
| 419 |
inFile = inputObject.getFilename(); |
| 420 |
} else { |
| 421 |
inputObject = new InputObject( inData.getBytes("UTF-8"), logWriter ); |
| 422 |
} |
| 423 |
inputObject.buildProfile( trace ); |
| 424 |
|
| 425 |
} catch (Exception e){ |
| 426 |
instructionErr( null, e.getMessage(), e.getStackTrace(), 31 ); |
| 427 |
} |
| 428 |
try { |
| 429 |
DocumentBuilderFactory domFactory = |
| 430 |
DocumentBuilderFactory.newInstance(); |
| 431 |
domFactory.setValidating(true); |
| 432 |
DocumentBuilder validatingDomBuilder = |
| 433 |
domFactory.newDocumentBuilder(); |
| 434 |
validatingDomBuilder.setEntityResolver(new XtpipesEntityResolver() |
| 435 |
); |
| 436 |
validatingDomBuilder.setErrorHandler(new ErrorHandler() { |
| 437 |
public void warning(SAXParseException e) throws SAXParseException { |
| 438 |
showSpecifics("warning",e); |
| 439 |
} |
| 440 |
public void error(SAXParseException e) throws SAXParseException { |
| 441 |
showSpecifics("error",e); |
| 442 |
} |
| 443 |
public void fatalError(SAXParseException e) throws SAXParseException { |
| 444 |
showSpecifics("fatal error",e); |
| 445 |
} |
| 446 |
public void showSpecifics(String s, SAXParseException e) |
| 447 |
throws SAXParseException { |
| 448 |
String err = "--- xtpipes " + s + " 24 --- " + e.getSystemId() |
| 449 |
+ " line " + e.getLineNumber() |
| 450 |
+ " col " + e.getColumnNumber() |
| 451 |
+ " : " + e.getMessage() ; |
| 452 |
if( exceptionErrs ) { throw new SAXParseException( |
| 453 |
err, (org.xml.sax.Locator) null ); } |
| 454 |
else { |
| 455 |
System.err.println( err ); |
| 456 |
System.exit(1); |
| 457 |
} } } |
| 458 |
); |
| 459 |
fc = TransformerFactory.newInstance(); |
| 460 |
identityTransformer = fc.newTransformer(); |
| 461 |
identityTransformer.setErrorListener( |
| 462 |
new ErrorListener() { |
| 463 |
public void warning(TransformerException e) throws TransformerException { |
| 464 |
showSpecifics(e); |
| 465 |
} |
| 466 |
public void error(TransformerException e) throws TransformerException { |
| 467 |
showSpecifics(e); |
| 468 |
} |
| 469 |
public void fatalError(TransformerException e) throws TransformerException { |
| 470 |
showSpecifics(e); |
| 471 |
} |
| 472 |
void showSpecifics(TransformerException e) |
| 473 |
throws TransformerException{ |
| 474 |
String err = e.getMessage() ; |
| 475 |
String loc = e.getLocationAsString(); |
| 476 |
if( loc != null ){ err = loc + ": " + err; } |
| 477 |
throw new TransformerException(err); |
| 478 |
} } |
| 479 |
); |
| 480 |
saxFactory = SAXParserFactory.newInstance(); |
| 481 |
saxFactory.setValidating(true); |
| 482 |
domFactory.setValidating(false); |
| 483 |
domBuilder = domFactory.newDocumentBuilder(); |
| 484 |
saxFactory = SAXParserFactory.newInstance(); |
| 485 |
saxFactory.setValidating(false); |
| 486 |
Class <?> cls = Xtpipes.class; |
| 487 |
Class<?> [] argTypes = { Node.class, String.class }; |
| 488 |
method = cls.getMethod( "execute", argTypes ); |
| 489 |
|
| 490 |
if( scriptMap != null ){ |
| 491 |
try{ |
| 492 |
String f = FileInfo.searchFile( scriptMap ); |
| 493 |
if( f == null ){ |
| 494 |
throw new java.io.FileNotFoundException( scriptMap ); |
| 495 |
} else { |
| 496 |
scriptMap = f; |
| 497 |
} |
| 498 |
XMLReader saxReader; |
| 499 |
if( saxReaderStack.empty() ){ |
| 500 |
SAXParser saxParser = saxFactory.newSAXParser(); |
| 501 |
saxReader = saxParser.getXMLReader(); |
| 502 |
saxReader.setEntityResolver(new org.xml.sax.EntityResolver() { |
| 503 |
public InputSource resolveEntity( |
| 504 |
String publicId, String systemId) { |
| 505 |
if( (new File(systemId)).exists() ){ |
| 506 |
return new org.xml.sax.InputSource( systemId ); |
| 507 |
} |
| 508 |
StringReader strReader = new StringReader(""); |
| 509 |
return new org.xml.sax.InputSource(strReader); |
| 510 |
} |
| 511 |
}); |
| 512 |
|
| 513 |
} else { |
| 514 |
saxReader = (XMLReader) saxReaderStack.pop(); |
| 515 |
} |
| 516 |
|
| 517 |
saxReader.setContentHandler( new DefaultHandler(){ |
| 518 |
private Stack <Boolean> condition = new Stack <Boolean> (); |
| 519 |
public void startDocument () { |
| 520 |
condition.push( new Boolean(true) ); |
| 521 |
} |
| 522 |
public void startElement(String ns, String sName, |
| 523 |
String qName, Attributes atts) { |
| 524 |
if( condition == null ){ return; } |
| 525 |
if( Xtpipes.trace ){ |
| 526 |
String s = "<" + qName + "\n"; |
| 527 |
for(int i=0; i<atts.getLength(); i++ ){ |
| 528 |
String name = atts.getQName(i); |
| 529 |
s += " " + name + "=\"" + atts.getValue(i) + "\""; |
| 530 |
} |
| 531 |
s += ">" ; |
| 532 |
Xtpipes.logWriter.println( s ); |
| 533 |
} |
| 534 |
|
| 535 |
boolean cond = ((Boolean) condition.peek()).booleanValue(); |
| 536 |
if( qName.equals("when") ){ |
| 537 |
if( cond ){ String name = atts.getValue("name"); |
| 538 |
String value = atts.getValue("value"); |
| 539 |
if( name.equals("system-id") ){ |
| 540 |
cond = value.equals(inputObject.getSystemId()); |
| 541 |
} |
| 542 |
else |
| 543 |
if( name.equals("public-id") ){ |
| 544 |
cond = value.equals(inputObject.getPublicId()); |
| 545 |
} |
| 546 |
else |
| 547 |
if( name.equals("dtd-root") ){ |
| 548 |
cond = value.equals(inputObject.getDtdRoot()); |
| 549 |
} |
| 550 |
else |
| 551 |
if( name.equals("root") ){ |
| 552 |
cond = value.equals(inputObject.getRoot()); |
| 553 |
} |
| 554 |
else |
| 555 |
if( name.equals("ext") ){ |
| 556 |
cond = inputObject.getFilename().endsWith("." + value); |
| 557 |
} |
| 558 |
else |
| 559 |
if( name.equals("prefix") ){ |
| 560 |
name = inputObject.getFilename(); |
| 561 |
if( name != null ){ |
| 562 |
int i = name.lastIndexOf('/'); |
| 563 |
if( (i != -1) && ((i+1) < name.length()) ){ |
| 564 |
name = name.substring(i+1); |
| 565 |
} |
| 566 |
i = name.lastIndexOf('\\'); |
| 567 |
if( (i != -1) && ((i+1) < name.length()) ){ |
| 568 |
name = name.substring(i+1); |
| 569 |
} |
| 570 |
cond = name.startsWith(value); |
| 571 |
} } |
| 572 |
else |
| 573 |
if( name.equals("meta-type") ){ |
| 574 |
cond = value.equals(inputObject.getMetaType()); |
| 575 |
} |
| 576 |
else |
| 577 |
if( name.equals("content-type") ){ |
| 578 |
cond = value.equals(inputObject.getContentType()); |
| 579 |
} |
| 580 |
} |
| 581 |
} |
| 582 |
else |
| 583 |
if( qName.equals("command-line") ){ |
| 584 |
if( scriptFile != null ){ |
| 585 |
if( trace ){ |
| 586 |
Xtpipes.logWriter.println( " Found script file in map: " |
| 587 |
+ Xtpipes.scriptFile ); |
| 588 |
} |
| 589 |
|
| 590 |
condition = null; |
| 591 |
return; |
| 592 |
} } |
| 593 |
else |
| 594 |
if( qName.equals("processing-instruction") ){ |
| 595 |
if( cond ){ |
| 596 |
String s = inputObject.getXtpipes(); |
| 597 |
if( s != null ){ |
| 598 |
Xtpipes.scriptFile = s; |
| 599 |
if( trace ){ |
| 600 |
Xtpipes.logWriter.println( " Found script file in map: " |
| 601 |
+ Xtpipes.scriptFile ); |
| 602 |
} |
| 603 |
|
| 604 |
condition = null; |
| 605 |
return; |
| 606 |
} } } |
| 607 |
else |
| 608 |
if( qName.equals("select") ){ |
| 609 |
if( cond ){ |
| 610 |
Xtpipes.scriptFile = atts.getValue("name"); |
| 611 |
if( trace ){ |
| 612 |
Xtpipes.logWriter.println( " Found script file in map: " |
| 613 |
+ Xtpipes.scriptFile ); |
| 614 |
} |
| 615 |
|
| 616 |
condition = null; |
| 617 |
return; |
| 618 |
} } |
| 619 |
condition.push( new Boolean(cond) ); |
| 620 |
} |
| 621 |
public void endElement(String ns, String sName, String qName) { |
| 622 |
if( condition == null ){ return; } |
| 623 |
if( Xtpipes.trace ){ |
| 624 |
String s = "</" + qName + ">"; |
| 625 |
Xtpipes.logWriter.println( s ); |
| 626 |
} |
| 627 |
|
| 628 |
condition.pop(); |
| 629 |
} |
| 630 |
|
| 631 |
} ); |
| 632 |
InputStream inputStream = |
| 633 |
(InputStream) (new File(scriptMap).toURI().toURL().openStream()); |
| 634 |
saxReader.parse( new InputSource(inputStream) ); |
| 635 |
saxReaderStack.push( saxReader ); |
| 636 |
} catch( java.io.FileNotFoundException e ){ |
| 637 |
instructionErr( null, |
| 638 |
"File not found: " + e.getMessage() |
| 639 |
+ "; command line option -i", |
| 640 |
33 ); |
| 641 |
} catch( Exception e ){ |
| 642 |
instructionErr( null, e.toString(), e.getStackTrace(), 27 ); |
| 643 |
} |
| 644 |
} |
| 645 |
if( scriptFile == null ){ |
| 646 |
scriptFile = "xtpipes-default.4xt"; |
| 647 |
} |
| 648 |
|
| 649 |
while( needScript ){ |
| 650 |
if( scriptFile == null ){ |
| 651 |
instructionErr( null, "Missing 4xt script file name", 32 ); |
| 652 |
} |
| 653 |
String f = FileInfo.searchFile( scriptFile ); |
| 654 |
if( f == null ){ |
| 655 |
throw new java.io.FileNotFoundException( scriptFile ); |
| 656 |
} else { |
| 657 |
scriptFile = f; |
| 658 |
} |
| 659 |
|
| 660 |
Document script = validatingDomBuilder.parse(scriptFile); |
| 661 |
if( trace ){ |
| 662 |
logWriter.println( "(" + scriptFile + ")" ); |
| 663 |
} |
| 664 |
|
| 665 |
execute( script.getFirstChild() ); |
| 666 |
} |
| 667 |
if( outFileName != null ){ |
| 668 |
outPrintWriter.close(); |
| 669 |
} |
| 670 |
|
| 671 |
Xtpipes.logWriter.flush(); |
| 672 |
} catch( org.xml.sax.SAXParseException e ){ |
| 673 |
String s = "Improper file " + scriptFile + ": " + e.getMessage(); |
| 674 |
instructionErr( null, s, 2 ); |
| 675 |
} catch( java.io.FileNotFoundException e ){ |
| 676 |
String s; |
| 677 |
if( scriptFile.equals( e.getMessage() ) ){ |
| 678 |
s = "Could not find file: " + scriptFile; |
| 679 |
} else { |
| 680 |
s = "Problem at script " + scriptFile + ": Could not find file " |
| 681 |
+ e.getMessage(); |
| 682 |
} |
| 683 |
instructionErr( null, s, 3 ); |
| 684 |
} catch( Exception e ){ |
| 685 |
String s = "Problems at file: " + scriptFile + "\n " + e; |
| 686 |
instructionErr( null, s, 4 ); |
| 687 |
} } |
| 688 |
|
| 689 |
private static void execute( Node node ) throws Exception { |
| 690 |
while( node != null ){ |
| 691 |
if( node.getNodeType()==Node.ELEMENT_NODE ){ |
| 692 |
String instruction = node.getNodeName(); |
| 693 |
if( trace ){ |
| 694 |
logWriter.print( "[##] = xtpipes => " + instruction ); |
| 695 |
if( node.hasAttributes() ){ |
| 696 |
NamedNodeMap attributes = node.getAttributes(); |
| 697 |
for(int i=0; i < attributes.getLength(); i++ ){ |
| 698 |
Node attr = attributes.item(i); |
| 699 |
logWriter.print( " " + attr.getNodeName() |
| 700 |
+ "=\"" + attr.getNodeValue() + "\"" ); |
| 701 |
} } |
| 702 |
logWriter.println(); logWriter.flush(); |
| 703 |
} |
| 704 |
|
| 705 |
if( instruction.equals( "xtpipes" ) ){ |
| 706 |
// String errMsg = ""; |
| 707 |
needScript = false; |
| 708 |
if( node.hasChildNodes() ){ |
| 709 |
if( outFileName != null ){ |
| 710 |
try { |
| 711 |
FileWriter fw = new FileWriter( outFileName ); |
| 712 |
outPrintWriter = new XtpipesPrintWriter( fw ); |
| 713 |
returnToFile = true; |
| 714 |
} catch(Exception e){ |
| 715 |
instructionErr( null, e.toString(), 12 ); |
| 716 |
} } |
| 717 |
|
| 718 |
if( node.hasAttributes() ){ |
| 719 |
Node attr = node.getAttributes() |
| 720 |
.getNamedItem( "signature" ); |
| 721 |
if ( (attr != null) && messages ) { |
| 722 |
logWriter.println( attr.getNodeValue() ); |
| 723 |
} |
| 724 |
|
| 725 |
attr = node.getAttributes() |
| 726 |
.getNamedItem( "preamble" ); |
| 727 |
if( (attr != null) |
| 728 |
&& attr.getNodeValue().equals( "yes" ) ){ |
| 729 |
// BufferedReader br = null; |
| 730 |
try { |
| 731 |
String s; |
| 732 |
boolean front = true; |
| 733 |
rootName = "<" + ((rootName==null)? inputObject.getRoot() : rootName); |
| 734 |
if( inData == null ){ |
| 735 |
// FileReader fr = new FileReader(inFile); |
| 736 |
// BufferedReader in = new BufferedReader(fr); |
| 737 |
URLConnection connection = |
| 738 |
new URL(inFile).openConnection(); |
| 739 |
connection.setRequestProperty("User-Agent", |
| 740 |
"[" |
| 741 |
+ System.getProperty("os.name") |
| 742 |
+ " / " |
| 743 |
+ System.getProperty("os.arch") |
| 744 |
+ "]" |
| 745 |
+ "[" |
| 746 |
+ System.getProperty("java.version") |
| 747 |
+ " - " |
| 748 |
+ System.getProperty("java.vendor") |
| 749 |
+ "]" |
| 750 |
); |
| 751 |
InputStream inputStream = connection.getInputStream(); |
| 752 |
BufferedReader in = new BufferedReader ( |
| 753 |
new InputStreamReader ( inputStream ) ); |
| 754 |
|
| 755 |
while ( ((s = in.readLine()) != null) && front ) { |
| 756 |
int i = s.indexOf( rootName ); |
| 757 |
if( i > -1 ){ |
| 758 |
front = false; |
| 759 |
s = s.substring(0,i); |
| 760 |
} |
| 761 |
outPrintWriter.println(s); |
| 762 |
returnToFile = false; |
| 763 |
} |
| 764 |
in.close(); |
| 765 |
|
| 766 |
} else { |
| 767 |
int i = inData.indexOf( rootName ); |
| 768 |
if( i > -1 ){ |
| 769 |
front = false; |
| 770 |
s = inData.substring(0,i); |
| 771 |
} else { s = ""; } |
| 772 |
outPrintWriter.println(s); |
| 773 |
returnToFile = false; |
| 774 |
|
| 775 |
} |
| 776 |
} catch (Exception e) { |
| 777 |
System.err.println( |
| 778 |
"--- Error --- Couldn't copy preamble: " + e); |
| 779 |
} |
| 780 |
|
| 781 |
} } |
| 782 |
|
| 783 |
execute( node.getFirstChild() ); |
| 784 |
} else { |
| 785 |
if( inData == null ){ |
| 786 |
/* |
| 787 |
errMsg = "Searching <?xtpipes file=\"...\"?> in " |
| 788 |
+ inFile + ": "; |
| 789 |
*/ |
| 790 |
scriptFile = inputObject.getXtpipes(); |
| 791 |
rootName = inputObject.getRoot(); |
| 792 |
needScript = true; |
| 793 |
|
| 794 |
} else { |
| 795 |
scriptFile = inputObject.getXtpipes(); |
| 796 |
rootName = inputObject.getRoot(); |
| 797 |
needScript = true; |
| 798 |
|
| 799 |
} |
| 800 |
|
| 801 |
} |
| 802 |
|
| 803 |
} else if( instruction.equals( "set" ) ){ |
| 804 |
String name = node.getAttributes().getNamedItem( "name" ) |
| 805 |
.getNodeValue(); |
| 806 |
Node cdata = node.getFirstChild(); |
| 807 |
while( cdata.getNodeType() != Node.CDATA_SECTION_NODE ){ |
| 808 |
cdata = cdata.getNextSibling(); |
| 809 |
} |
| 810 |
String code = cdata.getNodeValue().trim(); |
| 811 |
map.put( name, (Object) code ); |
| 812 |
|
| 813 |
} else if( instruction.equals( "get" ) ){ |
| 814 |
try { |
| 815 |
String name = node.getAttributes() |
| 816 |
.getNamedItem( "name" ).getNodeValue(); |
| 817 |
String file = node.getAttributes() |
| 818 |
.getNamedItem( "file" ).getNodeValue(); |
| 819 |
StreamSource in = new StreamSource( new File(file) ); |
| 820 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 821 |
identityTransformer.transform( in, new StreamResult(baos) ); |
| 822 |
byte [] bytes = baos.toByteArray(); |
| 823 |
map.put( name, (Object) new String(bytes) ); |
| 824 |
} catch( Exception e ){ |
| 825 |
instructionErr( node, e.toString(), 14 ); |
| 826 |
} |
| 827 |
|
| 828 |
} else if( instruction.equals( "print" ) ){ |
| 829 |
String name = node.getAttributes() |
| 830 |
.getNamedItem( "name" ).getNodeValue(); |
| 831 |
String xml = (String) map.get(name); |
| 832 |
if( node.getAttributes().getNamedItem( "file" )==null ){ |
| 833 |
if( !Xtpipes.trace ){ |
| 834 |
logWriter.println( "[##] = xtpipes => print: " + scriptFile ); |
| 835 |
} |
| 836 |
logWriter.println( XtpipesUni.toUni(xml, "") ); |
| 837 |
} else { |
| 838 |
String file = node.getAttributes() |
| 839 |
.getNamedItem( "file" ).getNodeValue(); |
| 840 |
try{ |
| 841 |
FileWriter fw = new FileWriter( file ); |
| 842 |
XtpipesPrintWriter out = new XtpipesPrintWriter( fw ); |
| 843 |
out.println( xml ); |
| 844 |
out.close(); |
| 845 |
} catch(Exception e){ |
| 846 |
instructionErr( node, e.toString(),15 ); |
| 847 |
} } |
| 848 |
|
| 849 |
} else if( instruction.equals( "return" ) ){ |
| 850 |
String name = node.getAttributes() |
| 851 |
.getNamedItem( "name" ).getNodeValue(); |
| 852 |
result = (String) map.get(name); |
| 853 |
if( returnToFile ){ |
| 854 |
outPrintWriter.println(result); |
| 855 |
} |
| 856 |
|
| 857 |
} else if( instruction.equals( "if" ) ){ |
| 858 |
try{ |
| 859 |
String xml = node.getAttributes() |
| 860 |
.getNamedItem( "xml" ).getNodeValue(); |
| 861 |
String dtd = node.getAttributes() |
| 862 |
.getNamedItem( "dtd" ).getNodeValue(); |
| 863 |
// String root = node.getAttributes() |
| 864 |
// .getNamedItem( "root" ).getNodeValue(); |
| 865 |
String doc = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" |
| 866 |
+ "<!DOCTYPE doc [\n" |
| 867 |
+ (String) map.get(dtd) |
| 868 |
+ "\n]>\n" |
| 869 |
+ (String) map.get(xml) ; |
| 870 |
|
| 871 |
byte [] bytes = doc.getBytes("UTF-8"); |
| 872 |
ByteArrayInputStream bais = new ByteArrayInputStream( bytes ); |
| 873 |
InputSource in = new InputSource( bais ); |
| 874 |
SAXParser saxParser = saxFactory.newSAXParser(); |
| 875 |
XMLReader xmlReader = saxParser.getXMLReader(); |
| 876 |
xmlReader.parse( in ); |
| 877 |
if( trace ){ |
| 878 |
logWriter.print( "--> true" ); |
| 879 |
} |
| 880 |
|
| 881 |
execute( node.getFirstChild() ); |
| 882 |
} catch ( Exception e ){ if( trace ){ |
| 883 |
logWriter.print( "--> true" ); |
| 884 |
} |
| 885 |
} |
| 886 |
|
| 887 |
} else if( instruction.equals( "xslt" ) ){ |
| 888 |
try{ |
| 889 |
Node xmlNode = node.getAttributes().getNamedItem( "xml" ); |
| 890 |
StreamSource inDoc = null; |
| 891 |
String xml; |
| 892 |
if( xmlNode == null ){ |
| 893 |
if( inData == null ){ |
| 894 |
inDoc = new StreamSource( new File(inFile) ); |
| 895 |
} else { |
| 896 |
byte [] bytes = inData.getBytes("UTF-8"); |
| 897 |
ByteArrayInputStream bais = new ByteArrayInputStream( bytes ); |
| 898 |
inDoc = new StreamSource( bais ); |
| 899 |
} |
| 900 |
|
| 901 |
} else { |
| 902 |
xml = xmlNode.getNodeValue(); |
| 903 |
String doc = (String) map.get(xml); |
| 904 |
if( doc!=null ){ |
| 905 |
byte [] bytes = doc.getBytes("UTF-8"); |
| 906 |
ByteArrayInputStream bais = new ByteArrayInputStream( bytes ); |
| 907 |
inDoc = new StreamSource( bais ); |
| 908 |
} } |
| 909 |
|
| 910 |
String xslt = node.getAttributes() |
| 911 |
.getNamedItem( "xsl" ).getNodeValue(); |
| 912 |
String templates = (String) map.get(xslt); |
| 913 |
byte [] bytes = templates.getBytes("UTF-8"); |
| 914 |
ByteArrayInputStream bais = new ByteArrayInputStream( bytes ); |
| 915 |
StreamSource inXslt = new StreamSource( bais ); |
| 916 |
|
| 917 |
Node nameNode = node.getAttributes().getNamedItem("name"); |
| 918 |
StreamResult outDoc; |
| 919 |
CharArrayWriter caos = null; |
| 920 |
if( nameNode == null ){ |
| 921 |
outDoc = new StreamResult(outPrintWriter); |
| 922 |
returnToFile = false; |
| 923 |
} else { |
| 924 |
caos = new CharArrayWriter(); |
| 925 |
outDoc = new StreamResult(caos); |
| 926 |
} |
| 927 |
|
| 928 |
errMssg = null; |
| 929 |
fc.setErrorListener( new ErrorListener() { |
| 930 |
public void warning(TransformerException e) throws TransformerException { |
| 931 |
showSpecifics(e); |
| 932 |
} |
| 933 |
public void error(TransformerException e) throws TransformerException { |
| 934 |
showSpecifics(e); |
| 935 |
} |
| 936 |
public void fatalError(TransformerException e) throws TransformerException { |
| 937 |
showSpecifics(e); |
| 938 |
} |
| 939 |
void showSpecifics(TransformerException e) |
| 940 |
throws TransformerException{ |
| 941 |
String err = e.getMessage() ; |
| 942 |
String loc = e.getLocationAsString(); |
| 943 |
if( loc != null ){ err = loc + ": " + err; } |
| 944 |
err = "XSL stylesheet problem: " + err; |
| 945 |
if( errMssg == null ){ errMssg = err; } |
| 946 |
throw new TransformerException(err); |
| 947 |
} } |
| 948 |
); |
| 949 |
Transformer transformer = fc.newTransformer( inXslt ); |
| 950 |
transformer.setErrorListener( new ErrorListener() { |
| 951 |
public void warning(TransformerException e) throws TransformerException { |
| 952 |
showSpecifics(e); |
| 953 |
} |
| 954 |
public void error(TransformerException e) throws TransformerException { |
| 955 |
showSpecifics(e); |
| 956 |
} |
| 957 |
public void fatalError(TransformerException e) throws TransformerException { |
| 958 |
showSpecifics(e); |
| 959 |
} |
| 960 |
void showSpecifics(TransformerException e) |
| 961 |
throws TransformerException{ |
| 962 |
String err = e.getMessage() ; |
| 963 |
String loc = e.getLocationAsString(); |
| 964 |
if( loc != null ){ err = loc + ": " + err; } |
| 965 |
if( errMssg == null ){ errMssg = err; } |
| 966 |
err = "XML document prblem: " + err; |
| 967 |
throw new TransformerException(err); |
| 968 |
} } |
| 969 |
); |
| 970 |
transformer.transform(inDoc, outDoc ); |
| 971 |
if( nameNode != null ){ |
| 972 |
String name = nameNode.getNodeValue(); |
| 973 |
char [] chars = caos.toCharArray() ; |
| 974 |
map.put( name, (Object) new String(chars) ); |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
} catch ( javax.xml.transform.TransformerException e ){ |
| 979 |
if( Xtpipes.trace ){ e.printStackTrace(); } |
| 980 |
instructionErr( node, |
| 981 |
e.getMessage() |
| 982 |
+ |
| 983 |
((errMssg==null)? "" : ("; " +errMssg)) |
| 984 |
, 37); |
| 985 |
} catch ( Exception e ){ |
| 986 |
if( Xtpipes.trace ){ e.printStackTrace(); } |
| 987 |
instructionErr( node, (errMssg==null)? e.toString() |
| 988 |
: e.toString() + "; " + errMssg |
| 989 |
, 16 ); |
| 990 |
} |
| 991 |
|
| 992 |
} else if( instruction.equals( "dom" ) ){ |
| 993 |
try{ |
| 994 |
Node xmlNode = node.getAttributes().getNamedItem( "xml" ); |
| 995 |
Document dom; |
| 996 |
if( xmlNode == null ){ |
| 997 |
if( inData == null ){ |
| 998 |
dom = domBuilder.parse( new File(inFile) ); |
| 999 |
} else { |
| 1000 |
byte [] bytes = inData.getBytes("UTF-8"); |
| 1001 |
InputStream is = new ByteArrayInputStream( bytes ); |
| 1002 |
dom = domBuilder.parse (is);} |
| 1003 |
|
| 1004 |
} else { |
| 1005 |
String xml = xmlNode.getNodeValue(); |
| 1006 |
String doc = (String) map.get(xml); |
| 1007 |
if( doc == null ){ |
| 1008 |
instructionErr( node, "improper xml attribute value", 18 ); |
| 1009 |
} |
| 1010 |
byte [] bytes = doc.getBytes("UTF-8"); |
| 1011 |
InputStream is = new ByteArrayInputStream( bytes ); |
| 1012 |
dom = domBuilder.parse (is); |
| 1013 |
} |
| 1014 |
|
| 1015 |
String className = node.getAttributes() |
| 1016 |
.getNamedItem( "class" ).getNodeValue(); |
| 1017 |
String methodName = node.getAttributes() |
| 1018 |
.getNamedItem( "method" ).getNodeValue(); |
| 1019 |
Class <?> cls = Class.forName( className ); |
| 1020 |
Class<?> [] argTypes = { Node.class }; |
| 1021 |
Method m = cls.getMethod( methodName, argTypes ); |
| 1022 |
Object parmValues[] = new Object[1]; |
| 1023 |
parmValues[0] = dom; |
| 1024 |
m.invoke( null, parmValues ); |
| 1025 |
|
| 1026 |
Node nameNode = node.getAttributes().getNamedItem("name"); |
| 1027 |
StreamResult outDoc; |
| 1028 |
CharArrayWriter caos = null; |
| 1029 |
if( nameNode == null ){ |
| 1030 |
outDoc = new StreamResult(outPrintWriter); |
| 1031 |
returnToFile = false; |
| 1032 |
} else { |
| 1033 |
caos = new CharArrayWriter(); |
| 1034 |
outDoc = new StreamResult(caos); |
| 1035 |
} |
| 1036 |
|
| 1037 |
cleanXmlns(dom); |
| 1038 |
DOMSource domSource = new DOMSource(dom); |
| 1039 |
try{ |
| 1040 |
identityTransformer.transform( domSource, outDoc ); |
| 1041 |
} catch ( javax.xml.transform.TransformerException e ){ |
| 1042 |
String s = Xtpipes.trace? |
| 1043 |
( |
| 1044 |
"\n------------------------ xml code ------------------------\n" |
| 1045 |
+ serialize( dom ) |
| 1046 |
+ "\n----------------------------------------------------------\n" |
| 1047 |
) |
| 1048 |
: ""; |
| 1049 |
instructionErr( node, e.getMessage() + s, 35 ); |
| 1050 |
} |
| 1051 |
if( nameNode != null ){ |
| 1052 |
String name = nameNode.getNodeValue(); |
| 1053 |
char [] chars = caos.toCharArray() ; |
| 1054 |
String domString = new String(chars); |
| 1055 |
Node dcl = node.getAttributes().getNamedItem( "dcl" ); |
| 1056 |
if( ((dcl == null) || (dcl.getNodeValue().equals("no") )) |
| 1057 |
&& |
| 1058 |
(domString.length() > 7) |
| 1059 |
&& |
| 1060 |
domString.startsWith("<?xml") |
| 1061 |
&& |
| 1062 |
!Character.isLetterOrDigit( domString.charAt(5) ) |
| 1063 |
){ |
| 1064 |
domString = domString.substring( domString.indexOf("?>") + 2 ); |
| 1065 |
} |
| 1066 |
|
| 1067 |
map.put( name, (Object) domString ); |
| 1068 |
} |
| 1069 |
|
| 1070 |
} catch ( NoSuchMethodException e ){ |
| 1071 |
instructionErr( node, |
| 1072 |
"could not find method: " + e.getMessage(), 18 ); |
| 1073 |
} catch ( java.lang.reflect.InvocationTargetException e ){ |
| 1074 |
if( Xtpipes.trace ){ e.printStackTrace(); } |
| 1075 |
instructionErr( node, e.getCause().toString(), 36); |
| 1076 |
} catch ( Exception e ){ |
| 1077 |
if( Xtpipes.trace ){ e.printStackTrace(); } |
| 1078 |
instructionErr( node, e.toString(), 20 ); |
| 1079 |
} |
| 1080 |
|
| 1081 |
} else if( instruction.equals( "sax" ) ){ |
| 1082 |
String errMsg = ""; |
| 1083 |
try{ |
| 1084 |
Node xmlNode = node.getAttributes().getNamedItem( "xml" ); |
| 1085 |
InputSource inputSource=null; |
| 1086 |
String xml = null; |
| 1087 |
if( xmlNode == null ){ |
| 1088 |
if( inData == null ){ |
| 1089 |
xml = inFile; |
| 1090 |
} else { |
| 1091 |
byte [] bytes = inData.getBytes("UTF-8"); |
| 1092 |
ByteArrayInputStream bais = new ByteArrayInputStream( bytes ); |
| 1093 |
inputSource = new InputSource( bais ); |
| 1094 |
} |
| 1095 |
|
| 1096 |
} else { |
| 1097 |
xml = xmlNode.getNodeValue(); |
| 1098 |
String doc = (String) map.get(xml); |
| 1099 |
if( doc!=null ){ |
| 1100 |
byte [] bytes = doc.getBytes("UTF-8"); |
| 1101 |
ByteArrayInputStream bais = new ByteArrayInputStream( bytes ); |
| 1102 |
inputSource = new InputSource( bais ); |
| 1103 |
} } |
| 1104 |
|
| 1105 |
String [] className = node.getAttributes() |
| 1106 |
.getNamedItem( "content-handler" ) |
| 1107 |
.getNodeValue() |
| 1108 |
.split(","); |
| 1109 |
|
| 1110 |
Class<?> [] argTypes = { |
| 1111 |
PrintWriter.class, HashMap.class, Method.class, |
| 1112 |
PrintWriter.class, boolean.class }; |
| 1113 |
Node nameNode = node.getAttributes().getNamedItem("name"); |
| 1114 |
PrintWriter out; |
| 1115 |
CharArrayWriter caos = null; |
| 1116 |
if( nameNode == null ){ |
| 1117 |
out = outPrintWriter; |
| 1118 |
returnToFile = false; |
| 1119 |
} else { |
| 1120 |
caos = new CharArrayWriter(); |
| 1121 |
out = new PrintWriter( caos ); |
| 1122 |
} |
| 1123 |
|
| 1124 |
Object parmValues[] = new Object[5]; |
| 1125 |
parmValues[0] = out; |
| 1126 |
HashMap <String,Object> scripts = new HashMap <String,Object> (); |
| 1127 |
Node script = node.getFirstChild(); |
| 1128 |
while( script != null ){ |
| 1129 |
if( script.getNodeType()==Node.ELEMENT_NODE ){ |
| 1130 |
String element = script.getAttributes().getNamedItem( "element" ) |
| 1131 |
.getNodeValue(); |
| 1132 |
if( scripts.containsKey(element) ){ |
| 1133 |
System.err.println( |
| 1134 |
"--- Warning --- redfining script: " + element ); |
| 1135 |
} |
| 1136 |
scripts.put( element, (Object) script ); |
| 1137 |
} |
| 1138 |
script = script.getNextSibling(); |
| 1139 |
} |
| 1140 |
parmValues[1] = scripts; |
| 1141 |
parmValues[2] = method; |
| 1142 |
parmValues[3] = Xtpipes.logWriter; |
| 1143 |
parmValues[4] = (Object) Xtpipes.trace; |
| 1144 |
Class<?> cls = Class.forName( className[0].trim() ); |
| 1145 |
Constructor<?> c = cls.getConstructor( argTypes ); |
| 1146 |
Object ch = (Object) c.newInstance( parmValues ); |
| 1147 |
|
| 1148 |
XMLReader saxReader; |
| 1149 |
if( saxReaderStack.empty() ){ |
| 1150 |
SAXParser saxParser = saxFactory.newSAXParser(); |
| 1151 |
saxReader = saxParser.getXMLReader(); |
| 1152 |
saxReader.setEntityResolver(new org.xml.sax.EntityResolver() { |
| 1153 |
public InputSource resolveEntity( |
| 1154 |
String publicId, String systemId) { |
| 1155 |
if( (new File(systemId)).exists() ){ |
| 1156 |
return new org.xml.sax.InputSource( systemId ); |
| 1157 |
} |
| 1158 |
StringReader strReader = new StringReader(""); |
| 1159 |
return new org.xml.sax.InputSource(strReader); |
| 1160 |
} |
| 1161 |
}); |
| 1162 |
|
| 1163 |
} else { |
| 1164 |
saxReader = (XMLReader) saxReaderStack.pop(); |
| 1165 |
} |
| 1166 |
|
| 1167 |
XMLReader reader = saxReader; |
| 1168 |
for( int i=1; i<className.length; i++ ){ |
| 1169 |
argTypes = new Class [3]; |
| 1170 |
argTypes[0] = PrintWriter.class; |
| 1171 |
argTypes[1] = PrintWriter.class; |
| 1172 |
argTypes[2] = boolean.class; |
| 1173 |
parmValues = new Object[3]; |
| 1174 |
parmValues[0] = out; |
| 1175 |
parmValues[1] = Xtpipes.logWriter; |
| 1176 |
parmValues[2] = (Object) Xtpipes.trace; |
| 1177 |
errMsg = "Class.forName( " + className[i].trim() + ") " ; |
| 1178 |
cls = Class.forName( className[i].trim() ); |
| 1179 |
errMsg = "get-constructor " |
| 1180 |
+ className[i].trim() |
| 1181 |
+ "( PrintWriter, PrintWriter, boolean ) " ; |
| 1182 |
c = cls.getConstructor( argTypes ); |
| 1183 |
errMsg = "get-object " |
| 1184 |
+ className[i].trim() |
| 1185 |
+ "( PrintWriter, PrintWriter, boolean ) " ; |
| 1186 |
if( (cls.getModifiers() % 2) != 1 ){ |
| 1187 |
errMsg += "; class not defined to be public. "; |
| 1188 |
} |
| 1189 |
XMLFilter filter = (XMLFilter) c.newInstance( parmValues ); |
| 1190 |
errMsg = "set-parent " |
| 1191 |
+ className[i].trim() |
| 1192 |
+ "( PrintWriter, PrintWriter, boolean ) " ; |
| 1193 |
filter.setParent(saxReader); |
| 1194 |
saxReader = filter; |
| 1195 |
} |
| 1196 |
|
| 1197 |
errMsg = "setContentHandler( " |
| 1198 |
+ className[0].trim() + " )"; |
| 1199 |
saxReader.setContentHandler( (org.xml.sax.ContentHandler) ch ); |
| 1200 |
Node lexAttr = node.getAttributes() |
| 1201 |
.getNamedItem( "lexical-handler" ); |
| 1202 |
if( lexAttr != null ){ |
| 1203 |
String lexName = lexAttr.getNodeValue(); |
| 1204 |
argTypes = new Class[3]; |
| 1205 |
argTypes[0] = Class.forName( className[0].trim() ); |
| 1206 |
argTypes[1] = PrintWriter.class; |
| 1207 |
argTypes[2] = boolean.class; |
| 1208 |
parmValues = new Object[3]; |
| 1209 |
parmValues[0] = ch; |
| 1210 |
parmValues[1] = Xtpipes.logWriter; |
| 1211 |
parmValues[2] = (Object) Xtpipes.trace; |
| 1212 |
errMsg = "Class.forName( " + lexName.trim() + ") " ; |
| 1213 |
cls = Class.forName( lexName.trim() ); |
| 1214 |
errMsg = "get-constructor " + |
| 1215 |
lexName.trim() + |
| 1216 |
"( " + className[0].trim() + " ) " ; |
| 1217 |
c = cls.getConstructor( argTypes ); |
| 1218 |
errMsg = "get-object " + |
| 1219 |
lexName.trim() + "( ... ) " ; |
| 1220 |
Object xh = (Object) c.newInstance( parmValues ); |
| 1221 |
errMsg = "set lexical handler " + lexName.trim() + " "; |
| 1222 |
saxReader.setProperty( |
| 1223 |
"http://xml.org/sax/properties/lexical-handler", |
| 1224 |
(org.xml.sax.ext.LexicalHandler) xh |
| 1225 |
); |
| 1226 |
} |
| 1227 |
|
| 1228 |
saxReader.setEntityResolver(new org.xml.sax.EntityResolver() { |
| 1229 |
public InputSource resolveEntity( |
| 1230 |
String publicId, String systemId) { |
| 1231 |
if( (new File(systemId)).exists() ){ |
| 1232 |
return new org.xml.sax.InputSource( systemId ); |
| 1233 |
} |
| 1234 |
StringReader strReader = new StringReader(""); |
| 1235 |
return new org.xml.sax.InputSource(strReader); |
| 1236 |
} |
| 1237 |
}); |
| 1238 |
|
| 1239 |
if( inputSource==null ){ |
| 1240 |
errMsg = "While parsing file " + xml + ": "; |
| 1241 |
InputStream inputStream = null; |
| 1242 |
if( Xtpipes.ml2xml == null ){ |
| 1243 |
if( Xtpipes.trace ){ |
| 1244 |
Xtpipes.logWriter.println( |
| 1245 |
"No request for ml2xml configuration (command line option -x)" ); |
| 1246 |
} |
| 1247 |
try{ |
| 1248 |
inputStream = (InputStream) (new File(xml).toURI().toURL().openStream()); |
| 1249 |
} catch ( java.io.FileNotFoundException ioe ){ |
| 1250 |
try{ |
| 1251 |
URL url = null; |
| 1252 |
try { |
| 1253 |
url = new URL(xml); |
| 1254 |
} catch ( java.net.MalformedURLException fnf ){ |
| 1255 |
url = new File(xml).toURI().toURL(); |
| 1256 |
} |
| 1257 |
inputStream = (InputStream) (url.openStream()); |
| 1258 |
} catch ( java.io.FileNotFoundException fnf ){ |
| 1259 |
inputStream = (InputStream) |
| 1260 |
( |
| 1261 |
new File( new File(xml).toURI().toURL().toString() ) |
| 1262 |
. toURI().toURL() |
| 1263 |
. openStream() |
| 1264 |
); |
| 1265 |
} } |
| 1266 |
|
| 1267 |
} else { |
| 1268 |
try{ |
| 1269 |
ml2xmlClassObj = Class.forName( "ml2xml.Ml2xml" ); |
| 1270 |
} catch (java.lang.ClassNotFoundException cnf ){ |
| 1271 |
instructionErr( null, "Class not found: ml2xml.Ml2xml", 25 ); |
| 1272 |
} |
| 1273 |
Class<?> [] argTyp = { String.class, String[].class }; |
| 1274 |
Constructor<?> con = ml2xmlClassObj.getConstructor( argTyp ); |
| 1275 |
try{ |
| 1276 |
if( Xtpipes.trace ){ |
| 1277 |
String s = "Calling: ml2xml.Ml2xml(inputStream," |
| 1278 |
+ "new String[]{" ; |
| 1279 |
for(int i=0; i < Xtpipes.ml2xml.length; i++){ |
| 1280 |
s += ((i==0)? "\"" : ", \"") + Xtpipes.ml2xml[i] + "\""; |
| 1281 |
} |
| 1282 |
s += "})"; |
| 1283 |
Xtpipes.logWriter.println( s ); |
| 1284 |
} |
| 1285 |
|
| 1286 |
inputStream = (InputStream) con.newInstance( |
| 1287 |
new Object[]{xml, ml2xml} |
| 1288 |
); |
| 1289 |
} catch(java.lang.reflect.InvocationTargetException ite){ |
| 1290 |
String s = "Problem at: ml2xml.Ml2xml(" + xml + "," |
| 1291 |
+ "new String[]{" ; |
| 1292 |
for(int i=0; i < Xtpipes.ml2xml.length; i++){ |
| 1293 |
s += ((i==0)? "\"" : ", \"") + Xtpipes.ml2xml[i] + "\""; |
| 1294 |
} |
| 1295 |
s += "})"; |
| 1296 |
instructionErr( null, s + "; " + ite.getCause(), 38); |
| 1297 |
} |
| 1298 |
|
| 1299 |
} |
| 1300 |
saxReader.parse( new InputSource(inputStream) ); |
| 1301 |
if( Xtpipes.ml2xml != null ){ |
| 1302 |
Class<?> [] argTyp = {}; |
| 1303 |
Method m = ml2xmlClassObj . getMethod( "closeFiles", argTyp ); |
| 1304 |
m.invoke( null, new Object[0] ); |
| 1305 |
} |
| 1306 |
|
| 1307 |
|
| 1308 |
} else { |
| 1309 |
errMsg = "xtpipes sax parsing error"; |
| 1310 |
saxReader.parse( inputSource ); |
| 1311 |
} |
| 1312 |
if( nameNode != null ){ |
| 1313 |
String name = nameNode.getNodeValue(); |
| 1314 |
char [] chars = caos.toCharArray() ; |
| 1315 |
map.put( name, (Object) new String(chars) ); |
| 1316 |
} |
| 1317 |
|
| 1318 |
saxReaderStack.push( reader ); |
| 1319 |
} catch ( java.io.FileNotFoundException e ){ |
| 1320 |
instructionErr( node, errMsg |
| 1321 |
+ "could not find file: " + e.getMessage(), 19 ); |
| 1322 |
} catch ( ClassNotFoundException e ){ |
| 1323 |
instructionErr( node, errMsg |
| 1324 |
+ " class not found: " |
| 1325 |
+ e.getMessage() + "\n classpath = " |
| 1326 |
+ System.getProperty("java.class.path") |
| 1327 |
+ " ---", 22 ); |
| 1328 |
} catch ( java.lang.reflect.InvocationTargetException e ){ |
| 1329 |
instructionErr( node, errMsg + ": " + e.getCause(), 23 ); |
| 1330 |
} catch ( Exception e ){ |
| 1331 |
Xtpipes.logWriter.flush(); |
| 1332 |
e.printStackTrace(); |
| 1333 |
instructionErr( node, errMsg + ": " + e.toString(), 29 ); |
| 1334 |
} |
| 1335 |
|
| 1336 |
} else { |
| 1337 |
instructionErr( node, "Improper instruction: " + instruction, 11 ); |
| 1338 |
} } |
| 1339 |
node = node.getNextSibling(); |
| 1340 |
} } |
| 1341 |
|
| 1342 |
public static String execute( Node node, String xml ) |
| 1343 |
throws Exception { |
| 1344 |
String name = "."; |
| 1345 |
String old = (String) map.get(name); |
| 1346 |
map.put( name, (Object) xml ); |
| 1347 |
execute( node.getFirstChild() ); |
| 1348 |
String s = (String) map.get(name); |
| 1349 |
if( old != null ){ map.put( name, (Object) old ); } |
| 1350 |
return s; |
| 1351 |
} |
| 1352 |
|
| 1353 |
private static void instructionErr( Node node, String e, int num ) |
| 1354 |
throws Exception { |
| 1355 |
String err = "--- xtpipes error " + num + " --- "; |
| 1356 |
if( node != null ){ |
| 1357 |
err += "At <" + node.getNodeName(); |
| 1358 |
NamedNodeMap attr = node.getAttributes(); |
| 1359 |
for(int i=0; i<attr.getLength(); i++){ |
| 1360 |
Node nd = attr.item(i); |
| 1361 |
err += " " + |
| 1362 |
nd.getNodeName() + "=\"" + |
| 1363 |
nd.getNodeValue() + "\"" ; |
| 1364 |
} |
| 1365 |
err += " > : " ; |
| 1366 |
} |
| 1367 |
err += e; |
| 1368 |
if( ml2xmlClassObj != null ){ |
| 1369 |
Class<?> [] argTypes = { }; |
| 1370 |
Method m = ml2xmlClassObj.getMethod( "closeFiles", argTypes ); |
| 1371 |
Object parmValues[] = new Object[0]; |
| 1372 |
m.invoke( null, parmValues ); |
| 1373 |
} |
| 1374 |
|
| 1375 |
Xtpipes.logWriter.flush(); |
| 1376 |
if( exceptionErrs ) { throw new Exception( err ); } |
| 1377 |
else { |
| 1378 |
System.err.println( err ); |
| 1379 |
System.exit(1); |
| 1380 |
} |
| 1381 |
} |
| 1382 |
private static void instructionErr( Node node, String e, |
| 1383 |
StackTraceElement[] st, int num ) |
| 1384 |
throws Exception { |
| 1385 |
Xtpipes.logWriter.println( |
| 1386 |
"--- xtpipes error " + num + " --- " + e |
| 1387 |
); |
| 1388 |
for(int i=st.length-1; i>=0; i-- ){ |
| 1389 |
Xtpipes.logWriter.println( st[i].toString() ); |
| 1390 |
} |
| 1391 |
instructionErr( node, e, num ); |
| 1392 |
} |
| 1393 |
|
| 1394 |
static String serialize( Node root ){ |
| 1395 |
if( root.getNodeType() == Node.TEXT_NODE) { |
| 1396 |
return root.getNodeValue(); |
| 1397 |
} |
| 1398 |
if( root.getNodeType() == Node.ELEMENT_NODE) { |
| 1399 |
String ser = ""; |
| 1400 |
String tagName = root.getNodeName(); |
| 1401 |
ser += "<" + tagName; |
| 1402 |
NamedNodeMap attributes = root.getAttributes(); |
| 1403 |
for(int i = 0; i < attributes.getLength(); i++) { |
| 1404 |
Attr attribute = (Attr) attributes.item(i); |
| 1405 |
ser += "\n" + attribute.getName() + "=\"" |
| 1406 |
+ attribute.getValue() + "\" "; |
| 1407 |
} |
| 1408 |
|
| 1409 |
ser += "\n>"; |
| 1410 |
NodeList children = root.getChildNodes(); |
| 1411 |
if(children.getLength() > 0) { |
| 1412 |
for(int i = 0; i < children.getLength(); i++) { |
| 1413 |
ser += serialize(children.item(i)); |
| 1414 |
} } |
| 1415 |
|
| 1416 |
ser += "</" + tagName + ">"; |
| 1417 |
return ser; |
| 1418 |
} |
| 1419 |
if( root.getNodeType() == Node.DOCUMENT_NODE) { |
| 1420 |
String ser = ""; |
| 1421 |
NodeList children = root.getChildNodes(); |
| 1422 |
if(children.getLength() > 0) { |
| 1423 |
for(int i = 0; i < children.getLength(); i++) { |
| 1424 |
ser += serialize(children.item(i)); |
| 1425 |
} } |
| 1426 |
|
| 1427 |
return ser; |
| 1428 |
} |
| 1429 |
if( root == null ){ return "null"; } |
| 1430 |
return ""; |
| 1431 |
} |
| 1432 |
|
| 1433 |
static ArrayList<String> nsName, nsValue; |
| 1434 |
static void cleanXmlns( Node root ){ |
| 1435 |
if( root.getNodeType() == Node.ELEMENT_NODE) { |
| 1436 |
int top = nsName.size(); |
| 1437 |
ArrayList<Attr> remove = new ArrayList<Attr>(); |
| 1438 |
NamedNodeMap attributes = root.getAttributes(); |
| 1439 |
for(int i = 0; i < attributes.getLength(); i++) { |
| 1440 |
Attr attribute = (Attr) attributes.item(i); |
| 1441 |
String name = attribute.getName(); |
| 1442 |
if( name.startsWith("xmlns") ){ |
| 1443 |
if( (name.length() == 5) || (name.charAt(5) == ':') ){ |
| 1444 |
String value = attribute.getValue(); |
| 1445 |
boolean bool = false; |
| 1446 |
for(int k=nsName.size(); k>0; ){ |
| 1447 |
k--; |
| 1448 |
if( ((String) nsName.get(k)) . equals(name) ){ |
| 1449 |
bool = ((String) nsValue.get(k)) . equals(value); |
| 1450 |
break; |
| 1451 |
} } |
| 1452 |
|
| 1453 |
if( bool ){ remove.add(attribute); |
| 1454 |
} else { nsName.add(name); nsValue.add(value); } |
| 1455 |
} } } |
| 1456 |
for(int i=remove.size(); i>0; ){ |
| 1457 |
i--; |
| 1458 |
((Element) root).removeAttributeNode( (Attr) remove.get(i) ); |
| 1459 |
} |
| 1460 |
remove = null; |
| 1461 |
|
| 1462 |
NodeList children = root.getChildNodes(); |
| 1463 |
if(children.getLength() > 0) { |
| 1464 |
for(int i = 0; i < children.getLength(); i++) { |
| 1465 |
cleanXmlns(children.item(i)); |
| 1466 |
} } |
| 1467 |
|
| 1468 |
for(int i=nsName.size(); i>top; ){ |
| 1469 |
i--; |
| 1470 |
nsName.remove(i); |
| 1471 |
nsValue.remove(i); |
| 1472 |
} |
| 1473 |
} else if( root.getNodeType() == Node.DOCUMENT_NODE) { |
| 1474 |
nsName = new ArrayList<String>(); |
| 1475 |
nsValue = new ArrayList<String>(); |
| 1476 |
NodeList children = root.getChildNodes(); |
| 1477 |
if(children.getLength() > 0) { |
| 1478 |
for(int i = 0; i < children.getLength(); i++) { |
| 1479 |
cleanXmlns(children.item(i)); |
| 1480 |
} } |
| 1481 |
|
| 1482 |
nsName = null; |
| 1483 |
nsValue = null; |
| 1484 |
} } |
| 1485 |
|
| 1486 |
} |
| 1487 |
class XtpipesEntityResolver implements org.xml.sax.EntityResolver { |
| 1488 |
public InputSource resolveEntity(String publicID, String systemID) |
| 1489 |
throws SAXException { |
| 1490 |
if( Xtpipes.trace ){ |
| 1491 |
Xtpipes.logWriter.println( "Resolving: publicID = \" " + publicID |
| 1492 |
+ "\" systemID = \"" + systemID + "\"" ); |
| 1493 |
} |
| 1494 |
String file = FileInfo.searchFile( systemID ); |
| 1495 |
if( file != null ){ |
| 1496 |
try{ |
| 1497 |
file = new File(file).toURI().toURL().toString(); |
| 1498 |
return new InputSource( file ); |
| 1499 |
} catch( java.net.MalformedURLException mfe){ |
| 1500 |
throw new SAXException( |
| 1501 |
"--- xtpipes error 30 --- improper file name: " + file ); |
| 1502 |
} } |
| 1503 |
return null; |
| 1504 |
} |
| 1505 |
|
| 1506 |
} |
| 1507 |
|
| 1508 |
|