1 4 package gnu.kawa.xslt; 5 import gnu.mapping.*; 6 import gnu.lists.*; 7 import gnu.expr.*; 8 import gnu.text.*; 9 import gnu.xquery.lang.*; 10 import gnu.kawa.xml.*; 11 12 13 14 public class XSLT extends XQuery 15 { 16 public static XSLT instance; 19 20 public String getName() 21 { 22 return "XSLT"; 23 } 24 25 public XSLT () 26 { 27 instance = this; 28 ModuleBody.setMainPrintValues(true); 29 } 30 31 public static XSLT getXsltInstance() 32 { 33 if (instance == null) 34 new XSLT (); 35 return instance; 36 } 37 38 Expression parseXPath (String string, SourceMessages messages) 39 { 40 try 41 { 42 Compilation tr = new Compilation(this, messages); 43 XQParser parser 44 = (XQParser) super.getLexer(new CharArrayInPort(string), messages); 45 java.util.Vector exps = new java.util.Vector (20); 47 for (;;) 48 { 49 Expression sexp = parser.parse(tr); 50 if (sexp == null) 51 break; 52 exps.addElement(sexp); 53 } 54 int nexps = exps.size(); 55 if (nexps == 0) 56 return QuoteExp.voidExp; 57 else if (nexps == 1) 58 return (Expression) exps.elementAt(0); 59 else 60 throw new InternalError ("too many xpath expressions"); } 62 catch (Throwable ex) 63 { 64 ex.printStackTrace(); 65 throw new InternalError ("caught "+ex); 66 } 67 } 68 69 public gnu.text.Lexer getLexer(InPort inp, gnu.text.SourceMessages messages) 70 { 71 return new XslTranslator(inp, messages, this); 72 } 73 74 76 public Compilation getCompilation (Lexer lexer, SourceMessages messages) 77 { 78 return new Compilation(this, messages); 79 } 80 81 public boolean parse (Compilation comp, int options) 82 throws java.io.IOException , gnu.text.SyntaxException 83 { 84 Compilation.defaultCallConvention = Compilation.CALL_WITH_CONSUMER; 85 ((XslTranslator) comp.lexer).parse(comp); 86 return true; 87 } 88 89 90 public static void registerEnvironment() 91 { 92 Language.setDefaults(new XSLT()); 93 } 94 95 public static void defineCallTemplate(Symbol name, 96 double priority, 97 Procedure template) 98 { 99 } 100 101 public static Symbol nullMode = Symbol.make(null, ""); 102 103 public static void defineApplyTemplate(String pattern, 104 double priority, 105 Symbol mode, 106 Procedure template) 107 { 108 if (mode == null) 109 mode = nullMode; 110 TemplateTable table = TemplateTable.getTemplateTable(mode); 111 table.enter(pattern, priority, template); 112 } 113 114 public static void defineTemplate(Symbol name, String pattern, 115 double priority, Symbol mode, 116 Procedure template) 117 { 118 if (name != null) 119 defineCallTemplate(name, priority, template); 120 if (pattern != null) 121 defineApplyTemplate(pattern, priority, mode, template); 122 } 123 124 public static void process(TreeList doc, Focus pos, CallContext ctx) 125 throws Throwable 126 { 127 Consumer out = ctx.consumer; 128 for (;;) 129 { 130 int ipos = pos.ipos; 131 int kind = doc.getNextKind(ipos); 132 switch (kind) 133 { 134 case Sequence.DOCUMENT_VALUE: 135 ipos = doc.firstChildPos(ipos); 136 break; 137 case Sequence.GROUP_VALUE: 138 Object type = pos.getNextTypeObject(); 139 Procedure proc = TemplateTable.nullModeTable.find(pos.getNextTypeName()); 140 String name = pos.getNextTypeName(); 141 if (proc != null) 142 { 143 proc.check0(ctx); 144 ctx.runUntilDone(); 145 } 146 else 147 { 148 out.beginGroup(type); 149 pos.push(doc, doc.firstChildPos(ipos)); 151 process(doc, pos, ctx); 152 pos.pop(); 153 out.endGroup(); 154 } 155 ipos = doc.nextDataIndex(ipos >>> 1) << 1; 156 pos.gotoNext(); 157 break; 158 case Sequence.CHAR_VALUE: 159 int ichild = ipos >>> 1; 160 int next = doc.nextNodeIndex(ichild, -1 >>> 1); 161 if (ipos == next) 162 next = doc.nextDataIndex(ichild); 163 doc.consumeIRange(ichild, next, out); 164 ipos = next << 1; 165 break; 166 case Sequence.TEXT_BYTE_VALUE: 167 case Sequence.OBJECT_VALUE: 168 default: 169 return; 170 } 171 pos.ipos = ipos; 172 } 173 } 174 175 public static void runStylesheet() 176 throws Throwable 177 { 178 CallContext ctx = CallContext.getInstance(); 179 String [] args = kawa.repl.commandLineArgArray; 180 for (int i = 0; i < args.length; i++) 181 { 182 String arg = args[i]; 183 KDocument doc = Document.parse(arg); 184 Focus pos = Focus.getCurrent(); 185 pos.push(doc.sequence, doc.ipos); 186 process((TreeList) doc.sequence, pos, ctx); 187 } 188 } 189 190 public static void applyTemplates(String select, Symbol mode) 191 throws Throwable 192 { 193 if (mode == null) 194 mode = nullMode; 195 TemplateTable table = TemplateTable.getTemplateTable(mode); 196 CallContext ctx = CallContext.getInstance(); 197 Focus pos = Focus.getCurrent(); 198 TreeList doc = (TreeList) pos.sequence; 199 pos.push(doc, doc.firstChildPos(pos.ipos)); 200 process(doc, pos, ctx); 201 pos.pop(); 202 } 203 } 204 | Popular Tags |