001: package migration.modules.rules;
002:
003: import java.util.*;
004: import java.io.*;
005:
006: import com.sun.portal.rewriter.rom.html.*;
007: import com.sun.portal.rewriter.rom.xml.*;
008: import com.sun.portal.rewriter.rom.common.*;
009:
010: import com.sun.portal.rewriter.rom.js.*;
011: import com.sun.portal.rewriter.rom.*;
012: import com.sun.portal.rewriter.util.*;
013: import com.sun.portal.rewriter.RewriterModule;
014:
015: public class ConvertRules {
016:
017: public static RuleSet createRuleSet(DataSource aDataSource) {
018:
019: try {
020: RuleCollection[] ruleCollections = new RuleCollection[3];
021: ruleCollections[0] = createHTMLRules(aDataSource);
022: ruleCollections[1] = createJSRules(aDataSource);
023: ruleCollections[2] = createXMLRules(aDataSource);
024: return new RuleSet("idsruleset", ruleCollections);
025:
026: } catch (Exception e) {
027: //e.printStackTrace();
028: System.err.println("Error in creating the ruleset: "
029: + e.getMessage());
030: System.exit(1);
031: }
032: return null;
033: }
034:
035: private static HTMLRules createHTMLRules(DataSource aDataSource)
036: throws Exception {
037: DataRuleCollection[] htmlRuleCollections = new DataRuleCollection[4];
038:
039: htmlRuleCollections[0] = createAppletRules(aDataSource);
040: htmlRuleCollections[1] = createFormRules(aDataSource);
041: htmlRuleCollections[2] = createAttributeRules(aDataSource);
042: return new HTMLRules(htmlRuleCollections);
043: }
044:
045: private static XMLRules createXMLRules(DataSource aDataSource)
046: throws Exception {
047:
048: DataRuleCollection[] xmlRuleCollections = new DataRuleCollection[2];
049: xmlRuleCollections[0] = createXMLAttrRules(aDataSource);
050: // xmlRuleCollections[0] = createAttributeRules( aDataSource );
051: xmlRuleCollections[1] = createXMLTextStringRules(aDataSource);
052: return new XMLRules(xmlRuleCollections);
053: }
054:
055: private static JSRules createJSRules(DataSource aDataSource)
056: throws Exception {
057:
058: DataRuleCollection[] jsRuleCollections = new DataRuleCollection[2];
059: jsRuleCollections[0] = createVariableRules(aDataSource);
060: jsRuleCollections[1] = createFunctionRules(aDataSource);
061: return new JSRules(jsRuleCollections);
062: }
063:
064: private static DataRuleCollection createAttributeRules(
065: DataSource aDataSource) {
066:
067: String[] attrStrings = aDataSource.getHTMLAttributeData();
068:
069: ArrayList list = new ArrayList(attrStrings.length);
070: //System.out.println("Atleast creating attributes...:"+attrStrings.length);
071: for (int i = 0; i < attrStrings.length; i++) {
072: //System.out.println("Attr:"+attrStrings[i]);
073: DataRule attr = new AttributeRule(
074: createAttribute(attrStrings[i]));
075: //System.out.println("Atleast creating attributes...:"+attr.toString());
076: if (attr != null) {
077: //System.out.println("Atleast creating attributes...:");
078: list.add(attr);
079: }
080: }
081: list.addAll(getJSTokenRules(aDataSource));
082: DataRule[] attrRules = (DataRule[]) list
083: .toArray(new DataRule[0]);
084: return new DataRuleCollection(Rule.ATTRIBUTE, attrRules);
085: }
086:
087: private static DataRuleCollection createAppletRules(
088: DataSource aDataSource) {
089:
090: String[] appletStrings = aDataSource.getHTMLAppletData();
091:
092: ArrayList list = new ArrayList(appletStrings.length);
093: for (int i = 0; i < appletStrings.length; i++) {
094:
095: DataRule applet = new AppletRule(
096: createApplet(appletStrings[i]));
097: if (null != applet) {
098: list.add(applet);
099: }
100: }
101: DataRule[] appletRules = (DataRule[]) list
102: .toArray(new DataRule[0]);
103: return new DataRuleCollection(Rule.APPLET, appletRules);
104: }
105:
106: private static Attribute createAttribute(String s) {
107:
108: String[] values = StringHelper.tokenize(s, " ", true);
109: if (values.length < 1) {
110: return null;
111: }
112: String aName = values[0];
113: String aVal;
114:
115: if (values.length < 2)
116: aVal = "";
117: else
118: aVal = values[1];
119:
120: String pattern = (values.length == 3) ? values[2] : "";
121: return new Attribute(aName, aVal, pattern, Rule.URL, null);
122: }
123:
124: private static Attribute createXMLAttribute(String s) {
125:
126: String[] values = StringHelper.tokenize(s, ",", true);
127: if (values.length < 1) {
128: return null;
129: }
130:
131: String aTag = values[0];
132: String aName = new String();
133: if (values.length < 2)
134: aName = "";
135: else
136: aName = values[1];
137:
138: String pattern = (values.length == 3) ? values[2] : "";
139: return new Attribute(aTag, aName, pattern, Rule.URL, null);
140: }
141:
142: private static TagText createTextString(String s) {
143:
144: String[] values = StringHelper.tokenize(s, ", ", true);
145: if (values.length < 1) {
146: return null;
147: }
148:
149: String aTag = values[0];
150:
151: String pattern = (values.length == 2) ? values[1] : "";
152: return new TagText(aTag, pattern, null);
153: }
154:
155: private static Applet createApplet(String s) {
156:
157: if (s.length() > 1 && s.charAt(0) != '*')
158: s = "*" + s;
159: String[] values = StringHelper.tokenize(s, " ", true);
160: if (values.length < 3)
161: return null;
162:
163: String src = values[0];
164: String code = values[1];
165: String param = values[2];
166: String pattern = (values.length == 4) ? values[3] : "";
167: return new Applet(code, param, pattern, src);
168: }
169:
170: public static DataRuleCollection createFormRules(
171: DataSource aDataSource) {
172: String[] formStrings = aDataSource.getHTMLFormData();
173: ArrayList list = new ArrayList(formStrings.length);
174: for (int i = 0; i < formStrings.length; i++) {
175: if (formStrings[i] != null && formStrings[i].length() > 0) {
176: DataRule formRule = new FormRule(
177: createForm(formStrings[i]));
178: list.add(formRule);
179: }
180: }
181: DataRule[] formRules = (DataRule[]) list
182: .toArray(new DataRule[0]);
183: return new DataRuleCollection(Rule.FORM, formRules);
184: }
185:
186: private static Form createForm(String s) {
187:
188: if (s.length() > 1 && s.charAt(0) != '*')
189: s = "*" + s;
190:
191: String[] values = StringHelper.tokenize(s, " ", true);
192: if (values.length < 3)
193: return null;
194:
195: String src = values[0];
196: String name = values[1];
197: String tag = values[2];
198:
199: StringBuffer pattern = new StringBuffer("");
200: for (int i = 3; i < values.length; i++) {
201: pattern.append(values[i]).append(" ");
202: }
203:
204: return new Form(name, tag, pattern.toString(), src);
205:
206: }
207:
208: /*private static DataRuleCollection createJSTokenRules(DataSource aDataSource){
209: int length = 0;
210: String[] jsTokens = aDataSource.getHTMLJSTokenData();
211:
212: if (jsTokens != null) {
213: length = jsTokens.length;
214: }
215:
216: List list = new ArrayList(length);
217: for ( int i = 0; i < length; i++){
218: if(jsTokens[i] != null && jsTokens[i].length() > 0) {
219: DataRule jsTokRule = new JSTokenRule(new JSToken(jsTokens[i]));
220: list.add(jsTokRule);
221: }
222: }
223: return new DataRuleCollection( Rule.JSTOKENS, dataRules );
224: }
225: */
226: private static List getJSTokenRules(DataSource aDataSource) {
227: int length = 0;
228: String[] jsTokens = aDataSource.getHTMLJSTokenData();
229:
230: if (jsTokens != null) {
231: length = jsTokens.length;
232: }
233:
234: List list = new ArrayList(length);
235: for (int i = 0; i < length; i++) {
236: if (jsTokens[i] != null && jsTokens[i].length() > 0) {
237: DataRule jsTokRule = new AttributeRule(new Attribute(
238: jsTokens[i], null, null, Rule.DJS, null));
239: list.add(jsTokRule);
240: }
241: }
242:
243: return list;
244: }
245:
246: private static DataRuleCollection createXMLAttrRules(
247: DataSource aDataSource) {
248: int length = 0;
249: String[] xmlAttrs = aDataSource.getXMLAttributeData();
250:
251: if (xmlAttrs != null) {
252: length = xmlAttrs.length;
253: }
254:
255: List list = new ArrayList(length);
256: for (int i = 0; i < length; i++) {
257: //DataRule xAttr=createAttribute(xmlAttrs[i]);
258: DataRule xAttr = new AttributeRule(
259: createXMLAttribute(xmlAttrs[i]));
260: if (xAttr != null)
261: list.add(xAttr);
262: }
263:
264: DataRule[] dataRules = (DataRule[]) list
265: .toArray(new DataRule[0]);
266: return new DataRuleCollection(Rule.ATTRIBUTE, dataRules);
267: }
268:
269: private static DataRuleCollection createXMLTextStringRules(
270: DataSource aDataSource) {
271: int length = 0;
272: String[] xmlTS = aDataSource.getXMLTextStringData();
273:
274: if (xmlTS != null) {
275: length = xmlTS.length;
276: }
277:
278: List list = new ArrayList(length);
279: for (int i = 0; i < length; i++) {
280: DataRule xTS = new TagTextRule(createTextString(xmlTS[i]));
281: if (xTS != null)
282: list.add(xTS);
283: }
284:
285: DataRule[] dataRules = (DataRule[]) list
286: .toArray(new DataRule[0]);
287: return new DataRuleCollection(Rule.TAGTEXT, dataRules);
288: }
289:
290: private static DataRuleCollection createVariableRules(
291: DataSource aDataSource) {
292:
293: List systemDataRules = null;
294:
295: List simpleDataRules = Arrays.asList(createVariables(
296: aDataSource.getJSURLVariablesData(), Rule.URL));
297: List expressionDataRules = Arrays.asList(createVariables(
298: aDataSource.getJSExpressionVariablesData(),
299: Rule.EXPRESSION));
300: List dhtmlDataRules = Arrays.asList(createVariables(aDataSource
301: .getJSDHTMLVariablesData(), Rule.DHTML));
302:
303: List dJSDataRules = Arrays.asList(createVariables(aDataSource
304: .getJSDJSVariablesData(), Rule.DJS));
305:
306: if (aDataSource.getJSSystemVariablesData() != null) {
307: systemDataRules = Arrays.asList(createVariables(aDataSource
308: .getJSSystemVariablesData(), Rule.SYSTEM));
309: }
310:
311: List allVariables = new ArrayList();
312: allVariables.addAll(simpleDataRules);
313: allVariables.addAll(expressionDataRules);
314: allVariables.addAll(dhtmlDataRules);
315: allVariables.addAll(dJSDataRules);
316:
317: if (aDataSource.getJSSystemVariablesData() != null) {
318: allVariables.addAll(systemDataRules);
319: }
320:
321: DataRule[] allRules = (DataRule[]) allVariables
322: .toArray(new DataRule[0]);
323: return new DataRuleCollection(Rule.VARIABLE, allRules);
324: }
325:
326: private static DataRule[] createVariables(String[] variables,
327: String type) {
328: if (variables != null) {
329: ArrayList list = new ArrayList(variables.length);
330: for (int i = 0; i < variables.length; i++) {
331: DataRule dataRule = new VariableRule(new Variable(
332: variables[i], type, null));
333: if (null != dataRule)
334: list.add(dataRule);
335:
336: }
337: return (DataRule[]) list.toArray(new DataRule[0]);
338: }
339: return new DataRule[0];
340: }
341:
342: private static DataRuleCollection createFunctionRules(
343: DataSource aDataSource) {
344: List simpleDataRules = Arrays.asList(createFunctions(
345: aDataSource.getJSURLFunctionParamsData(), Rule.URL));
346:
347: List expressionDataRules = Arrays.asList(createFunctions(
348: aDataSource.getJSExpressionFunctionParamsData(),
349: Rule.EXPRESSION));
350: List dhtmlDataRules = Arrays.asList(createFunctions(aDataSource
351: .getJSDHTMLFunctionParamsData(), Rule.DHTML));
352:
353: List dJSDataRules = Arrays.asList(createFunctions(aDataSource
354: .getJSDJSFunctionParamsData(), Rule.DJS));
355:
356: List allFunctions = new ArrayList();
357: allFunctions.addAll(simpleDataRules);
358: allFunctions.addAll(expressionDataRules);
359: allFunctions.addAll(dhtmlDataRules);
360: allFunctions.addAll(dJSDataRules);
361:
362: DataRule[] allRules = (DataRule[]) allFunctions
363: .toArray(new DataRule[0]);
364: return new DataRuleCollection(Rule.FUNCTION, allRules);
365: }
366:
367: private static DataRule[] createFunctions(String[] variables,
368: String type) {
369: int length = 0;
370: if (variables != null) {
371: length = variables.length;
372: }
373: ArrayList list = new ArrayList(length);
374: for (int i = 0; i < length; i++) {
375: DataRule dataRule = createFunction(type, variables[i]);
376: if (null != dataRule)
377: list.add(dataRule);
378:
379: }
380: return (DataRule[]) list.toArray(new DataRule[0]);
381: }
382:
383: private static DataRule createFunction(String type, String value) {
384: String[] values = StringHelper.tokenize(value, ":", true);
385: if (values.length < 2)
386: return null;
387:
388: String name = values[0];
389: String pattern = values[1];
390: return new FunctionRule(new Function(name, pattern, type, null));
391: }
392:
393: public static void main(String[] args) {
394: //System.out.println("Converting rules....");
395: String output = new String();
396: String infile, outfile;
397:
398: outfile = new String("outRuleSet.xml");
399: infile = args[0];
400: if (args.length == 2)
401: outfile = args[1];
402: if (args.length == 1)
403: outfile = "outRuleSet.xml";
404:
405: try {
406: output = ConvertRules.createRuleSet(
407: new IDSDataSource(infile)).toXML();
408: //output=ConvertRules.createRuleSet(new TestDataSource()).toXML();
409:
410: if (output != null) {
411: OutputStreamWriter fw = new OutputStreamWriter(
412: new FileOutputStream(outfile), "UTF-8");
413: fw.write(output);
414: fw.close();
415: }
416:
417: } catch (Exception e) {
418: System.err.println("Error writing to specifed file: "
419: + outfile);
420: //e.printStackTrace();
421: System.exit(1);
422: }
423: }
424: }
|