KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > kawa > xml > MakeElement


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

4 package gnu.kawa.xml;
5 import gnu.lists.*;
6 import gnu.mapping.*;
7 import gnu.bytecode.*;
8 import gnu.expr.*;
9 import gnu.xml.*;
10
11 public class MakeElement extends NodeConstructor
12 {
13   public static final MakeElement makeElement = new MakeElement();
14
15   public int numArgs() { return 0xFFFFF001; }
16
17   /** Optional tag. If non-null, the element tag is this value,
18    * rather than the first parameter. */

19   public Object JavaDoc tag;
20
21   public int copyNamespacesMode = XMLFilter.COPY_NAMESPACES_PRESERVE;
22
23   private boolean handlingKeywordParameters;
24
25   /** Should {@code KEYWORD: EXPRESSION} be mapped to an
26    * attribute constructor? */

27   public boolean isHandlingKeywordParameters ()
28   {
29     return handlingKeywordParameters;
30   }
31
32   public void setHandlingKeywordParameters (boolean value)
33   {
34     handlingKeywordParameters = value;
35   }
36
37   NamespaceBinding namespaceNodes;
38
39   public NamespaceBinding getNamespaceNodes ()
40   {
41     return namespaceNodes;
42   }
43
44   public void setNamespaceNodes (NamespaceBinding bindings)
45   {
46     namespaceNodes = bindings;
47   }
48
49   public static Symbol getTagName (ApplyExp exp)
50   {
51     Expression[] args = exp.getArgs();
52     if (args.length > 0)
53       {
54     Expression arg0 = args[0];
55     if (arg0 instanceof QuoteExp)
56       {
57         Object JavaDoc val = ((QuoteExp) arg0).getValue();
58         if (val instanceof Symbol)
59           return (Symbol) val;
60       }
61       }
62     return null;
63   }
64
65   public static void beginGroup(Consumer out, Object JavaDoc qname,
66                                 int copyNamespacesMode,
67                 NamespaceBinding namespaceNodes)
68   {
69     XName type;
70     if (qname instanceof Symbol)
71       type = new XName((Symbol) qname, namespaceNodes);
72     else
73       type = new XName(Symbol.make("", qname.toString(), ""), namespaceNodes);
74     if (out instanceof XMLFilter)
75       ((XMLFilter) out).copyNamespacesMode = copyNamespacesMode;
76     out.beginGroup(type);
77   }
78
79   public static void beginGroup(Consumer out, Object JavaDoc qname,
80                                 int copyNamespacesMode)
81   {
82     Symbol type;
83     if (qname instanceof Symbol)
84       type = (Symbol) qname;
85     else
86       type = Symbol.make("", qname.toString(), "");
87     if (out instanceof XMLFilter)
88       ((XMLFilter) out).copyNamespacesMode = copyNamespacesMode;
89     out.beginGroup(type);
90   }
91
92   public static void endGroup(Consumer out, Object JavaDoc type/*FIXME:unused*/)
93   {
94     out.endGroup();
95   }
96
97   public void apply (CallContext ctx)
98   {
99     Consumer saved = ctx.consumer;
100     Consumer out = pushNodeContext(ctx);
101     try
102       {
103     Object JavaDoc type = tag != null ? tag : ctx.getNextArg();
104     if (namespaceNodes != null)
105       beginGroup(out, type, copyNamespacesMode, namespaceNodes);
106     else
107       beginGroup(out, type, copyNamespacesMode);
108     Object JavaDoc endMarker = Special.dfault;
109     for (;;)
110       {
111         Object JavaDoc arg = ctx.getNextArg(endMarker);
112         if (arg == endMarker)
113           break;
114         if (arg instanceof Consumable)
115           ((Consumable) arg).consume(out);
116         else
117           ctx.writeValue(arg);
118             // Handling Keyword values is actually done by the Consumer.
119
if (isHandlingKeywordParameters())
120               out.endAttribute();
121       }
122     endGroup(out, type);
123       }
124     finally
125       {
126     popNodeContext(saved, ctx);
127       }
128   }
129
130   public void compileToNode (ApplyExp exp, Compilation comp,
131                  ConsumerTarget target)
132   {
133     Variable consumer = target.getConsumerVariable();
134     Expression[] args = exp.getArgs();
135     int nargs = args.length;
136     CodeAttr code = comp.getCode();
137     code.emitLoad(consumer);
138     code.emitDup();
139     int i;
140     if (tag == null)
141       {
142         args[0].compile(comp, Target.pushObject);
143         i = 1;
144       }
145     else
146       {
147         comp.compileConstant(tag, Target.pushObject);
148         i = 0;
149       }
150     code.emitDup(1, 1); // dup_x1
151
// Stack: consumer, tagtype, consumer, tagtype
152
code.emitPushInt(copyNamespacesMode);
153     if (namespaceNodes != null)
154       {
155     comp.compileConstant(namespaceNodes, Target.pushObject);
156     code.emitInvokeStatic(beginGroupMethod4);
157       }
158     else
159       code.emitInvokeStatic(beginGroupMethod3);
160     for (; i < nargs; i++)
161       {
162         compileChild(args[i], comp, target);
163         if (isHandlingKeywordParameters())
164           {
165             code.emitLoad(consumer);
166             code.emitInvokeInterface(MakeAttribute.endAttributeMethod);
167           }
168       }
169     code.emitInvokeStatic(endGroupMethod);
170   }
171
172   public Type getReturnType (Expression[] args)
173   {
174     return Compilation.typeObject;
175   }
176
177   static final ClassType typeMakeElement
178     = ClassType.make("gnu.kawa.xml.MakeElement");
179   static final Method beginGroupMethod3
180     = typeMakeElement.getDeclaredMethod("beginGroup", 3);
181   static final Method beginGroupMethod4
182     = typeMakeElement.getDeclaredMethod("beginGroup", 4);
183   static final Method endGroupMethod
184     = typeMakeElement.getDeclaredMethod("endGroup", 2);
185 }
186
Popular Tags