KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > samples > NormalizingFactory


1 /* Copyright 2002-2004 Elliotte Rusty Harold
2    
3    This library is free software; you can redistribute it and/or modify
4    it under the terms of version 2.1 of the GNU Lesser General Public
5    License as published by the Free Software Foundation.
6    
7    This library is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10    GNU Lesser General Public License for more details.
11    
12    You should have received a copy of the GNU Lesser General Public
13    License along with this library; if not, write to the
14    Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15    Boston, MA 02111-1307 USA
16    
17    You can contact Elliotte Rusty Harold by sending e-mail to
18    elharo@metalab.unc.edu. Please include the word "XOM" in the
19    subject line. The XOM home page is located at http://www.xom.nu/
20 */

21
22 package nu.xom.samples;
23
24 import java.io.IOException JavaDoc;
25
26 import nu.xom.Attribute;
27 import nu.xom.Builder;
28 import nu.xom.Document;
29 import nu.xom.NodeFactory;
30 import nu.xom.Nodes;
31 import nu.xom.ParsingException;
32 import nu.xom.Serializer;
33
34 /**
35  * <p>
36  * Demonstrates a custom <code>NodeFactory</code> that normalizes
37  * all white space in text nodes and attribute values.
38  * Normalization involves stripping all leading and trailing white
39  * space, converting all tabs, carriage returns, and line feeds to a
40  * single space each, and then converting all remaining runs of white
41  * space to a single space. Text nodes that contain nothing after
42  * this process is applied are not created. This may be useful for
43  * record-like XML.
44  * </p>
45  *
46  * <p>
47  * This class does <strong>not</strong> perform
48  * Unicode normalization.
49  * </p>
50  *
51  * @author Elliotte Rusty Harold
52  * @version 1.0
53  *
54  */

55 public class NormalizingFactory extends NodeFactory {
56
57     private Nodes empty = new Nodes();
58
59     // We don't need text nodes at all
60
public Nodes makeText(String JavaDoc data) {
61         data = normalizeSpace(data);
62         if ("".equals(data)) return empty;
63         return super.makeText(data);
64     }
65
66     public Nodes makeAttribute(String JavaDoc name, String JavaDoc URI,
67       String JavaDoc value, Attribute.Type type) {
68         value = normalizeSpace(value);
69         return super.makeAttribute(name, URI, value, type);
70     }
71
72     
73     // not the most efficient implementation
74
private static String JavaDoc normalizeSpace(String JavaDoc data) {
75         data = data.replace('\t', ' ');
76         data = data.replace('\n', ' ');
77         data = data.replace('\r', ' ');
78         data = data.trim();
79         
80         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
81         for (int i = 0; i < data.length(); i++) {
82             if (i == 0 || data.charAt(i-1) != ' '
83               || data.charAt(i) != ' ') {
84                 result.append(data.charAt(i));
85             }
86         }
87         
88         return result.toString();
89     }
90     
91     
92     public static void main(String JavaDoc[] args) {
93   
94         if (args.length == 0) {
95           System.out.println(
96             "Usage: java nu.xom.samples.NormalizingFactory URL"
97           );
98           return;
99         }
100           
101         Builder builder = new Builder(new NormalizingFactory());
102          
103         try {
104             Document doc = builder.build(args[0]);
105             Serializer serializer = new Serializer(System.out);
106             serializer.write(doc);
107         }
108         // indicates a well-formedness error
109
catch (ParsingException ex) {
110             System.out.println(args[0] + " is not well-formed.");
111             System.out.println(ex.getMessage());
112         }
113         catch (IOException JavaDoc ex) {
114             System.out.println(ex);
115         }
116       
117     }
118
119 }
Popular Tags