| 1 | /* |
| 2 | * @(#) $Id: AbstractXmlProcessStatusTranslator.java,v 1.1.1.1 2006/03/19 06:09:42 rossen Exp $ |
| 3 | * |
| 4 | * Copyright (c) 2006, WebHydra.com |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * * Neither the name of the WebHydra.com nor the names of its contributors |
| 16 | * may be used to endorse or promote products derived from this software |
| 17 | * without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | package com.webhydra.slug.process.xml; |
| 33 | |
| 34 | import com.webhydra.slug.process.AbstractProcessStatusTranslator; |
| 35 | import com.webhydra.slug.process.ProcessProgress; |
| 36 | import com.webhydra.slug.process.ProcessStatus; |
| 37 | import com.webhydra.slug.process.IProcessStatusTranslator; |
| 38 | import com.webhydra.slug.SlugInitializationException; |
| 39 | import java.io.IOException; |
| 40 | import java.io.InputStream; |
| 41 | import java.util.Map; |
| 42 | |
| 43 | /** |
| 44 | * Abstract XML <code>ProcessStatus</code> Translator |
| 45 | * @author rossen |
| 46 | */ |
| 47 | public abstract class AbstractXmlProcessStatusTranslator extends AbstractProcessStatusTranslator implements IProcessStatusTranslator |
| 48 | { |
| 49 | /** |
| 50 | * Instantiation parameter specifing will document docType definitions |
| 51 | */ |
| 52 | public static final String PARAM_USE_DOC_DEF = "com.webhydra.stlug.use.docdef"; |
| 53 | |
| 54 | static final String XML_TYPE_DTD = "dtd"; |
| 55 | static final String XML_TYPE_XSD = "xsd"; |
| 56 | static final String XML_TYPE_FILE_NAME = "slug-process-status"; |
| 57 | |
| 58 | /** |
| 59 | * Holds value of property useDocType. |
| 60 | */ |
| 61 | private boolean useDocType = false; |
| 62 | |
| 63 | /** |
| 64 | * Holds value of property classDef. |
| 65 | */ |
| 66 | private String classDef; |
| 67 | |
| 68 | /** Default constructor |
| 69 | */ |
| 70 | protected AbstractXmlProcessStatusTranslator() |
| 71 | { |
| 72 | setContentType("application/xml"); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Getter for the useDocType propertie. |
| 77 | * @return useDocType proeprty. |
| 78 | */ |
| 79 | protected boolean shouldUseDocType() |
| 80 | { |
| 81 | return useDocType; |
| 82 | } |
| 83 | |
| 84 | /** Retruns one the supported document types XML_TYPE_DTD or XML_TYPE_XSD. |
| 85 | */ |
| 86 | abstract protected String getDocType(); |
| 87 | |
| 88 | /** |
| 89 | * Load document type definition. |
| 90 | * @exception SlugInitializationException if IOException is encounted during |
| 91 | * Document Type loading. |
| 92 | */ |
| 93 | public void prepare() throws SlugInitializationException |
| 94 | { |
| 95 | try |
| 96 | { |
| 97 | StringBuffer doc = new StringBuffer(); |
| 98 | // String uri = "/com/webhydra/slug/xml/" + XML_TYPE_FILE_NAME + "." + getDocType(); |
| 99 | String uri = XML_TYPE_FILE_NAME + "." + getDocType(); |
| 100 | InputStream is = this.getClass().getResourceAsStream(uri); |
| 101 | while (true) |
| 102 | { |
| 103 | byte [] buff = new byte[is.available()]; |
| 104 | if (buff.length > 0) |
| 105 | { |
| 106 | is.read(buff); |
| 107 | doc.append(new String(buff)); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | classDef = doc.toString(); |
| 115 | } |
| 116 | catch (Exception e) // IOException, NullPointerException |
| 117 | { |
| 118 | throw new SlugInitializationException(e); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Overrides the default implelemntation. |
| 124 | * Sets <code>useDocType</code> to true if <code>PARAM_USE_DOC_DEF</code> is |
| 125 | * present as a key in the setup parameters. |
| 126 | * @param setup map with setup parameters. |
| 127 | * @exception SlugInitializationException not thrown |
| 128 | */ |
| 129 | public void init(Map setup) throws SlugInitializationException |
| 130 | { |
| 131 | super.init(setup); |
| 132 | useDocType = false; |
| 133 | if (setup != null) |
| 134 | { |
| 135 | useDocType = (setup.containsKey(PARAM_USE_DOC_DEF)); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Generate class definition. |
| 141 | * |
| 142 | * @return implementation dependent String representation of provided parameter class. |
| 143 | */ |
| 144 | public String getClassDef() |
| 145 | { |
| 146 | return classDef; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Escape & < and > characters. |
| 151 | * @param str string to be escaped. |
| 152 | * @return original string with &, < and > substituted for &, < and > respectively. |
| 153 | */ |
| 154 | protected String xmlEscape(String str) |
| 155 | { |
| 156 | if ((str == null) || (str.trim().length() == 0)) |
| 157 | { |
| 158 | return ""; |
| 159 | } |
| 160 | StringBuffer result = new StringBuffer(); |
| 161 | for (int i = 0; i < str.length(); i++) |
| 162 | { |
| 163 | char ch = str.charAt(i); |
| 164 | switch (ch) |
| 165 | { |
| 166 | case '&': |
| 167 | result.append("&"); |
| 168 | break; |
| 169 | case '<': |
| 170 | result.append("<"); |
| 171 | break; |
| 172 | case '>': |
| 173 | result.append(">"); |
| 174 | break; |
| 175 | default: |
| 176 | result.append(ch); |
| 177 | } |
| 178 | } |
| 179 | return result.toString(); |
| 180 | } |
| 181 | |
| 182 | } |