| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.phoenix.templategen; |
|---|
| 34 | |
|---|
| 35 | import java.io.IOException; |
|---|
| 36 | import java.io.StringReader; |
|---|
| 37 | import java.util.ArrayList; |
|---|
| 38 | import java.util.HashMap; |
|---|
| 39 | import java.util.List; |
|---|
| 40 | import java.util.Map; |
|---|
| 41 | |
|---|
| 42 | import javax.xml.parsers.FactoryConfigurationError; |
|---|
| 43 | import javax.xml.parsers.ParserConfigurationException; |
|---|
| 44 | import javax.xml.parsers.SAXParser; |
|---|
| 45 | import javax.xml.parsers.SAXParserFactory; |
|---|
| 46 | |
|---|
| 47 | import org.xml.sax.Attributes; |
|---|
| 48 | import org.xml.sax.InputSource; |
|---|
| 49 | import org.xml.sax.SAXException; |
|---|
| 50 | import org.xml.sax.helpers.DefaultHandler; |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * @author Albrecht Messner |
|---|
| 54 | */ |
|---|
| 55 | public class TemplateDescr |
|---|
| 56 | { |
|---|
| 57 | private String mDescription = null; |
|---|
| 58 | private final Map mParameterMap = new HashMap(); |
|---|
| 59 | private final List mParameterList = new ArrayList(); |
|---|
| 60 | private final Map mFiles = new HashMap(); |
|---|
| 61 | |
|---|
| 62 | public String getDescription () |
|---|
| 63 | { |
|---|
| 64 | return mDescription; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | public Map getParameterMap () |
|---|
| 68 | { |
|---|
| 69 | return mParameterMap; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | public List getParameterList () |
|---|
| 73 | { |
|---|
| 74 | return mParameterList; |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | public Map getFilesMap () |
|---|
| 78 | { |
|---|
| 79 | return mFiles; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | public void parseDescription (String xmlDescription) |
|---|
| 83 | throws IOException, ParserConfigurationException, |
|---|
| 84 | SAXException, FactoryConfigurationError |
|---|
| 85 | { |
|---|
| 86 | final DefaultHandler docHandler = new DefaultHandler() { |
|---|
| 87 | |
|---|
| 88 | private final StringBuffer mCharBuffer = new StringBuffer(); |
|---|
| 89 | private Parameter mParam = null; |
|---|
| 90 | |
|---|
| 91 | /** {@inheritDoc} */ |
|---|
| 92 | public void startElement ( |
|---|
| 93 | String uri, |
|---|
| 94 | String localName, |
|---|
| 95 | String qName, |
|---|
| 96 | Attributes attributes) |
|---|
| 97 | throws SAXException |
|---|
| 98 | { |
|---|
| 99 | try |
|---|
| 100 | { |
|---|
| 101 | if ("parameter".equals(qName)) |
|---|
| 102 | { |
|---|
| 103 | final String name = attributes.getValue("name"); |
|---|
| 104 | final int minLength |
|---|
| 105 | = Integer.parseInt(attributes.getValue("minLength")); |
|---|
| 106 | final int maxLength |
|---|
| 107 | = Integer.parseInt(attributes.getValue("maxLength")); |
|---|
| 108 | final boolean multiLine |
|---|
| 109 | = Boolean.valueOf(attributes.getValue("multiLine")) |
|---|
| 110 | .booleanValue(); |
|---|
| 111 | mParam = new Parameter(name, minLength, maxLength, multiLine); |
|---|
| 112 | } |
|---|
| 113 | else if ("description".equals(qName)) |
|---|
| 114 | { |
|---|
| 115 | mCharBuffer.setLength(0); |
|---|
| 116 | } |
|---|
| 117 | else if ("default".equals(qName) |
|---|
| 118 | || "regexp".equals(qName)) |
|---|
| 119 | { |
|---|
| 120 | if (mParam == null) |
|---|
| 121 | { |
|---|
| 122 | throw new SAXException( |
|---|
| 123 | "'default' and 'regexp' tags may only occur " |
|---|
| 124 | + " inside a 'parameter' tag"); |
|---|
| 125 | } |
|---|
| 126 | mCharBuffer.setLength(0); |
|---|
| 127 | } |
|---|
| 128 | else if ("file".equals(qName)) |
|---|
| 129 | { |
|---|
| 130 | final String source = attributes.getValue("source"); |
|---|
| 131 | final String target = attributes.getValue("target"); |
|---|
| 132 | mFiles.put(source, target); |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | catch (SAXException e) |
|---|
| 136 | { |
|---|
| 137 | e.printStackTrace(); |
|---|
| 138 | throw e; |
|---|
| 139 | } |
|---|
| 140 | catch (RuntimeException e) |
|---|
| 141 | { |
|---|
| 142 | e.printStackTrace(); |
|---|
| 143 | throw e; |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | /** {@inheritDoc} */ |
|---|
| 148 | public void endElement (String uri, String localName, String qName) |
|---|
| 149 | throws SAXException |
|---|
| 150 | { |
|---|
| 151 | try |
|---|
| 152 | { |
|---|
| 153 | if (qName.equals("parameter")) |
|---|
| 154 | { |
|---|
| 155 | mParameterMap.put(mParam.getName(), mParam); |
|---|
| 156 | mParameterList.add(mParam); |
|---|
| 157 | |
|---|
| 158 | // System.out.println(param); |
|---|
| 159 | mParam = null; |
|---|
| 160 | } |
|---|
| 161 | else if (qName.equals("description")) |
|---|
| 162 | { |
|---|
| 163 | if (mParam != null) |
|---|
| 164 | { |
|---|
| 165 | mParam.setDescription(mCharBuffer.toString().trim()); |
|---|
| 166 | } |
|---|
| 167 | else if (mDescription == null) |
|---|
| 168 | { |
|---|
| 169 | mDescription = mCharBuffer.toString().trim(); |
|---|
| 170 | } |
|---|
| 171 | else |
|---|
| 172 | { |
|---|
| 173 | throw new SAXException( |
|---|
| 174 | "'description' element not expected here"); |
|---|
| 175 | } |
|---|
| 176 | mCharBuffer.setLength(0); |
|---|
| 177 | } |
|---|
| 178 | else if (qName.equals("default")) |
|---|
| 179 | { |
|---|
| 180 | mParam.setDefaultValue(mCharBuffer.toString().trim()); |
|---|
| 181 | mCharBuffer.setLength(0); |
|---|
| 182 | } |
|---|
| 183 | else if (qName.equals("regexp")) |
|---|
| 184 | { |
|---|
| 185 | mParam.setRegexp(mCharBuffer.toString().trim()); |
|---|
| 186 | mCharBuffer.setLength(0); |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | catch (SAXException e) |
|---|
| 190 | { |
|---|
| 191 | e.printStackTrace(); |
|---|
| 192 | throw e; |
|---|
| 193 | } |
|---|
| 194 | catch (RuntimeException e) |
|---|
| 195 | { |
|---|
| 196 | e.printStackTrace(); |
|---|
| 197 | throw e; |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | /** {@inheritDoc} */ |
|---|
| 202 | public void characters (char[] ch, int start, int length) |
|---|
| 203 | throws SAXException |
|---|
| 204 | { |
|---|
| 205 | mCharBuffer.append(ch, start, length); |
|---|
| 206 | } |
|---|
| 207 | }; |
|---|
| 208 | |
|---|
| 209 | final SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); |
|---|
| 210 | final StringReader sr = new StringReader(xmlDescription); |
|---|
| 211 | parser.parse(new InputSource(sr), docHandler); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | /** {@inheritDoc} */ |
|---|
| 215 | public String toString () |
|---|
| 216 | { |
|---|
| 217 | final StringBuffer sbuf = new StringBuffer(); |
|---|
| 218 | sbuf.append("[ParameterDescription"); |
|---|
| 219 | sbuf.append("\n description=").append(mDescription); |
|---|
| 220 | sbuf.append("\n files=").append(mFiles.toString()); |
|---|
| 221 | sbuf.append("\n parameters="); |
|---|
| 222 | sbuf.append(mParameterMap.toString()); |
|---|
| 223 | sbuf.append(']'); |
|---|
| 224 | return sbuf.toString(); |
|---|
| 225 | } |
|---|
| 226 | } |
|---|