Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.internal.ws.processor.util; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.tools.internal.ws.processor.generator.GeneratorException; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import java.io.BufferedWriter; |
aoqi@0 | 31 | import java.io.IOException; |
aoqi@0 | 32 | import java.io.Writer; |
aoqi@0 | 33 | import java.nio.charset.Charset; |
aoqi@0 | 34 | import java.nio.charset.CharsetEncoder; |
aoqi@0 | 35 | import java.text.MessageFormat; |
aoqi@0 | 36 | |
aoqi@0 | 37 | /** |
aoqi@0 | 38 | * |
aoqi@0 | 39 | * @author WS Development Team |
aoqi@0 | 40 | */ |
aoqi@0 | 41 | public class IndentingWriter extends BufferedWriter { |
aoqi@0 | 42 | |
aoqi@0 | 43 | private boolean beginningOfLine = true; |
aoqi@0 | 44 | private int currentIndent = 0; |
aoqi@0 | 45 | private int indentStep = 4; |
aoqi@0 | 46 | |
aoqi@0 | 47 | public IndentingWriter(Writer out) { |
aoqi@0 | 48 | super(out); |
aoqi@0 | 49 | } |
aoqi@0 | 50 | |
aoqi@0 | 51 | public IndentingWriter(Writer out,int step) { |
aoqi@0 | 52 | this(out); |
aoqi@0 | 53 | |
aoqi@0 | 54 | if (indentStep < 0) { |
aoqi@0 | 55 | throw new IllegalArgumentException("negative indent step"); |
aoqi@0 | 56 | } |
aoqi@0 | 57 | indentStep = step; |
aoqi@0 | 58 | } |
aoqi@0 | 59 | |
aoqi@0 | 60 | public void write(int c) throws IOException { |
aoqi@0 | 61 | checkWrite(); |
aoqi@0 | 62 | super.write(c); |
aoqi@0 | 63 | } |
aoqi@0 | 64 | |
aoqi@0 | 65 | public void write(char[] cbuf, int off, int len) throws IOException { |
aoqi@0 | 66 | if (len > 0) { |
aoqi@0 | 67 | checkWrite(); |
aoqi@0 | 68 | } |
aoqi@0 | 69 | super.write(cbuf, off, len); |
aoqi@0 | 70 | } |
aoqi@0 | 71 | |
aoqi@0 | 72 | public void write(String s, int off, int len) throws IOException { |
aoqi@0 | 73 | if (len > 0) { |
aoqi@0 | 74 | checkWrite(); |
aoqi@0 | 75 | } |
aoqi@0 | 76 | super.write(s, off, len); |
aoqi@0 | 77 | } |
aoqi@0 | 78 | |
aoqi@0 | 79 | public void newLine() throws IOException { |
aoqi@0 | 80 | super.newLine(); |
aoqi@0 | 81 | beginningOfLine = true; |
aoqi@0 | 82 | } |
aoqi@0 | 83 | |
aoqi@0 | 84 | protected void checkWrite() throws IOException { |
aoqi@0 | 85 | if (beginningOfLine) { |
aoqi@0 | 86 | beginningOfLine = false; |
aoqi@0 | 87 | int i = currentIndent; |
aoqi@0 | 88 | while (i > 0) { |
aoqi@0 | 89 | super.write(' '); |
aoqi@0 | 90 | -- i; |
aoqi@0 | 91 | } |
aoqi@0 | 92 | } |
aoqi@0 | 93 | } |
aoqi@0 | 94 | |
aoqi@0 | 95 | protected void indentIn() { |
aoqi@0 | 96 | currentIndent += indentStep; |
aoqi@0 | 97 | } |
aoqi@0 | 98 | |
aoqi@0 | 99 | protected void indentOut() { |
aoqi@0 | 100 | currentIndent -= indentStep; |
aoqi@0 | 101 | if (currentIndent < 0) { |
aoqi@0 | 102 | currentIndent = 0; |
aoqi@0 | 103 | } |
aoqi@0 | 104 | } |
aoqi@0 | 105 | |
aoqi@0 | 106 | public void pI() { |
aoqi@0 | 107 | indentIn(); |
aoqi@0 | 108 | } |
aoqi@0 | 109 | |
aoqi@0 | 110 | public void pO() { |
aoqi@0 | 111 | indentOut(); |
aoqi@0 | 112 | } |
aoqi@0 | 113 | |
aoqi@0 | 114 | public void pI(int levels) { |
aoqi@0 | 115 | for (int i = 0; i < levels; ++i) { |
aoqi@0 | 116 | indentIn(); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | } |
aoqi@0 | 119 | |
aoqi@0 | 120 | public void pO(int levels) { |
aoqi@0 | 121 | for (int i = 0; i < levels; ++i) { |
aoqi@0 | 122 | indentOut(); |
aoqi@0 | 123 | } |
aoqi@0 | 124 | } |
aoqi@0 | 125 | |
aoqi@0 | 126 | public void p(String s) throws IOException { |
aoqi@0 | 127 | /* |
aoqi@0 | 128 | int tabCount = 0; |
aoqi@0 | 129 | for (int i = 0; i < s.length(); ++i) { |
aoqi@0 | 130 | if (s.charAt(i) == '\t') { |
aoqi@0 | 131 | ++tabCount; |
aoqi@0 | 132 | indentIn(); |
aoqi@0 | 133 | } |
aoqi@0 | 134 | } |
aoqi@0 | 135 | |
aoqi@0 | 136 | String printStr = s.substring(tabCount); |
aoqi@0 | 137 | */ |
aoqi@0 | 138 | boolean canEncode = true; |
aoqi@0 | 139 | |
aoqi@0 | 140 | //bug fix: 4839636 |
aoqi@0 | 141 | try{ |
aoqi@0 | 142 | if(!canEncode(s)) { |
aoqi@0 | 143 | canEncode = false; |
aoqi@0 | 144 | } |
aoqi@0 | 145 | } catch (Throwable t) { |
aoqi@0 | 146 | |
aoqi@0 | 147 | // there was some exception, what should we do? |
aoqi@0 | 148 | // lets ignore it for now and proceed with the code generation! |
aoqi@0 | 149 | } |
aoqi@0 | 150 | |
aoqi@0 | 151 | if(!canEncode) { |
aoqi@0 | 152 | throw new GeneratorException( |
aoqi@0 | 153 | "generator.indentingwriter.charset.cantencode", s); |
aoqi@0 | 154 | } |
aoqi@0 | 155 | write(s); |
aoqi@0 | 156 | /* |
aoqi@0 | 157 | while (tabCount-- > 0) { |
aoqi@0 | 158 | indentOut(); |
aoqi@0 | 159 | } |
aoqi@0 | 160 | */ |
aoqi@0 | 161 | } |
aoqi@0 | 162 | |
aoqi@0 | 163 | /** |
aoqi@0 | 164 | * Check if encode can handle the chars in this string. |
aoqi@0 | 165 | * |
aoqi@0 | 166 | */ |
aoqi@0 | 167 | protected boolean canEncode(String s) { |
aoqi@0 | 168 | final CharsetEncoder encoder = |
aoqi@0 | 169 | Charset.forName(System.getProperty("file.encoding")).newEncoder(); |
aoqi@0 | 170 | char[] chars = s.toCharArray(); |
aoqi@0 | 171 | for (int i=0; i<chars.length; i++) { |
aoqi@0 | 172 | if(!encoder.canEncode(chars[i])) { |
aoqi@0 | 173 | return false; |
aoqi@0 | 174 | } |
aoqi@0 | 175 | } |
aoqi@0 | 176 | return true; |
aoqi@0 | 177 | } |
aoqi@0 | 178 | |
aoqi@0 | 179 | public void p(String s1, String s2) throws IOException { |
aoqi@0 | 180 | p(s1); |
aoqi@0 | 181 | p(s2); |
aoqi@0 | 182 | } |
aoqi@0 | 183 | |
aoqi@0 | 184 | public void p(String s1, String s2, String s3) throws IOException { |
aoqi@0 | 185 | p(s1); |
aoqi@0 | 186 | p(s2); |
aoqi@0 | 187 | p(s3); |
aoqi@0 | 188 | } |
aoqi@0 | 189 | |
aoqi@0 | 190 | public void p(String s1, String s2, String s3, String s4) throws IOException { |
aoqi@0 | 191 | p(s1); |
aoqi@0 | 192 | p(s2); |
aoqi@0 | 193 | p(s3); |
aoqi@0 | 194 | p(s4); |
aoqi@0 | 195 | } |
aoqi@0 | 196 | |
aoqi@0 | 197 | public void p(String s1, String s2, String s3, String s4, String s5) throws IOException { |
aoqi@0 | 198 | p(s1); |
aoqi@0 | 199 | p(s2); |
aoqi@0 | 200 | p(s3); |
aoqi@0 | 201 | p(s4); |
aoqi@0 | 202 | p(s5); |
aoqi@0 | 203 | } |
aoqi@0 | 204 | |
aoqi@0 | 205 | public void pln() throws IOException { |
aoqi@0 | 206 | newLine(); |
aoqi@0 | 207 | } |
aoqi@0 | 208 | |
aoqi@0 | 209 | public void pln(String s) throws IOException { |
aoqi@0 | 210 | p(s); |
aoqi@0 | 211 | pln(); |
aoqi@0 | 212 | } |
aoqi@0 | 213 | |
aoqi@0 | 214 | public void pln(String s1, String s2) throws IOException { |
aoqi@0 | 215 | p(s1, s2); |
aoqi@0 | 216 | pln(); |
aoqi@0 | 217 | } |
aoqi@0 | 218 | |
aoqi@0 | 219 | public void pln(String s1, String s2, String s3) throws IOException { |
aoqi@0 | 220 | p(s1, s2, s3); |
aoqi@0 | 221 | pln(); |
aoqi@0 | 222 | } |
aoqi@0 | 223 | |
aoqi@0 | 224 | public void pln(String s1, String s2, String s3, String s4) throws IOException { |
aoqi@0 | 225 | p(s1, s2, s3, s4); |
aoqi@0 | 226 | pln(); |
aoqi@0 | 227 | } |
aoqi@0 | 228 | |
aoqi@0 | 229 | public void pln(String s1, String s2, String s3, String s4, String s5) throws IOException { |
aoqi@0 | 230 | p(s1, s2, s3, s4, s5); |
aoqi@0 | 231 | pln(); |
aoqi@0 | 232 | } |
aoqi@0 | 233 | |
aoqi@0 | 234 | public void plnI(String s) throws IOException { |
aoqi@0 | 235 | p(s); |
aoqi@0 | 236 | pln(); |
aoqi@0 | 237 | pI(); |
aoqi@0 | 238 | } |
aoqi@0 | 239 | |
aoqi@0 | 240 | public void pO(String s) throws IOException { |
aoqi@0 | 241 | pO(); |
aoqi@0 | 242 | p(s); |
aoqi@0 | 243 | } |
aoqi@0 | 244 | |
aoqi@0 | 245 | public void pOln(String s) throws IOException { |
aoqi@0 | 246 | pO(s); |
aoqi@0 | 247 | pln(); |
aoqi@0 | 248 | } |
aoqi@0 | 249 | |
aoqi@0 | 250 | public void pOlnI(String s) throws IOException { |
aoqi@0 | 251 | pO(s); |
aoqi@0 | 252 | pln(); |
aoqi@0 | 253 | pI(); |
aoqi@0 | 254 | } |
aoqi@0 | 255 | |
aoqi@0 | 256 | public void p(Object o) throws IOException { |
aoqi@0 | 257 | write(o.toString()); |
aoqi@0 | 258 | } |
aoqi@0 | 259 | |
aoqi@0 | 260 | public void pln(Object o) throws IOException { |
aoqi@0 | 261 | p(o.toString()); |
aoqi@0 | 262 | pln(); |
aoqi@0 | 263 | } |
aoqi@0 | 264 | |
aoqi@0 | 265 | public void plnI(Object o) throws IOException { |
aoqi@0 | 266 | p(o.toString()); |
aoqi@0 | 267 | pln(); |
aoqi@0 | 268 | pI(); |
aoqi@0 | 269 | } |
aoqi@0 | 270 | |
aoqi@0 | 271 | public void pO(Object o) throws IOException { |
aoqi@0 | 272 | pO(); |
aoqi@0 | 273 | p(o.toString()); |
aoqi@0 | 274 | } |
aoqi@0 | 275 | |
aoqi@0 | 276 | public void pOln(Object o) throws IOException { |
aoqi@0 | 277 | pO(o.toString()); |
aoqi@0 | 278 | pln(); |
aoqi@0 | 279 | } |
aoqi@0 | 280 | |
aoqi@0 | 281 | public void pOlnI(Object o) throws IOException { |
aoqi@0 | 282 | pO(o.toString()); |
aoqi@0 | 283 | pln(); |
aoqi@0 | 284 | pI(); |
aoqi@0 | 285 | } |
aoqi@0 | 286 | |
aoqi@0 | 287 | public void pM(String s) throws IOException { |
aoqi@0 | 288 | int i = 0; |
aoqi@0 | 289 | while (i < s.length()) { |
aoqi@0 | 290 | int j = s.indexOf('\n', i); |
aoqi@0 | 291 | if (j == -1) { |
aoqi@0 | 292 | p(s.substring(i)); |
aoqi@0 | 293 | break; |
aoqi@0 | 294 | } else { |
aoqi@0 | 295 | pln(s.substring(i, j)); |
aoqi@0 | 296 | i = j + 1; |
aoqi@0 | 297 | } |
aoqi@0 | 298 | } |
aoqi@0 | 299 | } |
aoqi@0 | 300 | |
aoqi@0 | 301 | public void pMln(String s) throws IOException { |
aoqi@0 | 302 | pM(s); |
aoqi@0 | 303 | pln(); |
aoqi@0 | 304 | } |
aoqi@0 | 305 | |
aoqi@0 | 306 | public void pMlnI(String s) throws IOException { |
aoqi@0 | 307 | pM(s); |
aoqi@0 | 308 | pln(); |
aoqi@0 | 309 | pI(); |
aoqi@0 | 310 | } |
aoqi@0 | 311 | |
aoqi@0 | 312 | public void pMO(String s) throws IOException { |
aoqi@0 | 313 | pO(); |
aoqi@0 | 314 | pM(s); |
aoqi@0 | 315 | } |
aoqi@0 | 316 | |
aoqi@0 | 317 | public void pMOln(String s) throws IOException { |
aoqi@0 | 318 | pMO(s); |
aoqi@0 | 319 | pln(); |
aoqi@0 | 320 | } |
aoqi@0 | 321 | |
aoqi@0 | 322 | public void pF(String pattern, Object[] arguments) throws IOException { |
aoqi@0 | 323 | pM(MessageFormat.format(pattern, arguments)); |
aoqi@0 | 324 | } |
aoqi@0 | 325 | |
aoqi@0 | 326 | public void pFln(String pattern, Object[] arguments) throws IOException { |
aoqi@0 | 327 | pF(pattern, arguments); |
aoqi@0 | 328 | pln(); |
aoqi@0 | 329 | } |
aoqi@0 | 330 | } |