001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.rewriter.engines.xml.test;
006:
007: import com.sun.portal.rewriter.Rewriter;
008: import com.sun.portal.rewriter.RewriterPool;
009: import com.sun.portal.rewriter.engines.LanguageConstants;
010: import com.sun.portal.rewriter.rom.RuleSet;
011: import com.sun.portal.rewriter.test.util.BasicTestCase;
012: import com.sun.portal.rewriter.test.util.BasicXMLTestCase;
013: import com.sun.portal.rewriter.test.util.CreateRuleSet;
014: import junit.framework.TestSuite;
015:
016: public class TestXMLInHTML extends BasicXMLTestCase {
017: private static final String sampleXMLData = "<RuleSet id=\"default_ruleset\">\n"
018: + "\n"
019: + " <img text=\"abc.html\"/>"
020: + " <!-- Rules for Rewriting HTML Source -->\n"
021: + " <HTMLRules>\n"
022: + "\n"
023: + " <!-- Rules for Rewriting Form Input/Option Values List -->\n"
024: + " <Forms>\n"
025: + " </Forms>\n"
026: + "\n"
027: + " <!-- Rules for Rewriting Applet/Object Parameter Values List -->\n"
028: + " <Applets>\n"
029: + " </Applets>\n"
030: + "\n"
031: + " <!-- Rules for Rewriting HTML Attributes -->\n"
032: + " <Attributes>\n"
033: + " <Attribute name=\"action\"/>\n"
034: + " <Attribute name=\"background\"/>\n"
035: + " <Attribute name=\"codebase\"/>\n"
036: + " <Attribute name=\"href\"/>\n"
037: + " <Attribute name=\"src\"/>\n"
038: + " <Attribute name=\"value\"/>\n"
039: + " <Attribute name=\"imagePath\"/>\n"
040: + " <Attribute name=\"lowsrc\"/>\n"
041: + " <Attribute name=\"archive\"/>\n"
042: + " </Attributes>\n"
043: + "\n"
044: + " <!-- Rules for Rewriting HTML Attributes containing Java Script -->\n"
045: + " <JSTokens>\n"
046: + " <Attribute name=\"onAbort\" type=\"DJS\"/>\n"
047: + " <Attribute name=\"onBlur\" type=\"DJS\"/>\n"
048: + " <Attribute name=\"onChange\" type=\"DJS\"/>\n"
049: + " <Attribute name=\"onClick\" type=\"DJS\"/>\n"
050: + " <Attribute name=\"onDblClick\" type=\"DJS\"/>\n"
051: + " <Attribute name=\"onError\" type=\"DJS\"/>\n"
052: + " <Attribute name=\"onFocus\" type=\"DJS\"/>\n"
053: + " <Attribute name=\"onKeyDown\" type=\"DJS\"/>\n"
054: + " <Attribute name=\"onKeyPress\" type=\"DJS\"/>\n"
055: + " <Attribute name=\"onKeyUp\" type=\"DJS\"/>\n"
056: + " <Attribute name=\"onLoad\" type=\"DJS\"/>\n"
057: + " <Attribute name=\"onMouseDown\" type=\"DJS\"/>\n"
058: + " <Attribute name=\"onMouseMove\" type=\"DJS\"/>\n"
059: + " <Attribute name=\"onMouseOut\" type=\"DJS\"/>\n"
060: + " <Attribute name=\"onMouseOver\" type=\"DJS\"/>\n"
061: + " <Attribute name=\"onMouseUp\" type=\"DJS\"/>\n"
062: + " <Attribute name=\"onReset\" type=\"DJS\"/>\n"
063: + " <Attribute name=\"onSelect\" type=\"DJS\"/>\n"
064: + " <Attribute name=\"onSubmit\" type=\"DJS\"/>\n"
065: + " <Attribute name=\"onUnload\" type=\"DJS\"/>\n"
066: + " </JSTokens>\n"
067: + " </HTMLRules>\n"
068: + "\n"
069: + " <!-- Rules for Rewriting JavaScript Source -->\n"
070: + " <JSRules>\n"
071: + "\n"
072: + " <!-- Rules for Rewriting JavaScript variables in URLs -->\n"
073: + " <Variables>\n"
074: + " <Variable name=\" imgsrc \" type=\"URL\"/>\n"
075: + " <Variable name=\" location.href \" type=\"URL\"/>\n"
076: + " <Variable name=\" _fr.location \" type=\"URL\"/>\n"
077: + " <Variable name=\" mf.location \" type=\"URL\"/>\n"
078: + " <Variable name=\" parent.location \" type=\"URL\"/>\n"
079: + " <Variable name=\" self.location \" type=\"URL\"/>\n"
080: + " <Variable name=\" location \" type=\"EXPRESSION\"/>\n"
081: + " <Variable name=\" window.location.pathname \" type=\"SYSTEM\"/>\n"
082: + " </Variables>\n"
083: + "\n"
084: + " <!-- Rules for Rewriting JavaScript Function Parameters -->\n"
085: + " <Functions>\n"
086: + " <Function type=\"URL\" name=\"openURL\" paramPatterns=\"y\"/>\n"
087: + " <Function type=\"URL\" name=\"openAppURL\" paramPatterns=\"y\"/>\n"
088: + " <Function type=\"URL\" name=\"openNewWindow\" paramPatterns=\"y\"/>\n"
089: + " <Function type=\"URL\" name=\"parent.openNewWindow\" paramPatterns=\"y\"/>\n"
090: + " <Function type=\"URL\" name=\"window.open\" paramPatterns=\"y\"/>\n"
091: + " <Function type=\"DHTML\" name=\"document.write\" paramPatterns=\"y\"/>\n"
092: + " <Function type=\"DHTML\" name=\"document.writeln\" paramPatterns=\"y\"/>\n"
093: + " </Functions>\n"
094: + "\n"
095: + " </JSRules>\n"
096: + "\n"
097: + " <!-- Rules for Rewriting XML Source -->\n"
098: + " <XMLRules>\n"
099: + "\n"
100: + " <!-- Rules for Rewriting Attributes -->\n"
101: + " <Attributes>\n"
102: + " <Attribute name=\"xmlns\"/>\n"
103: + " <Attribute name=\"href\" tag=\"a\"/>\n"
104: + " </Attributes>\n"
105: + "\n"
106: + " <!-- Rules for Rewriting TextStrings -->\n"
107: + " <TextStrings>\n"
108: + " <TagText tag=\"baseroot\"/>\n"
109: + " <TagText tag=\"img\"/>\n"
110: + " <TagText tag=\"xsl:attribute\" attributePatterns=\"name=src\"/>\n"
111: + " </TextStrings>\n"
112: + "\n"
113: + " </XMLRules>\n"
114: + "\n" + " <img text=\"abc.html\"/>" + "</RuleSet>\n";
115:
116: private static final String expectedXMLData = "<RuleSet id=\"default_ruleset\">\n"
117: + "\n"
118: + " <img text=\"http://rajanagendra.sun.com/Base/Raja/abc.html\"/>"
119: + " <!-- Rules for Rewriting HTML Source -->\n"
120: + " <HTMLRules>\n"
121: + "\n"
122: + " <!-- Rules for Rewriting Form Input/Option Values List -->\n"
123: + " <Forms>\n"
124: + " </Forms>\n"
125: + "\n"
126: + " <!-- Rules for Rewriting Applet/Object Parameter Values List -->\n"
127: + " <Applets>\n"
128: + " </Applets>\n"
129: + "\n"
130: + " <!-- Rules for Rewriting HTML Attributes -->\n"
131: + " <Attributes>\n"
132: + " <Attribute name=\"action\"/>\n"
133: + " <Attribute name=\"background\"/>\n"
134: + " <Attribute name=\"codebase\"/>\n"
135: + " <Attribute name=\"href\"/>\n"
136: + " <Attribute name=\"src\"/>\n"
137: + " <Attribute name=\"value\"/>\n"
138: + " <Attribute name=\"imagePath\"/>\n"
139: + " <Attribute name=\"lowsrc\"/>\n"
140: + " <Attribute name=\"archive\"/>\n"
141: + " </Attributes>\n"
142: + "\n"
143: + " <!-- Rules for Rewriting HTML Attributes containing Java Script -->\n"
144: + " <JSTokens>\n"
145: + " <Attribute name=\"onAbort\" type=\"DJS\"/>\n"
146: + " <Attribute name=\"onBlur\" type=\"DJS\"/>\n"
147: + " <Attribute name=\"onChange\" type=\"DJS\"/>\n"
148: + " <Attribute name=\"onClick\" type=\"DJS\"/>\n"
149: + " <Attribute name=\"onDblClick\" type=\"DJS\"/>\n"
150: + " <Attribute name=\"onError\" type=\"DJS\"/>\n"
151: + " <Attribute name=\"onFocus\" type=\"DJS\"/>\n"
152: + " <Attribute name=\"onKeyDown\" type=\"DJS\"/>\n"
153: + " <Attribute name=\"onKeyPress\" type=\"DJS\"/>\n"
154: + " <Attribute name=\"onKeyUp\" type=\"DJS\"/>\n"
155: + " <Attribute name=\"onLoad\" type=\"DJS\"/>\n"
156: + " <Attribute name=\"onMouseDown\" type=\"DJS\"/>\n"
157: + " <Attribute name=\"onMouseMove\" type=\"DJS\"/>\n"
158: + " <Attribute name=\"onMouseOut\" type=\"DJS\"/>\n"
159: + " <Attribute name=\"onMouseOver\" type=\"DJS\"/>\n"
160: + " <Attribute name=\"onMouseUp\" type=\"DJS\"/>\n"
161: + " <Attribute name=\"onReset\" type=\"DJS\"/>\n"
162: + " <Attribute name=\"onSelect\" type=\"DJS\"/>\n"
163: + " <Attribute name=\"onSubmit\" type=\"DJS\"/>\n"
164: + " <Attribute name=\"onUnload\" type=\"DJS\"/>\n"
165: + " </JSTokens>\n"
166: + " </HTMLRules>\n"
167: + "\n"
168: + " <!-- Rules for Rewriting JavaScript Source -->\n"
169: + " <JSRules>\n"
170: + "\n"
171: + " <!-- Rules for Rewriting JavaScript variables in URLs -->\n"
172: + " <Variables>\n"
173: + " <Variable name=\" imgsrc \" type=\"URL\"/>\n"
174: + " <Variable name=\" location.href \" type=\"URL\"/>\n"
175: + " <Variable name=\" _fr.location \" type=\"URL\"/>\n"
176: + " <Variable name=\" mf.location \" type=\"URL\"/>\n"
177: + " <Variable name=\" parent.location \" type=\"URL\"/>\n"
178: + " <Variable name=\" self.location \" type=\"URL\"/>\n"
179: + " <Variable name=\" location \" type=\"EXPRESSION\"/>\n"
180: + " <Variable name=\" window.location.pathname \" type=\"SYSTEM\"/>\n"
181: + " </Variables>\n"
182: + "\n"
183: + " <!-- Rules for Rewriting JavaScript Function Parameters -->\n"
184: + " <Functions>\n"
185: + " <Function type=\"URL\" name=\"openURL\" paramPatterns=\"y\"/>\n"
186: + " <Function type=\"URL\" name=\"openAppURL\" paramPatterns=\"y\"/>\n"
187: + " <Function type=\"URL\" name=\"openNewWindow\" paramPatterns=\"y\"/>\n"
188: + " <Function type=\"URL\" name=\"parent.openNewWindow\" paramPatterns=\"y\"/>\n"
189: + " <Function type=\"URL\" name=\"window.open\" paramPatterns=\"y\"/>\n"
190: + " <Function type=\"DHTML\" name=\"document.write\" paramPatterns=\"y\"/>\n"
191: + " <Function type=\"DHTML\" name=\"document.writeln\" paramPatterns=\"y\"/>\n"
192: + " </Functions>\n"
193: + "\n"
194: + " </JSRules>\n"
195: + "\n"
196: + " <!-- Rules for Rewriting XML Source -->\n"
197: + " <XMLRules>\n"
198: + "\n"
199: + " <!-- Rules for Rewriting Attributes -->\n"
200: + " <Attributes>\n"
201: + " <Attribute name=\"xmlns\"/>\n"
202: + " <Attribute name=\"href\" tag=\"a\"/>\n"
203: + " </Attributes>\n"
204: + "\n"
205: + " <!-- Rules for Rewriting TextStrings -->\n"
206: + " <TextStrings>\n"
207: + " <TagText tag=\"baseroot\"/>\n"
208: + " <TagText tag=\"img\"/>\n"
209: + " <TagText tag=\"xsl:attribute\" attributePatterns=\"name=src\"/>\n"
210: + " </TextStrings>\n"
211: + "\n"
212: + " </XMLRules>\n"
213: + "\n"
214: + " <img text=\"http://rajanagendra.sun.com/Base/Raja/abc.html\"/>"
215: + "</RuleSet>\n";
216:
217: public TestXMLInHTML(String aName) {
218: super (aName);
219: }//constuctor
220:
221: public void testEmbeededXML() throws Exception {
222: String[][] lData = {
223:
224: { "<Xml>" + sampleXMLData + "</xMl>",
225: "<Xml>" + expectedXMLData + "</xMl>", }, //0
226:
227: { "<xml>" + sampleXMLData + "</xml>",
228: "<xml>" + expectedXMLData + "</xml>", }, //1
229:
230: { "<XML>" + sampleXMLData + "</XML>",
231: "<XML>" + expectedXMLData + "</XML>", }, //2
232:
233: { "<Xml><!--" + sampleXMLData + "--></xMl>",
234: "<Xml><!--" + expectedXMLData + "--></xMl>", }, //3
235:
236: {
237: "<xml>\n<!--" + sampleXMLData + "-->\n</xml>",
238: "<xml>\n<!--" + expectedXMLData + "-->\n</xml>", }, //4
239:
240: {
241: "<xml>\n\t\t\n <!--" + sampleXMLData
242: + "-->\n</xml>",
243: "<xml>\n\t\t\n <!--" + expectedXMLData
244: + "-->\n</xml>", }, //5
245:
246: {
247: "<XML><!--\n" + sampleXMLData + "\n--></XML>",
248: "<XML><!--\n" + expectedXMLData + "\n--></XML>", }, //6
249:
250: { "<xml>\n<!--" + sampleXMLData + "</xml>",
251: "<xml>\n<!--" + expectedXMLData + "</xml>", }, //7
252:
253: { "<XML>\n<!--" + sampleXMLData + "</XML>",
254: "<XML>\n<!--" + expectedXMLData + "</XML>", }, //8
255:
256: { "<XmL>\n<!--" + sampleXMLData + "</XmL>",
257: "<XmL>\n<!--" + expectedXMLData + "</XmL>", }, //9
258: };
259:
260: String lAttributeRules = "<Attribute name=\"te*t\"/>"; //4
261: for (int i = 0; i < lData.length; i++) {
262: RuleSet lRuleSet = CreateRuleSet
263: .withXMLAttributeRules(lAttributeRules);
264: Rewriter lRewriter = RewriterPool.create(lRuleSet,
265: LanguageConstants.HTML_MIME);
266: String lResult = lRewriter.rewrite(lData[i][0],
267: getDefaultTranslator());
268: assertEquals("Failed i= " + i, lData[i][1], lResult);
269: }//for loop
270: }//testEmbeededXML()
271:
272: public void testRewritingOfInvalidXMLs() throws Exception {
273: String[][] lData = {
274: //xml does not support // as comment
275: //but javascript does support
276: { "<Xml><!--" + sampleXMLData + "//--></xMl>",
277: "<Xml><!--" + expectedXMLData + "//--></xMl>", }, //0
278:
279: {
280: "<xml>\n<!--" + sampleXMLData + "//-->\n</xml>",
281: "<xml>\n<!--" + expectedXMLData
282: + "//-->\n</xml>", }, //1
283:
284: {
285: "<XML><!--\n" + sampleXMLData + "\n//--></XML>",
286: "<XML><!--\n" + expectedXMLData
287: + "\n//--></XML>", }, //2
288:
289: { "<Xml><!--" + sampleXMLData + "//--></xMl>",
290: "<Xml><!--" + expectedXMLData + "//--></xMl>", }, //3
291:
292: { "<xml>\n<!--" + sampleXMLData + "--</xml>",
293: "<xml>\n<!--" + expectedXMLData + "--</xml>", }, //4
294:
295: { "<XML>\n<!--" + sampleXMLData + "--</XML>",
296: "<XML>\n<!--" + expectedXMLData + "--</XML>", }, //5
297:
298: { "<XmL>\n<!--" + sampleXMLData + "--</XmL>",
299: "<XmL>\n<!--" + expectedXMLData + "--</XmL>", }, //6
300: };
301:
302: String lAttributeRules = "<Attribute name=\"te*t\"/>"; //4
303: for (int i = 0; i < lData.length; i++) {
304: RuleSet lRuleSet = CreateRuleSet
305: .withXMLAttributeRules(lAttributeRules);
306: Rewriter lRewriter = RewriterPool.create(lRuleSet,
307: LanguageConstants.HTML_MIME);
308: String lResult = lRewriter.rewrite(lData[i][0],
309: getDefaultTranslator());
310: assertEquals("Failed i= " + i, lData[i][1], lResult);
311: }//for loop
312: }//testRewritingOfInvalidXMLs()
313:
314: public static void main(String[] args) {
315: com.sun.portal.rewriter.RewriterModule.initFile();
316: BasicTestCase.run(TestXMLInHTML.class);
317: TestSuite suite = new TestSuite();
318: suite.addTest(new TestXMLInHTML("testEmbeededXML"));
319: //BasicTestCase.run( suite );
320: }//main()
321:
322: }//TestXMLInHTML
|