001/** 002 * Copyright 2004-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.student.contract.writer; 017 018import java.io.PrintStream; 019 020/** 021 * Base class for all XML writers 022 * @author nwright 023 */ 024public class XmlWriter { 025 026 private char indentChar = '\t'; 027 private int indent; 028 private PrintStream out; 029 030 public XmlWriter() { 031 } 032 033 public XmlWriter(PrintStream out, int indent) { 034 this.indent = indent; 035 this.out = out; 036 } 037 038 public void setOut(PrintStream out) { 039 this.out = out; 040 } 041 042 public void setIndent(int indent) { 043 this.indent = indent; 044 } 045 046 public int getIndent() { 047 return indent; 048 } 049 050 public void incrementIndent() { 051 indent++; 052 } 053 054 public void decrementIndent() { 055 indent--; 056 } 057 058 public PrintStream getOut() { 059 return out; 060 } 061 062 public void indent() { 063 indent(out, indentChar); 064 } 065 066 public void indent(PrintStream o, char indentCharacter) { 067 for (int i = 0; i < indent; i++) { 068 o.print(indentCharacter); 069 } 070 } 071 072 public void println(String str) { 073 out.println(str); 074 } 075 076 public void indentPrintln(String str) { 077 indent(); 078 out.println(str); 079 } 080 081 public void indentPrint(String str) { 082 indent(); 083 out.print(str); 084 } 085 086 public void print(String str) { 087 out.print(str); 088 } 089 090 public void writeAttribute(String attribute, String value) { 091 if (value == null) { 092 return; 093 } 094 if (value.equals("")) { 095 return; 096 } 097 out.print(" "); 098 out.print(attribute); 099 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("""); 149 break; 150 case '\'': 151 sb.append("'"); 152 break; 153 case '&': 154 sb.append("&"); 155 break; 156 case '<': 157 sb.append("<"); 158 break; 159 case '>': 160 sb.append(">"); 161 break; 162 163 164 //case ' ': sb.append(" ");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}