View Javadoc

1   /*
2    * Copyright 2009 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may	obtain a copy of the License at
7    *
8    * 	http://www.osedu.org/licenses/ECL-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.contract.writer;
17  
18  import java.io.PrintStream;
19  
20  /**
21   * Base class for all XML writers
22   * @author nwright
23   */
24  public class XmlWriter {
25  
26      private char indentChar = '\t';
27      private int indent;
28      private PrintStream out;
29  
30      public XmlWriter() {
31      }
32  
33      public XmlWriter(PrintStream out, int indent) {
34          this.indent = indent;
35          this.out = out;
36      }
37  
38      public void setOut(PrintStream out) {
39          this.out = out;
40      }
41  
42      public void setIndent(int indent) {
43          this.indent = indent;
44      }
45  
46      public int getIndent() {
47          return indent;
48      }
49  
50      public void incrementIndent() {
51          indent++;
52      }
53  
54      public void decrementIndent() {
55          indent--;
56      }
57  
58      public PrintStream getOut() {
59          return out;
60      }
61  
62      public void indent() {
63          indent(out, indentChar);
64      }
65  
66      public void indent(PrintStream o, char indentCharacter) {
67          for (int i = 0; i < indent; i++) {
68              o.print(indentCharacter);
69          }
70      }
71  
72      public void println(String str) {
73          out.println(str);
74      }
75  
76      public void indentPrintln(String str) {
77          indent();
78          out.println(str);
79      }
80  
81      public void indentPrint(String str) {
82          indent();
83          out.print(str);
84      }
85  
86      public void print(String str) {
87          out.print(str);
88      }
89  
90      public void writeAttribute(String attribute, String value) {
91          if (value == null) {
92              return;
93          }
94          if (value.equals("")) {
95              return;
96          }
97          out.print(" ");
98          out.print(attribute);
99          out.print("=\"");
100         out.print(value);
101         out.print("\"");
102     }
103 
104     public void writeTag(String tag, String value) {
105         writeTag(tag, null, value);
106     }
107 
108     public void writeTag(String tag, String modifiers, String value) {
109         if (value == null) {
110             return;
111         }
112         if (value.equals("")) {
113             return;
114         }
115         indent();
116         out.print("<" + tag);
117         if (modifiers != null && !modifiers.isEmpty()) {
118             out.print(" " + modifiers);
119         }
120         out.print(">");
121 
122         out.print(value);
123         out.print("</" + tag + ">");
124         out.println("");
125     }
126 
127     public void writeComment(String comment) {
128         if (comment == null) {
129             return;
130         }
131         if (comment.equals("")) {
132             return;
133         }
134         indent();
135         out.print("<!-- ");
136         out.print(comment);
137         out.println(" -->");
138     }
139 
140     public String escapeXML(String s) {
141         StringBuffer sb = new StringBuffer();
142         int n = s.length();
143         for (int i = 0; i < n; i++) {
144             // http://www.hdfgroup.org/HDF5/XML/xml_escape_chars.htm
145             char c = s.charAt(i);
146             switch (c) {
147                 case '"':
148                     sb.append("&quot;");
149                     break;
150                 case '\'':
151                     sb.append("&apos;");
152                     break;
153                 case '&':
154                     sb.append("&amp;");
155                     break;
156                 case '<':
157                     sb.append("&lt;");
158                     break;
159                 case '>':
160                     sb.append("&gt;");
161                     break;
162 
163 
164                 //case ' ': sb.append("&nbsp;");break;
165 
166                 default:
167                     sb.append(c);
168                     break;
169             }
170         }
171         return sb.toString();
172     }
173 
174     public void writeCommentBox(String comment) {
175         String border =
176                 "***************************************************************";
177         while (border.length() < comment.length()) {
178             border = border + border;
179         }
180         border = border.substring(0, comment.length());
181         writeComment(border);
182         writeComment(comment);
183         writeComment(border);
184     }
185 }