📄 pipelinefactory.java
字号:
+ "' stage of pipeline, " + message); } EventConsumer createStage (EventConsumer next) throws IOException { String name = id; // most builtins are just class aliases for (int i = 0; i < builtinStages.length; i++) { if (id.equals (builtinStages [i][0])) { name = builtinStages [i][1]; break; } } // Save output as XML or XHTML text if ("write".equals (name) || "xhtml".equals (name)) { String filename; boolean isXhtml = "xhtml".equals (name); OutputStream out = null; TextConsumer consumer; if (param == null) fail ("parameter is required"); filename = param.toString (); if ("stdout".equals (filename)) out = System.out; else if ("stderr".equals (filename)) out = System.err; else { File f = new File (filename);/* if (!f.isAbsolute ()) fail ("require absolute file paths"); */ if (f.exists ()) fail ("file already exists: " + f.getName ());// XXX this races against the existence test out = new FileOutputStream (f); } if (!isXhtml) consumer = new TextConsumer (out); else consumer = new TextConsumer ( new OutputStreamWriter (out, "8859_1"), true); consumer.setPrettyPrinting (true); if (next == null) return consumer; return new TeeConsumer (consumer, next); } else { // // Here go all the builtins that are just aliases for // classes, and all stage IDs that started out as such // class names. The following logic relies on several // documented conventions for constructor invocation. // String msg = null; try { Class klass = Class.forName (name); Class argTypes [] = null; Constructor constructor = null; boolean filter = false; Object params [] = null; Object obj = null; // do we need a filter stage? if (next != null) { // "next" consumer is always passed, with // or without the optional string param if (param == null) { argTypes = new Class [1]; argTypes [0] = EventConsumer.class; params = new Object [1]; params [0] = next; msg = "no-param filter"; } else { argTypes = new Class [2]; argTypes [0] = String.class; argTypes [1] = EventConsumer.class; params = new Object [2]; params [0] = param.toString (); params [1] = next; msg = "one-param filter"; } try { constructor = klass.getConstructor (argTypes); } catch (NoSuchMethodException e) { // try creating a filter from a // terminus and a tee filter = true; msg += " built from "; } } // build from a terminus stage, with or // without the optional string param if (constructor == null) { String tmp; if (param == null) { argTypes = new Class [0]; params = new Object [0]; tmp = "no-param terminus"; } else { argTypes = new Class [1]; argTypes [0] = String.class; params = new Object [1]; params [0] = param.toString (); tmp = "one-param terminus"; } if (msg == null) msg = tmp; else msg += tmp; constructor = klass.getConstructor (argTypes); // NOT creating terminus by dead-ending // filters ... users should think about // that one, something's likely wrong } obj = constructor.newInstance (params); // return EventConsumers directly, perhaps after // turning them into a filter if (obj instanceof EventConsumer) { if (filter) return new TeeConsumer ((EventConsumer) obj, next); return (EventConsumer) obj; } // if it's not a handler, it's an error // we can wrap handlers in a filter EventFilter retval = new EventFilter (); boolean updated = false; if (obj instanceof ContentHandler) { retval.setContentHandler ((ContentHandler) obj); updated = true; } if (obj instanceof DTDHandler) { retval.setDTDHandler ((DTDHandler) obj); updated = true; } if (obj instanceof LexicalHandler) { retval.setProperty ( EventFilter.PROPERTY_URI + "lexical-handler", obj); updated = true; } if (obj instanceof DeclHandler) { retval.setProperty ( EventFilter.PROPERTY_URI + "declaration-handler", obj); updated = true; } if (!updated) fail ("class is neither Consumer nor Handler"); if (filter) return new TeeConsumer (retval, next); return retval; } catch (IOException e) { throw e; } catch (NoSuchMethodException e) { fail (name + " constructor missing -- " + msg); } catch (ClassNotFoundException e) { fail (name + " class not found"); } catch (Exception e) { // e.printStackTrace (); fail ("stage not available: " + e.getMessage ()); } } // NOTREACHED return null; } } private static class Pipeline { Stage stage; // rest may be null Pipeline rest; EventConsumer next; Pipeline (Stage s) { stage = s; } public String toString () { if (rest == null && next == null) return stage.toString (); if (rest != null) return stage + " | " + rest; throw new IllegalArgumentException ("next"); } EventConsumer createPipeline () throws IOException { if (next == null) { if (rest == null) next = stage.createStage (null); else next = stage.createStage (rest.createPipeline ()); } return next; } }/* public static void main (String argv []) { try { // three basic terminus cases createPipeline ("null"); createPipeline ("validate"); createPipeline ("write ( stdout )"); // four basic filters createPipeline ("nsfix | write ( stderr )"); createPipeline ("wf | null"); createPipeline ("null | null"); createPipeline ("call ( http://www.example.com/services/xml-1a ) | xhtml ( stdout )"); // tee junctions createPipeline ("tee ( validate ) | write ( stdout )"); createPipeline ("tee ( nsfix | write ( stdout ) ) | validate"); // longer pipeline createPipeline ("nsfix | tee ( validate ) | write ( stdout )"); createPipeline ( "null | wf | nsfix | tee ( validate ) | write ( stdout )"); // try some parsing error cases try { createPipeline ("null ("); // extra token '(' System.err.println ("** didn't report error"); } catch (Exception e) { System.err.println ("== err: " + e.getMessage ()); } try { createPipeline ("nsfix |"); // extra token '|' System.err.println ("** didn't report error"); } catch (Exception e) { System.err.println ("== err: " + e.getMessage ()); } try { createPipeline ("xhtml ( foo"); // missing right paren System.err.println ("** didn't report error"); } catch (Exception e) { System.err.println ("== err: " + e.getMessage ()); } try { createPipeline ("xhtml ( foo bar"); // required right paren System.err.println ("** didn't report error"); } catch (Exception e) { System.err.println ("== err: " + e.getMessage ()); } try { createPipeline ("tee ( nsfix | validate");// missing right paren System.err.println ("** didn't report error"); } catch (Exception e) { System.err.println ("== err: " + e.getMessage ()); } // try some construction error cases try { createPipeline ("call"); // missing param System.err.println ("** didn't report error"); } catch (Exception e) { System.err.println ("== err: " + e.getMessage ()); } try { createPipeline ("call ( foobar )"); // broken param System.err.println ("** didn't report error"); } catch (Exception e) { System.err.println ("== err: " + e.getMessage ()); } try { createPipeline ("nsfix ( foobar )"); // illegal param System.err.println ("** didn't report error"); } catch (Exception e) { System.err.println ("== err: " + e.getMessage ()); } try { createPipeline ("null ( foobar )"); // illegal param System.err.println ("** didn't report error"); } catch (Exception e) { System.err.println ("== err: " + e.getMessage ()); } try { createPipeline ("wf ( foobar )"); // illegal param System.err.println ("** didn't report error"); } catch (Exception e) { System.err.println ("== err: " + e.getMessage ()); } try { createPipeline ("xhtml ( foobar.html )"); new File ("foobar.html").delete (); // now supported } catch (Exception e) { System.err.println ("** err: " + e.getMessage ()); } try { createPipeline ("xhtml"); // missing param System.err.println ("** didn't report error"); } catch (Exception e) { System.err.println ("== err: " + e.getMessage ()); } try { createPipeline ("write ( stdout ) | null"); // nonterminal System.err.println ("** didn't report error"); } catch (Exception e) { System.err.println ("== err: " + e.getMessage ()); } try { createPipeline ("validate | null"); // now supported } catch (Exception e) { System.err.println ("** err: " + e.getMessage ()); } try { createPipeline ("validate ( foo )"); // illegal param System.err.println ("** didn't report error"); } catch (Exception e) { System.err.println ("== err: " + e.getMessage ()); } try { createPipeline ("tee"); // missing param System.err.println ("** didn't report error"); } catch (Exception e) { System.err.println ("== err: " + e.getMessage ()); } try { // only builtins so far createPipeline ("com.example.xml.FilterClass"); System.err.println ("** didn't report error"); } catch (Exception e) { System.err.println ("== err: " + e.getMessage ()); } } catch (Exception e) { e.printStackTrace (); } }/**/}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -