KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > util > XMLEncoder


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.util;
21
22 import java.io.PrintWriter JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26
27 /**
28  * A helper class to encode objects to XML.
29  *
30  * @since 1.1
31  * @author Andrus Adamchik
32  */

33 public class XMLEncoder {
34     protected String JavaDoc indent;
35     protected PrintWriter JavaDoc out;
36
37     protected boolean indentLine;
38     protected int indentTimes;
39
40     public XMLEncoder(PrintWriter JavaDoc out) {
41         this.out = out;
42     }
43
44     public XMLEncoder(PrintWriter JavaDoc out, String JavaDoc indent) {
45         this.indent = indent;
46         this.out = out;
47     }
48
49     public PrintWriter JavaDoc getPrintWriter() {
50         return out;
51     }
52
53     public void indent(int i) {
54         indentTimes += i;
55         if (indentTimes < 0) {
56             indentTimes = 0;
57         }
58     }
59
60     /**
61      * Utility method that prints all map values,
62      * assuming they are XMLSerializable objects.
63      */

64     public void print(Map JavaDoc map) {
65         Iterator JavaDoc it = map.entrySet().iterator();
66         while (it.hasNext()) {
67             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
68             ((XMLSerializable) entry.getValue()).encodeAsXML(this);
69         }
70     }
71
72     /**
73      * Utility method that prints all map values,
74      * assuming they are XMLSerializable objects.
75      */

76     public void print(Collection JavaDoc c) {
77         Iterator JavaDoc it = c.iterator();
78         while (it.hasNext()) {
79             ((XMLSerializable) it.next()).encodeAsXML(this);
80         }
81     }
82
83     /**
84      * Prints a common XML element - property with name and value.
85      */

86     public void printProperty(String JavaDoc name, String JavaDoc value) {
87         printIndent();
88         out.print("<property name=\"");
89         out.print(name);
90         out.print("\" value=\"");
91         out.print(value);
92         out.println("\"/>");
93         indentLine = true;
94     }
95
96     /**
97      * Prints a common XML element - property with name and value.
98      */

99     public void printProperty(String JavaDoc name, boolean b) {
100         printProperty(name, String.valueOf(b));
101     }
102
103     /**
104      * Prints a common XML element - property with name and value.
105      */

106     public void printProperty(String JavaDoc name, int i) {
107         printProperty(name, String.valueOf(i));
108     }
109
110     public void print(String JavaDoc text) {
111         printIndent();
112         out.print(text);
113     }
114
115     public void print(char c) {
116         printIndent();
117         out.print(c);
118     }
119
120     public void print(boolean b) {
121         printIndent();
122         out.print(b);
123     }
124
125     public void print(int i) {
126         printIndent();
127         out.print(i);
128     }
129
130     public void println(String JavaDoc text) {
131         printIndent();
132         out.println(text);
133         indentLine = true;
134     }
135
136     public void println(char c) {
137         printIndent();
138         out.println(c);
139         indentLine = true;
140     }
141
142     private void printIndent() {
143         if (indentLine) {
144             indentLine = false;
145
146             if (indentTimes > 0 && indent != null) {
147                 for (int i = 0; i < indentTimes; i++) {
148                     out.print(indent);
149                 }
150             }
151         }
152     }
153 }
154
Popular Tags