1 /* 2 * Copyright 2013 smartics, Kronseder & Reiner GmbH 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 de.smartics.html5.jatl; 17 18 import java.io.BufferedWriter; 19 import java.io.OutputStream; 20 import java.io.OutputStreamWriter; 21 import java.nio.charset.Charset; 22 23 import javax.enterprise.context.ApplicationScoped; 24 25 import de.smartics.util.lang.Arg; 26 27 /** 28 * Factory to create HTML instances for the creation of HTML documents with 29 * {@link Html}. 30 */ 31 @ApplicationScoped 32 public class HtmlFactory 33 { 34 // ********************************* Fields ********************************* 35 36 // --- constants ------------------------------------------------------------ 37 38 // --- members -------------------------------------------------------------- 39 40 /** 41 * The size of the buffer to write to. 42 * <p> 43 * The default is set to <code>8192</code>. 44 * </p> 45 */ 46 private int bufferSize = 8192; 47 48 /** 49 * The character set to use. 50 * <p> 51 * The default is set to <code>UTF-8</code>. 52 * </p> 53 */ 54 private Charset charset = Charset.forName("UTF-8"); //ISO-8859-1 55 56 // ****************************** Initializer ******************************* 57 58 // ****************************** Constructors ****************************** 59 60 // ****************************** Inner Classes ***************************** 61 62 // ********************************* Methods ******************************** 63 64 // --- init ----------------------------------------------------------------- 65 66 // --- get&set -------------------------------------------------------------- 67 68 /** 69 * Returns the size of the buffer to write to. 70 * <p> 71 * The default is set to <code>8192</code>. 72 * </p> 73 * 74 * @return the size of the buffer to write to. 75 */ 76 public int getBufferSize() 77 { 78 return bufferSize; 79 } 80 81 /** 82 * Sets the size of the buffer to write to. 83 * <p> 84 * The default is set to <code>8192</code>. 85 * </p> 86 * 87 * @param bufferSize the size of the buffer to write to. 88 * @throws IllegalArgumentException if buffer size is zero or less. 89 */ 90 public void setBufferSize(final int bufferSize) 91 throws IllegalArgumentException 92 { 93 if (bufferSize <= 0) 94 { 95 throw new IllegalArgumentException( 96 "The buffer size is required to be larger than zero, but was " 97 + bufferSize + '.'); 98 } 99 this.bufferSize = bufferSize; 100 } 101 102 /** 103 * Returns the character set to use. 104 * <p> 105 * The default is set to <code>UTF-8</code>. 106 * </p> 107 * 108 * @return the character set to use. 109 */ 110 public Charset getCharset() 111 { 112 return charset; 113 } 114 115 /** 116 * Sets the character set to use. 117 * <p> 118 * The default is set to <code>UTF-8</code>. 119 * </p> 120 * 121 * @param charset the character set to use. 122 * @throws NullPointerException if {@code charset} is <code>null</code>. 123 */ 124 public void setCharset(final Charset charset) throws NullPointerException 125 { 126 this.charset = Arg.checkNotNull("charset", charset); 127 } 128 129 // --- business ------------------------------------------------------------- 130 131 /** 132 * Creates the {@link Html} instance based on the properties set to the 133 * factory. 134 * 135 * @param output the output to write to. 136 * @return the {@link Html} instance. 137 * @throws NullPointerException if {@code output} is <code>null</code>. 138 */ 139 public Html create(final OutputStream output) throws NullPointerException 140 { 141 final BufferedWriter writer = 142 new BufferedWriter(new OutputStreamWriter(Arg.checkNotNull("output", 143 output), charset), bufferSize); 144 145 final Html html = new Html(writer); 146 return html; 147 } 148 149 // --- object basics -------------------------------------------------------- 150 151 }