001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: Sax.java,v 1.4 2007/03/27 21:59:42 mlipp Exp $
021: *
022: * $Log: Sax.java,v $
023: * Revision 1.4 2007/03/27 21:59:42 mlipp
024: * Fixed lots of checkstyle warnings.
025: *
026: * Revision 1.3 2006/09/29 12:32:09 drmlipp
027: * Consistently using WfMOpen as projct name now.
028: *
029: * Revision 1.2 2006/03/08 14:46:41 drmlipp
030: * Synchronized with 1.3.3p5.
031: *
032: * Revision 1.1.1.2.6.1 2005/12/05 12:13:33 drmlipp
033: * Added tests for toString() of SAXContentBuffer.
034: *
035: * Revision 1.1.1.2 2004/08/18 15:18:47 drmlipp
036: * Update to 1.2
037: *
038: * Revision 1.12 2004/06/25 14:52:10 lipp
039: * Minimal implementation of XMLStreamReader.
040: *
041: * Revision 1.11 2004/05/09 18:41:43 lipp
042: * Added support for content > 32k.
043: *
044: * Revision 1.10 2003/06/27 09:44:13 lipp
045: * Fixed copyright/license information.
046: *
047: * Revision 1.9 2003/04/24 20:51:21 lipp
048: * Fixed some warnings.
049: *
050: * Revision 1.8 2003/04/21 20:25:41 lipp
051: * Major revision of implementation.
052: *
053: * Revision 1.7 2003/04/17 21:04:13 lipp
054: * Minor updates for namespace support.
055: *
056: * Revision 1.6 2002/10/08 13:25:12 schlue
057: * Classpath modified to ensure that xerces is used.
058: * Use of localname changed to qname.
059: *
060: * Revision 1.5 2002/02/03 21:41:42 lipp
061: * Cleaned up unittests.
062: *
063: * Revision 1.4 2002/01/23 10:42:20 lipp
064: * Added delegating handler.
065: *
066: * Revision 1.3 2002/01/23 09:42:34 lipp
067: * Added default handler and attributes.
068: *
069: * Revision 1.2 2002/01/22 22:28:26 lipp
070: * Getting started with sax support, the second.
071: *
072: * Revision 1.1 2002/01/22 15:51:48 lipp
073: * Getting started with sax support.
074: *
075: */
076: package util;
077:
078: import java.util.HashSet;
079: import java.util.Set;
080:
081: import javax.xml.parsers.SAXParser;
082: import javax.xml.parsers.SAXParserFactory;
083: import javax.xml.stream.XMLStreamConstants;
084: import javax.xml.stream.XMLStreamReader;
085:
086: import org.xml.sax.Attributes;
087: import org.xml.sax.EntityResolver;
088: import org.xml.sax.InputSource;
089: import org.xml.sax.SAXException;
090: import org.xml.sax.XMLReader;
091: import org.xml.sax.helpers.DefaultHandler;
092:
093: import de.danet.an.util.sax.DelegatingHandler;
094: import de.danet.an.util.sax.HandlerStack;
095: import de.danet.an.util.sax.SAXContentBuffer;
096: import de.danet.an.util.sax.StackedHandler;
097:
098: import junit.framework.*;
099:
100: /**
101: * Test class for sax utility classes
102: */
103: public class Sax extends TestCase {
104:
105: /**
106: * Konstruktor zum Erzeugen eines TestCase
107: */
108: public Sax(String name) {
109: super (name);
110: }
111:
112: /**
113: * Assembling the test suite
114: */
115: public static Test suite() throws Exception {
116: TestSuite suite = new TestSuite();
117: suite.addTest(new Sax("saxContentBuf1"));
118: suite.addTest(new Sax("saxContentBuf2"));
119: suite.addTest(new Sax("saxContentBuf3"));
120: suite.addTest(new Sax("simpleParse"));
121: suite.addTest(new Sax("scopedParse"));
122: suite.addTest(new Sax("scopedParse2"));
123: suite.addTest(new Sax("scopedParse3"));
124: suite.addTest(new Sax("scopedParse4"));
125: return suite;
126: }
127:
128: private XMLReader getReader() throws Exception {
129: SAXParserFactory pf = SAXParserFactory.newInstance();
130: pf.setValidating(false);
131: pf.setNamespaceAware(true);
132: SAXParser parser = pf.newSAXParser();
133: XMLReader reader = parser.getXMLReader();
134: reader.setEntityResolver(new EntityResolver() {
135: public InputSource resolveEntity(String publicId,
136: String systemId) {
137: return new InputSource(Sax.class
138: .getResourceAsStream("/xpdl.dtd"));
139: }
140: });
141: return reader;
142: }
143:
144: /**
145: * Test
146: */
147: public void saxContentBuf1() throws Exception {
148: SAXContentBuffer buf = new SAXContentBuffer();
149: final String ts1 = "Hello world!";
150: StringBuffer ts2 = new StringBuffer();
151: for (int i = 0; i < 5000; i++) {
152: ts2.append("0123456789");
153: }
154: assertTrue(ts2.length() == 50000);
155:
156: buf.characters(ts1.toCharArray(), 0, ts1.toCharArray().length);
157: buf.characters(ts2.toString().toCharArray(), 0, ts2.toString()
158: .toCharArray().length);
159:
160: final String[] res = new String[2];
161: buf.emit(new DefaultHandler() {
162: public void characters(char[] ch, int start, int length)
163: throws SAXException {
164: if (res[0] == null) {
165: res[0] = new String(ch, start, length);
166: } else {
167: res[1] = new String(ch, start, length);
168: }
169: }
170: });
171: assertTrue(res[0].equals(ts1));
172: assertTrue(res[1].equals(ts2.toString()));
173: }
174:
175: /**
176: * Test
177: */
178: public void saxContentBuf2() throws Exception {
179: SAXContentBuffer buf = new SAXContentBuffer();
180: XMLReader reader = getReader();
181: reader.setContentHandler(buf);
182: reader.parse(new InputSource(Sax.class
183: .getResourceAsStream("/util/testXPDL.xml")));
184: XMLStreamReader rdr = buf.createXMLStreamReader();
185: int event = 0;
186: int sees = 0;
187: int eees = 0;
188: while (rdr.hasNext()) {
189: event = rdr.next();
190: switch (event) {
191: case XMLStreamConstants.START_ELEMENT:
192: if (sees == 0) {
193: assertTrue(rdr.getNamespaceURI().equals(
194: "http://www.wfmc.org/2002/XPDL1.0"));
195: assertTrue(rdr.getLocalName().equals("Package"));
196: }
197: sees += 1;
198: break;
199: case XMLStreamConstants.END_ELEMENT:
200: eees += 1;
201: break;
202: }
203: }
204: assertTrue(event == XMLStreamConstants.END_DOCUMENT);
205: assertTrue(sees > 0);
206: assertTrue(sees == eees);
207: }
208:
209: /**
210: * Test
211: */
212: public void saxContentBuf3() throws Exception {
213: SAXContentBuffer buf = new SAXContentBuffer();
214: XMLReader reader = getReader();
215: reader.setContentHandler(buf);
216: reader.parse(new InputSource(Sax.class
217: .getResourceAsStream("/util/simpleSample.xml")));
218: assertTrue(buf.toString(), buf.toString().equals(
219: "<body><item/></body>"));
220: }
221:
222: /**
223: * Simple parsing test
224: */
225: public void simpleParse() throws Exception {
226: XMLReader reader = getReader();
227: HandlerStack stack = new HandlerStack(reader,
228: new StackedHandler());
229: reader.parse(new InputSource(Sax.class
230: .getResourceAsStream("/util/testXPDL.xml")));
231: }
232:
233: /**
234: * Scoped parsing test
235: */
236: public void scopedParse() throws Exception {
237: final Set outerTags = new HashSet();
238: final Set innerTags = new HashSet();
239: final String expPath = "/{http://www.wfmc.org/2002/XPDL1.0}Package"
240: + "/{http://www.wfmc.org/2002/XPDL1.0}WorkflowProcesses"
241: + "/{http://www.wfmc.org/2002/XPDL1.0}WorkflowProcess"
242: + "/{http://www.wfmc.org/2002/XPDL1.0}Activities"
243: + "/{http://www.wfmc.org/2002/XPDL1.0}Activity"
244: + "/{http://www.wfmc.org/2002/XPDL1.0}TransitionRestrictions";
245: XMLReader reader = getReader();
246: HandlerStack stack = new HandlerStack(reader,
247: new StackedHandler() {
248: public void startElement(String uri,
249: String localName, String qName,
250: Attributes atts) throws SAXException {
251: outerTags.add(qName);
252: if (qName.equals("TransitionRestrictions")) {
253: assertTrue(currentPath().equals(expPath));
254: getStack().push(new SubHandler());
255: }
256: }
257: });
258: stack.setContextData("innerTags", innerTags);
259: reader.parse(new InputSource(Sax.class
260: .getResourceAsStream("/util/testXPDL.xml")));
261: assertTrue(outerTags.contains("Activity"));
262: assertTrue(outerTags.contains("TransitionRestrictions"));
263: assertTrue(!outerTags.contains("TransitionRestriction"));
264: assertTrue(innerTags.contains("TransitionRestrictions"));
265: assertTrue(innerTags.contains("TransitionRestriction"));
266: assertTrue(!innerTags.contains("Activity"));
267: }
268:
269: public static class SubHandler extends StackedHandler {
270: private Set tags;
271:
272: public void startElement(String uri, String localName,
273: String qName, Attributes atts) throws SAXException {
274: Set tags = (Set) getContextData("innerTags");
275: tags.add(qName);
276: }
277: }
278:
279: /**
280: * Scoped parsing test using delegating handler
281: */
282: public void scopedParse2() throws Exception {
283: final Set outerTags = new HashSet();
284: final Set innerTags = new HashSet();
285: XMLReader reader = getReader();
286: DelegatingHandler dh = new DelegatingHandler() {
287: public void startElement(String uri, String localName,
288: String qName, Attributes atts) throws SAXException {
289: outerTags.add(qName);
290: super .startElement(uri, localName, qName, atts);
291: }
292: };
293: dh.addHandler("http://www.wfmc.org/2002/XPDL1.0",
294: "TransitionRestrictions", SubHandler.class);
295: HandlerStack stack = new HandlerStack(reader, dh);
296: stack.setContextData("innerTags", innerTags);
297: reader.parse(new InputSource(Sax.class
298: .getResourceAsStream("/util/testXPDL.xml")));
299: assertTrue(outerTags.contains("Activity"));
300: assertTrue(outerTags.contains("TransitionRestrictions"));
301: assertTrue(!outerTags.contains("TransitionRestriction"));
302: assertTrue(innerTags.contains("TransitionRestrictions"));
303: assertTrue(innerTags.contains("TransitionRestriction"));
304: assertTrue(!innerTags.contains("Activity"));
305: }
306:
307: /**
308: * Scoped parsing test using delegating handler
309: */
310: public void scopedParse3() throws Exception {
311: final Set outerTags = new HashSet();
312: final Set innerTags = new HashSet();
313: XMLReader reader = getReader();
314: DelegatingHandler dh = new DelegatingHandler() {
315: public void startElement(String uri, String localName,
316: String qName, Attributes atts) throws SAXException {
317: outerTags.add(qName);
318: super .startElement(uri, localName, qName, atts);
319: }
320: };
321: dh
322: .addHandler(
323: "{http://www.wfmc.org/2002/XPDL1.0}Activity"
324: + "/{http://www.wfmc.org/2002/XPDL1.0}TransitionRestrictions",
325: SubHandler.class);
326: HandlerStack stack = new HandlerStack(reader, dh);
327: stack.setContextData("innerTags", innerTags);
328: reader.parse(new InputSource(Sax.class
329: .getResourceAsStream("/util/testXPDL.xml")));
330: assertTrue(outerTags.contains("Activity"));
331: assertTrue(outerTags.contains("TransitionRestrictions"));
332: assertTrue(!outerTags.contains("TransitionRestriction"));
333: assertTrue(innerTags.contains("TransitionRestrictions"));
334: assertTrue(innerTags.contains("TransitionRestriction"));
335: assertTrue(!innerTags.contains("Activity"));
336: }
337:
338: /**
339: * Scoped parsing test using delegating handler
340: */
341: public void scopedParse4() throws Exception {
342: final Set outerTags = new HashSet();
343: final Set innerTags = new HashSet();
344: XMLReader reader = getReader();
345: DelegatingHandler dh = new DelegatingHandler() {
346: public void startElement(String uri, String localName,
347: String qName, Attributes atts) throws SAXException {
348: outerTags.add(qName);
349: super .startElement(uri, localName, qName, atts);
350: }
351: };
352: dh
353: .addHandler(
354: "/{http://www.wfmc.org/2002/XPDL1.0}Package"
355: + "/{http://www.wfmc.org/2002/XPDL1.0}WorkflowProcesses"
356: + "/{http://www.wfmc.org/2002/XPDL1.0}WorkflowProcess"
357: + "/{http://www.wfmc.org/2002/XPDL1.0}Activities"
358: + "/{http://www.wfmc.org/2002/XPDL1.0}Activity"
359: + "/{http://www.wfmc.org/2002/XPDL1.0}TransitionRestrictions",
360: SubHandler.class);
361: HandlerStack stack = new HandlerStack(reader, dh);
362: stack.setContextData("innerTags", innerTags);
363: reader.parse(new InputSource(Sax.class
364: .getResourceAsStream("/util/testXPDL.xml")));
365: assertTrue(outerTags.contains("Activity"));
366: assertTrue(outerTags.contains("TransitionRestrictions"));
367: assertTrue(!outerTags.contains("TransitionRestriction"));
368: assertTrue(innerTags.contains("TransitionRestrictions"));
369: assertTrue(innerTags.contains("TransitionRestriction"));
370: assertTrue(!innerTags.contains("Activity"));
371: }
372:
373: }
|