PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/castor-1.3.3/ddlgen/src/main/java/org/castor/ddlgen/DDLWriter.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 463 lines | 220 code | 51 blank | 192 comment | 20 complexity | 3715c8384f21d3bf99f638a9ad8f667b MD5 | raw file
  1. /*
  2. * Copyright 2007 Le Duc Bao, Ralf Joachim
  3. *
  4. * Licensed under the Apache 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.apache.org/licenses/LICENSE-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.castor.ddlgen;
  17. import java.io.BufferedWriter;
  18. import java.io.IOException;
  19. import java.io.InterruptedIOException;
  20. import java.io.OutputStream;
  21. import java.io.OutputStreamWriter;
  22. import java.io.Writer;
  23. import java.text.MessageFormat;
  24. /**
  25. * Replace PrintStream and StringBuffer by a Writer implementation
  26. * We have various properties to configure output that are in-depended of the schema object:
  27. * <li/>org.castor.ddlgen.CharFormat=SENSITIVE, UPPER and LOWER
  28. * <li/>org.castor.ddlgen.Newline=\n
  29. * <li/>org.castor.ddlgen.Indention=\t
  30. *
  31. * These properties are accessed at various places all around ddlgen at the moment.The idea
  32. * is that these properties are set only once at the new Writer and do not need to be
  33. * accessed elsewhere. This has the following advantages:
  34. * <li/>improved performance as the properties don't need to be accessed for every object to output
  35. * <li/>functionallity to format genertaed ddl is concentrated in one class: the new Writer
  36. * <li/>all the toDDL(), toDropDDL(), toCreateDDL() methods get much shorter
  37. *
  38. * I thought of the following interface for the new Writer (not complete):
  39. * <li/>write(String) outputs String as is
  40. * <li/>writeln(String) calls write(String) followed by newline()
  41. * <li/>newline() output newline and indention of next line
  42. * <li/>indent() increases indention
  43. * <li/>unindent() decreases indention
  44. *
  45. * More write() and writeln() methods for other data types may be added on demand. A further
  46. * improvement could be to offer write(String, Object[]) methods that internally use
  47. * MessageFormat. This would enable us to use a pattern based approach for DDL generation.
  48. * These patterns may sometimes be much easier to read and maintain.
  49. *
  50. * In addition to the introduction of the new Writer it will be required to pass an instance
  51. * of the Writer to every method where DDL gets generated. Therefore the parameterless
  52. * toCreate() method have to be changed to toCreateDDL(DDLWriter). This also applies to other
  53. * such methods.
  54. *
  55. * @author <a href="mailto:leducbao@gmail.com">Le Duc Bao</a>
  56. * @version $Revision: 8993 $ $Date: 2011-08-02 01:28:52 +0200 (Di, 02 Aug 2011) $
  57. * @since 1.1.2
  58. */
  59. public final class DDLWriter extends Writer {
  60. //--------------------------------------------------------------------------
  61. /** Print writer to write all output to. */
  62. private Writer _writer;
  63. /** Remember errors. */
  64. private IOException _error = null;
  65. /** Newline flag is turn on at a new line before any other character gets written. */
  66. private boolean _isNewline = true;
  67. /** Current indent level. */
  68. private int _indentLevel = 0;
  69. /** String to output to indent a line. */
  70. private String _indentText = DDLGenConfiguration.DEFAULT_INDENT;
  71. /** String to output for a new line. */
  72. private String _newline = DDLGenConfiguration.DEFAULT_NEWLINE;
  73. /** Character format defined by CHAR_FORMAT_KEY in configuration file. */
  74. private String _chrFormat = DDLGenConfiguration.CHAR_FORMAT_SENSITIVE;
  75. //--------------------------------------------------------------------------
  76. /**
  77. * Construct new DDLWriter with given output stream and configuration file.
  78. *
  79. * @param output Output stream to write output characters to.
  80. * @param conf Configuration.
  81. */
  82. public DDLWriter(final OutputStream output, final Configuration conf) {
  83. this(new BufferedWriter(new OutputStreamWriter(output)), conf);
  84. }
  85. /**
  86. * Construct new DDLWriter with given writer and configuration file.
  87. *
  88. * @param writer Writer to write output characters to.
  89. * @param conf Configuration.
  90. */
  91. public DDLWriter(final Writer writer, final Configuration conf) {
  92. super(writer);
  93. _writer = writer;
  94. _newline = conf.getStringValue(
  95. DDLGenConfiguration.NEWLINE_KEY,
  96. DDLGenConfiguration.DEFAULT_NEWLINE);
  97. _indentText = conf.getStringValue(
  98. DDLGenConfiguration.INDENT_KEY,
  99. DDLGenConfiguration.DEFAULT_INDENT);
  100. _chrFormat = conf.getStringValue(
  101. DDLGenConfiguration.CHAR_FORMAT_KEY,
  102. DDLGenConfiguration.CHAR_FORMAT_SENSITIVE);
  103. }
  104. /**
  105. * Flush the writer.
  106. */
  107. public void flush() {
  108. try {
  109. synchronized (lock) {
  110. if (_writer == null) { throw new IOException("Writer closed."); }
  111. _writer.flush();
  112. }
  113. } catch (IOException ex) {
  114. _error = ex;
  115. }
  116. }
  117. /**
  118. * Close the writer.
  119. */
  120. public void close () {
  121. try {
  122. synchronized (lock) {
  123. if (_writer != null) {
  124. _writer.close();
  125. _writer = null;
  126. }
  127. }
  128. } catch (IOException ex) {
  129. _error = ex;
  130. }
  131. }
  132. /**
  133. * Check if any error occured at previous operations of the writer. If an IOException was
  134. * caught at any previous operation of the writer it will be thrown now.
  135. *
  136. * @throws IOException IOException caught at any previous operation of the writer.
  137. */
  138. public void checkError() throws IOException {
  139. if (_error != null) { throw _error; }
  140. }
  141. //--------------------------------------------------------------------------
  142. /**
  143. * Increase indention by 1.
  144. */
  145. public void indent() {
  146. _indentLevel++;
  147. }
  148. /**
  149. * Decrease indention by 1.
  150. */
  151. public void unindent() {
  152. _indentLevel = (_indentLevel <= 0) ? 0 : _indentLevel - 1;
  153. }
  154. //--------------------------------------------------------------------------
  155. /**
  156. * {@inheritDoc}
  157. */
  158. public void write(final char[] buf, final int off, final int len) {
  159. write(new String(buf, off, len));
  160. }
  161. /**
  162. * {@inheritDoc}
  163. */
  164. public void write(final char[] buf) {
  165. write(new String(buf));
  166. }
  167. /**
  168. * {@inheritDoc}
  169. */
  170. public void write(final int c) {
  171. write(new String(new char[] {(char) c}));
  172. }
  173. /**
  174. * {@inheritDoc}
  175. */
  176. public void write(final String s, final int off, final int len) {
  177. write(s.substring(off, off + len));
  178. }
  179. /**
  180. * {@inheritDoc}
  181. */
  182. public void write(final String s) {
  183. if (s != null) {
  184. try {
  185. synchronized (lock) {
  186. if (_writer == null) { throw new IOException("Writer closed."); }
  187. if (DDLGenConfiguration.CHAR_FORMAT_LOWER.equalsIgnoreCase(_chrFormat)) {
  188. _writer.write(s.toLowerCase());
  189. } else if (DDLGenConfiguration.CHAR_FORMAT_UPPER.equalsIgnoreCase(_chrFormat)) {
  190. _writer.write(s.toUpperCase());
  191. } else {
  192. _writer.write(s);
  193. }
  194. }
  195. } catch (InterruptedIOException ex) {
  196. Thread.currentThread().interrupt();
  197. } catch (IOException ex) {
  198. _error = ex;
  199. }
  200. }
  201. }
  202. /**
  203. * Write indention.
  204. */
  205. private void writeIndention() {
  206. try {
  207. synchronized (lock) {
  208. if (_writer == null) { throw new IOException("Writer closed."); }
  209. if (_isNewline) {
  210. for (int i = 0; i < _indentLevel; i++) {
  211. _writer.write(_indentText);
  212. }
  213. _isNewline = false;
  214. }
  215. }
  216. } catch (InterruptedIOException ex) {
  217. Thread.currentThread().interrupt();
  218. } catch (IOException ex) {
  219. _error = ex;
  220. }
  221. }
  222. /**
  223. * Write newline.
  224. */
  225. private void writeNewline() {
  226. try {
  227. synchronized (lock) {
  228. if (_writer == null) { throw new IOException("Writer closed."); }
  229. _isNewline = true;
  230. _writer.write(_newline);
  231. }
  232. } catch (InterruptedIOException ex) {
  233. Thread.currentThread().interrupt();
  234. } catch (IOException ex) {
  235. _error = ex;
  236. }
  237. }
  238. //--------------------------------------------------------------------------
  239. /**
  240. * Print an array of characters.
  241. *
  242. * @param chars Array of chars to be printed.
  243. */
  244. public void print(final char[] chars) {
  245. synchronized (lock) {
  246. writeIndention();
  247. write(chars);
  248. }
  249. }
  250. /**
  251. * Print a double-precision floating-point number.
  252. *
  253. * @param number Double to be printed.
  254. */
  255. public void print(final double number) {
  256. synchronized (lock) {
  257. writeIndention();
  258. write(String.valueOf(number));
  259. }
  260. }
  261. /**
  262. * Print an integer number.
  263. *
  264. * @param number Integer to be printed.
  265. */
  266. public void print(final int number) {
  267. synchronized (lock) {
  268. writeIndention();
  269. write(String.valueOf(number));
  270. }
  271. }
  272. /**
  273. * Print a long number.
  274. *
  275. * @param number Long to be printed.
  276. */
  277. public void print(final long number) {
  278. synchronized (lock) {
  279. writeIndention();
  280. write(String.valueOf(number));
  281. }
  282. }
  283. /**
  284. * Print an object.
  285. *
  286. * @param object Object to be printed.
  287. */
  288. public void print(final Object object) {
  289. synchronized (lock) {
  290. writeIndention();
  291. write(String.valueOf(object));
  292. }
  293. }
  294. /**
  295. * Print a string.
  296. *
  297. * @param string String to be printed.
  298. */
  299. public void print(final String string) {
  300. synchronized (lock) {
  301. writeIndention();
  302. write(string);
  303. }
  304. }
  305. /**
  306. * A convenience method to print a formatted string build by filling placeholders of the
  307. * specified pattern with given arguments.
  308. *
  309. * @param pattern Pattern with placeholders.
  310. * @param arguments Arguments to replace placeholders in pattern.
  311. */
  312. public void print(final String pattern, final Object[] arguments) {
  313. synchronized (lock) {
  314. writeIndention();
  315. write(MessageFormat.format(pattern, arguments));
  316. }
  317. }
  318. //--------------------------------------------------------------------------
  319. /**
  320. * Terminate the current line by writing the line separator string.
  321. */
  322. public void println() {
  323. writeNewline();
  324. }
  325. /**
  326. * Print an array of characters and terminate the line.
  327. *
  328. * @param chars Array of chars to be printed.
  329. */
  330. public void println(final char[] chars) {
  331. synchronized (lock) {
  332. writeIndention();
  333. write(chars);
  334. writeNewline();
  335. }
  336. }
  337. /**
  338. * Print a double-precision floating-point number and terminate the line.
  339. *
  340. * @param number Double to be printed.
  341. */
  342. public void println(final double number) {
  343. synchronized (lock) {
  344. writeIndention();
  345. write(String.valueOf(number));
  346. writeNewline();
  347. }
  348. }
  349. /**
  350. * Print an integer number and terminate the line.
  351. *
  352. * @param number Integer to be printed.
  353. */
  354. public void println(final int number) {
  355. synchronized (lock) {
  356. writeIndention();
  357. write(String.valueOf(number));
  358. writeNewline();
  359. }
  360. }
  361. /**
  362. * Print a long number and terminate the line.
  363. *
  364. * @param number Long to be printed.
  365. */
  366. public void println(final long number) {
  367. synchronized (lock) {
  368. writeIndention();
  369. write(String.valueOf(number));
  370. writeNewline();
  371. }
  372. }
  373. /**
  374. * Print an object and terminate the line.
  375. *
  376. * @param object Object to be printed.
  377. */
  378. public void println(final Object object) {
  379. synchronized (lock) {
  380. writeIndention();
  381. write(String.valueOf(object));
  382. writeNewline();
  383. }
  384. }
  385. /**
  386. * Print a string and terminate the line.
  387. *
  388. * @param string String to be printed.
  389. */
  390. public void println(final String string) {
  391. synchronized (lock) {
  392. writeIndention();
  393. write(string);
  394. writeNewline();
  395. }
  396. }
  397. /**
  398. * A convenience method to print a formatted string build by filling placeholders of the
  399. * specified pattern with given arguments. Line will be terminated after the formatted string.
  400. *
  401. * @param pattern Pattern with placeholders.
  402. * @param arguments Arguments to replace placeholders in pattern.
  403. */
  404. public void println(final String pattern, final Object[] arguments) {
  405. synchronized (lock) {
  406. writeIndention();
  407. write(MessageFormat.format(pattern, arguments));
  408. writeNewline();
  409. }
  410. }
  411. //--------------------------------------------------------------------------
  412. }