| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 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 |
| 54 | | | |
| 55 | 0 | | public class TemplateDescr |
| 56 | | | { |
| 57 | 0 | | private String mDescription = null; |
| 58 | 0 | | private final Map mParameterMap = new HashMap(); |
| 59 | 0 | | private final List mParameterList = new ArrayList(); |
| 60 | 0 | | private final Map mFiles = new HashMap(); |
| 61 | | | |
| 62 | | (1) | public String getDescription () |
| 63 | | | { |
| 64 | 0 | | return mDescription; |
| 65 | | | } |
| 66 | | | |
| 67 | | (2) | public Map getParameterMap () |
| 68 | | | { |
| 69 | 0 | | return mParameterMap; |
| 70 | | | } |
| 71 | | | |
| 72 | | (3) | public List getParameterList () |
| 73 | | | { |
| 74 | 0 | | return mParameterList; |
| 75 | | | } |
| 76 | | | |
| 77 | | (4) | public Map getFilesMap () |
| 78 | | | { |
| 79 | 0 | | return mFiles; |
| 80 | | | } |
| 81 | | | |
| 82 | | (5)(6) | public void parseDescription (String xmlDescription) |
| 83 | | | throws IOException, ParserConfigurationException, |
| 84 | | | SAXException, FactoryConfigurationError |
| 85 | | | { |
| 86 | 0 | (7) | final DefaultHandler docHandler = new DefaultHandler() { |
| 87 | | | |
| 88 | 0 | | private final StringBuffer mCharBuffer = new StringBuffer(); |
| 89 | 0 | | 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 | 0 | | if ("parameter".equals(qName)) |
| 102 | | | { |
| 103 | 0 | | final String name = attributes.getValue("name"); |
| 104 | 0 | | final int minLength |
| 105 | | | = Integer.parseInt(attributes.getValue("minLength")); |
| 106 | 0 | | final int maxLength |
| 107 | | | = Integer.parseInt(attributes.getValue("maxLength")); |
| 108 | 0 | | final boolean multiLine |
| 109 | | | = Boolean.valueOf(attributes.getValue("multiLine")) |
| 110 | | | .booleanValue(); |
| 111 | 0 | | mParam = new Parameter(name, minLength, maxLength, multiLine); |
| 112 | 0 | | } |
| 113 | 0 | | else if ("description".equals(qName)) |
| 114 | | | { |
| 115 | 0 | | mCharBuffer.setLength(0); |
| 116 | | | } |
| 117 | 0 | | else if ("default".equals(qName) |
| 118 | | | || "regexp".equals(qName)) |
| 119 | | | { |
| 120 | 0 | | if (mParam == null) |
| 121 | | | { |
| 122 | 0 | (8) | throw new SAXException( |
| 123 | | | "'default' and 'regexp' tags may only occur " |
| 124 | | | + " inside a 'parameter' tag"); |
| 125 | | | } |
| 126 | 0 | | mCharBuffer.setLength(0); |
| 127 | | | } |
| 128 | 0 | | else if ("file".equals(qName)) |
| 129 | | | { |
| 130 | 0 | | final String source = attributes.getValue("source"); |
| 131 | 0 | | final String target = attributes.getValue("target"); |
| 132 | 0 | | mFiles.put(source, target); |
| 133 | | | } |
| 134 | | | } |
| 135 | 0 | | catch (SAXException e) |
| 136 | | | { |
| 137 | 0 | (9) | e.printStackTrace(); |
| 138 | 0 | | throw e; |
| 139 | | | } |
| 140 | 0 | | catch (RuntimeException e) |
| 141 | | | { |
| 142 | 0 | (10) | e.printStackTrace(); |
| 143 | 0 | | throw e; |
| 144 | 0 | | } |
| 145 | 0 | | } |
| 146 | | | |
| 147 | | | {@inheritDoc} |
| 148 | | | public void endElement (String uri, String localName, String qName) |
| 149 | | | throws SAXException |
| 150 | | | { |
| 151 | | | try |
| 152 | | | { |
| 153 | 0 | (11)(12) | if (qName.equals("parameter")) |
| 154 | | | { |
| 155 | 0 | | mParameterMap.put(mParam.getName(), mParam); |
| 156 | 0 | | mParameterList.add(mParam); |
| 157 | | | |
| 158 | | | |
| 159 | 0 | | mParam = null; |
| 160 | | | } |
| 161 | 0 | (13)(14) | else if (qName.equals("description")) |
| 162 | | | { |
| 163 | 0 | | if (mParam != null) |
| 164 | | | { |
| 165 | 0 | | mParam.setDescription(mCharBuffer.toString().trim()); |
| 166 | | | } |
| 167 | 0 | | else if (mDescription == null) |
| 168 | | | { |
| 169 | 0 | | mDescription = mCharBuffer.toString().trim(); |
| 170 | | | } |
| 171 | | | else |
| 172 | | | { |
| 173 | 0 | (15) | throw new SAXException( |
| 174 | | | "'description' element not expected here"); |
| 175 | | | } |
| 176 | 0 | | mCharBuffer.setLength(0); |
| 177 | | | } |
| 178 | 0 | (16)(17) | else if (qName.equals("default")) |
| 179 | | | { |
| 180 | 0 | | mParam.setDefaultValue(mCharBuffer.toString().trim()); |
| 181 | 0 | | mCharBuffer.setLength(0); |
| 182 | | | } |
| 183 | 0 | (18)(19) | else if (qName.equals("regexp")) |
| 184 | | | { |
| 185 | 0 | | mParam.setRegexp(mCharBuffer.toString().trim()); |
| 186 | 0 | | mCharBuffer.setLength(0); |
| 187 | | | } |
| 188 | | | } |
| 189 | 0 | | catch (SAXException e) |
| 190 | | | { |
| 191 | 0 | (20) | e.printStackTrace(); |
| 192 | 0 | | throw e; |
| 193 | | | } |
| 194 | 0 | | catch (RuntimeException e) |
| 195 | | | { |
| 196 | 0 | (21) | e.printStackTrace(); |
| 197 | 0 | | throw e; |
| 198 | 0 | | } |
| 199 | 0 | | } |
| 200 | | | |
| 201 | | | {@inheritDoc} |
| 202 | | | public void characters (char[] ch, int start, int length) |
| 203 | | | throws SAXException |
| 204 | | | { |
| 205 | 0 | | mCharBuffer.append(ch, start, length); |
| 206 | 0 | | } |
| 207 | | | }; |
| 208 | | | |
| 209 | 0 | | final SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); |
| 210 | 0 | | final StringReader sr = new StringReader(xmlDescription); |
| 211 | 0 | | parser.parse(new InputSource(sr), docHandler); |
| 212 | 0 | | } |
| 213 | | | |
| 214 | | | {@inheritDoc} |
| 215 | | | public String toString () |
| 216 | | | { |
| 217 | 0 | (22) | final StringBuffer sbuf = new StringBuffer(); |
| 218 | 0 | (23) | sbuf.append("[ParameterDescription"); |
| 219 | 0 | | sbuf.append("\n description=").append(mDescription); |
| 220 | 0 | | sbuf.append("\n files=").append(mFiles.toString()); |
| 221 | 0 | | sbuf.append("\n parameters="); |
| 222 | 0 | | sbuf.append(mParameterMap.toString()); |
| 223 | 0 | | sbuf.append(']'); |
| 224 | 0 | | return sbuf.toString(); |
| 225 | | | } |
| 226 | | | } |