KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > util > arl > ArlUtil


1 //
2
// Ejen (code generation system)
3
// Copyright (C) 2001, 2002 François Wolff (ejen@noos.fr).
4
//
5
// This file is part of Ejen.
6
//
7
// Ejen is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
11
//
12
// Ejen is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License
18
// along with Ejen; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
//
21
package org.ejen.util.arl;
22
23 import java.io.StringReader JavaDoc;
24 import org.apache.xml.utils.WrappedRuntimeException;
25
26 /**
27  * ArlUtil class utility (static methods).
28  * <p>
29  * "ARL" stands for "ARray Language". It is a very basic language that allows
30  * to set elements of an <code>int</code> array to one of three flag values:
31  * <code>F_ACCEPT</code>, <code>F_REMOVE</code> or <code>F_CROSS</code>.
32  * <p>
33  * An ARL sequence looks like <code>"~23;+34;-45;-[46,56];..."</code> The
34  * meaning of this expression is: "set the 23th element of the array to
35  * <code>F_CROSS</code>; set the 34th element to <code>F_ACCEPT</code>; set
36  * the 45th to <code>F_REMOVE</code>; set all elements between and including
37  * the 46th and the 56th to <code>F_REMOVE</code>;..."
38  * <p>
39  * Order is important: <code>"-[23,56];~34"</code> means "set all elements of
40  * the array between and including 23 and 56 to <code>F_REMOVE</code>; <i>then</i>
41  * set the 34th element to <code>F_CROSS</code>". On the other hand,
42  * <code>"~34;-[23,56]"</code> is strictly identical to <code>"-[23,56]"</code>.
43  * <p>
44  * The "+" sign (for <code>F_ACCEPT</code>) is optional: <code>"+45"</code> is identical
45  * to <code>"45"</code>.
46  * <p>
47  * This language is used for parsers control
48  * (see {@link org.ejen.ext.parsers.java_1_2.JavaSourceToXML#process(
49  * org.apache.xalan.extensions.ExpressionContext,String,String,String)
50  * JavaSourceToXML.process(ExpressionContext,String,String,String)},
51  * {@link org.ejen.ext.parsers.java_1_2.SimpleNode#toNode(org.w3c.dom.Document,
52  * org.w3c.dom.Node,
53  * int[],
54  * int[],
55  * boolean)
56  * SimpleNode.toNode(Document,Node,int[],int[],boolean)}
57  * and {@link org.ejen.ext.parsers.java_1_2.Token#toNode(org.w3c.dom.Document,
58  * org.w3c.dom.Node,
59  * int[],
60  * boolean)
61  * Token.toNode(Document,Node,int[],boolean)}).
62  * <p>
63  * @author F. Wolff
64  * @version 1.0
65  */

66 public class ArlUtil implements ArlParserTreeConstants, ArlParserConstants {
67     
68     public static final int F_ACCEPT = 0;
69     public static final int F_REMOVE = 1;
70     public static final int F_CROSS = 2;
71
72     /**
73      * Prevents intanciation.
74      */

75     protected ArlUtil() {}
76
77     /**
78      * Returns the array parameter whose elements are set according to the
79      * arl parameter.
80      * @param arl an ARL sequence.
81      * @param array the array to modify.
82      * @return the modified array parameter.
83      * @throws org.apache.xml.utils.WrappedRuntimeException parse exception,
84      * array out of bound exception...
85      */

86     public static int[] process(String JavaDoc arl, int[] array) {
87         if (array == null) {
88             return null;
89         }
90         StringReader JavaDoc sr = null;
91
92         try {
93             sr = new StringReader JavaDoc(arl);
94             if (ArlParser.token_source == null) {
95                 new ArlParser(sr);
96             } else {
97                 ArlParser.ReInit(sr);
98             }
99             SimpleNode sn = ArlParser.ArlSequence();
100
101             for (int i = 0; i < sn.jjtGetNumChildren(); i++) {
102                 int flag = F_ACCEPT;
103                 SimpleNode exp = (SimpleNode) (sn.jjtGetChild(i));
104                 Token t = exp.getFirstToken();
105
106                 if (t.kind >= ACCEPT && t.kind <= CROSS) {
107                     if (t.kind == REMOVE) {
108                         flag = F_REMOVE;
109                     } else if (t.kind == CROSS) {
110                         flag = F_CROSS;
111                     }
112                     t = t.next;
113                 }
114                 if (exp.jjtGetNumChildren() == 0) {
115                     array[Integer.parseInt(t.image)] = flag;
116                 } else {
117                     exp = (SimpleNode) (exp.jjtGetChild(0));
118                     t = exp.getFirstToken().next;
119                     int i1 = Integer.parseInt(t.image);
120                     int i2 = Integer.parseInt(t.next.next.image);
121
122                     for (int j = i1; j <= i2; j++) {
123                         array[j] = flag;
124                     }
125                 }
126             }
127             return array;
128         } catch (Exception JavaDoc e) {
129             throw new WrappedRuntimeException("Bad Arl sequence: " + arl, e);
130         }
131         finally {
132             if (sr != null) {
133                 try {
134                     sr.close();
135                 } catch (Exception JavaDoc e) {}
136                 finally {
137                     sr = null;
138                 }
139             }
140         }
141     }
142 }
143
Popular Tags