KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > xslt > XSLT


1 // Copyright (c) 2002, 2003 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

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 /** New Kawa language XSLT (XML Stylesheet Language Tranformations). */
13
14 public class XSLT extends XQuery
15 {
16   // This field need to be public so that the findLiteral method in
17
// gnu.expr.LitTable can find it.
18
public static XSLT instance;
19
20   public String JavaDoc 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 JavaDoc 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     //parser.nesting = 1;
46
java.util.Vector JavaDoc exps = new java.util.Vector JavaDoc(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 JavaDoc("too many xpath expressions"); // FIXME
61
}
62     catch (Throwable JavaDoc ex)
63       {
64     ex.printStackTrace();
65     throw new InternalError JavaDoc ("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   /** Override {@code XQuery} implementation to get {@code Language} default.
75    */

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 JavaDoc, gnu.text.SyntaxException
83   {
84     Compilation.defaultCallConvention = Compilation.CALL_WITH_CONSUMER;
85     ((XslTranslator) comp.lexer).parse(comp);
86     return true;
87   }
88
89   /** The compiler insert calls to this method for applications and applets. */
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 JavaDoc 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 JavaDoc 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 JavaDoc
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 JavaDoc type = pos.getNextTypeObject();
139         Procedure proc = TemplateTable.nullModeTable.find(pos.getNextTypeName());
140         String JavaDoc name = pos.getNextTypeName();
141         if (proc != null)
142           {
143         proc.check0(ctx);
144         ctx.runUntilDone();
145           }
146         else
147           {
148         out.beginGroup(type);
149         // FIXME emit attributes
150
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 JavaDoc
177   {
178     CallContext ctx = CallContext.getInstance();
179     String JavaDoc[] args = kawa.repl.commandLineArgArray;
180     for (int i = 0; i < args.length; i++)
181       {
182     String JavaDoc 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 JavaDoc select, Symbol mode)
191     throws Throwable JavaDoc
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