KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > samples > rule > SongFilter


1 /*
2  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: SongFilter.java,v 1.4 2005/01/29 14:53:14 maartenc Exp $
8  */

9
10 package org.dom4j.samples.rule;
11
12 import java.net.URL JavaDoc;
13
14 import org.dom4j.Document;
15 import org.dom4j.DocumentHelper;
16 import org.dom4j.Element;
17 import org.dom4j.Node;
18 import org.dom4j.io.OutputFormat;
19 import org.dom4j.io.SAXReader;
20 import org.dom4j.io.XMLWriter;
21 import org.dom4j.rule.Action;
22 import org.dom4j.rule.Rule;
23 import org.dom4j.rule.Stylesheet;
24
25 /**
26  * This class is 1:1 representation of the stlyesheet
27  * <code>SongFilter.xsl</code> in package.
28  *
29  * It demonstrates the usage of dom4j <i>Declarative Rule API </i>
30  *
31  * For more information see the
32  *
33  * @link{ http://www.dom4j.org/cookbook.html cookbook }.
34  *
35  * @author <a HREF="mailto:toby-wan-kenobi@gmx.de">Tobias Rademacher </a>
36  * @version $Revision $Date
37  *
38  */

39 public class SongFilter {
40
41     private Document resultDoc;
42
43     private Element songElement;
44
45     private Element currentSongElement;
46
47     private Stylesheet style;
48
49     /** Creates a new instance of SongFilter */
50     public SongFilter() {
51         this.songElement = DocumentHelper.createElement("song");
52     }
53
54     public Document filtering(org.dom4j.Document doc) throws Exception JavaDoc {
55         Element resultRoot = DocumentHelper.createElement("result");
56         this.resultDoc = DocumentHelper.createDocument(resultRoot);
57
58         Rule songElementRule = new Rule();
59         songElementRule.setPattern(DocumentHelper
60                 .createPattern("/Songs/song/mp3/id3"));
61         songElementRule.setAction(new SongElementBuilder());
62
63         Rule titleTextNodeFilter = new Rule();
64         titleTextNodeFilter.setPattern(DocumentHelper
65                 .createPattern("/Songs/song/mp3/id3/title"));
66         titleTextNodeFilter.setAction(new NodeTextFilter());
67
68         this.style = new Stylesheet();
69         this.style.addRule(songElementRule);
70         this.style.addRule(titleTextNodeFilter);
71
72         style.run(doc);
73
74         return this.resultDoc;
75     }
76
77     private class SongElementBuilder implements Action {
78         public void run(Node node) throws Exception JavaDoc {
79             currentSongElement = songElement.createCopy();
80             resultDoc.getRootElement().add(currentSongElement);
81
82             style.applyTemplates(node);
83         }
84     }
85
86     private class NodeTextFilter implements Action {
87         public void run(Node node) throws Exception JavaDoc {
88             if (currentSongElement != null) {
89                 currentSongElement.setText(node.getText());
90             }
91         }
92     }
93
94     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
95         SongFilter filter = new SongFilter();
96         URL JavaDoc source = filter.getClass().getResource(
97                 "/org/dom4j/samples/rule/Songs.xml");
98         Document result = filter.filtering(new SAXReader().read(source));
99
100         XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
101         writer.setOutputStream(System.out);
102         writer.write(result);
103
104     }
105
106 }
107
108 /*
109  * Redistribution and use of this software and associated documentation
110  * ("Software"), with or without modification, are permitted provided that the
111  * following conditions are met:
112  *
113  * 1. Redistributions of source code must retain copyright statements and
114  * notices. Redistributions must also contain a copy of this document.
115  *
116  * 2. Redistributions in binary form must reproduce the above copyright notice,
117  * this list of conditions and the following disclaimer in the documentation
118  * and/or other materials provided with the distribution.
119  *
120  * 3. The name "DOM4J" must not be used to endorse or promote products derived
121  * from this Software without prior written permission of MetaStuff, Ltd. For
122  * written permission, please contact dom4j-info@metastuff.com.
123  *
124  * 4. Products derived from this Software may not be called "DOM4J" nor may
125  * "DOM4J" appear in their names without prior written permission of MetaStuff,
126  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
127  *
128  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
129  *
130  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
131  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
132  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
133  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
134  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
135  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
136  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
137  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
138  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
139  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
140  * POSSIBILITY OF SUCH DAMAGE.
141  *
142  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
143  *
144  * $Id: SongFilter.java,v 1.4 2005/01/29 14:53:14 maartenc Exp $
145  */

146
Popular Tags