1 package org.apache.turbine.services.intake.transform; 2 3 18 19 import java.io.BufferedReader ; 20 import java.io.FileReader ; 21 import java.io.IOException ; 22 23 import javax.xml.parsers.ParserConfigurationException ; 24 import javax.xml.parsers.SAXParser ; 25 import javax.xml.parsers.SAXParserFactory ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import org.apache.turbine.services.intake.xmlmodel.AppData; 31 import org.apache.turbine.services.intake.xmlmodel.Rule; 32 import org.apache.turbine.services.intake.xmlmodel.XmlField; 33 import org.apache.turbine.services.intake.xmlmodel.XmlGroup; 34 35 import org.xml.sax.Attributes ; 36 import org.xml.sax.InputSource ; 37 import org.xml.sax.SAXException ; 38 import org.xml.sax.SAXParseException ; 39 import org.xml.sax.helpers.DefaultHandler ; 40 41 51 public class XmlToAppData extends DefaultHandler 52 { 53 54 private static Log log = LogFactory.getLog(XmlToAppData.class); 55 56 private AppData app = null; 57 private XmlGroup currGroup = null; 58 private XmlField currField = null; 59 private Rule currRule = null; 60 61 private StringBuffer charBuffer = null; 62 63 private static SAXParserFactory saxFactory = null; 64 65 static 66 { 67 saxFactory = SAXParserFactory.newInstance(); 68 saxFactory.setValidating(true); 69 } 70 71 74 public XmlToAppData() 75 { 76 app = new AppData(); 77 } 78 79 89 public AppData parseFile(String xmlFile) 90 throws ParserConfigurationException , SAXException , IOException 91 { 92 SAXParser parser = saxFactory.newSAXParser(); 93 94 FileReader fr = new FileReader (xmlFile); 95 BufferedReader br = new BufferedReader (fr); 96 try 97 { 98 InputSource is = new InputSource (br); 99 parser.parse(is, this); 100 } 101 finally 102 { 103 br.close(); 104 } 105 106 return app; 107 } 108 109 116 public InputSource resolveEntity(String publicId, String systemId) 117 { 118 return new DTDResolver().resolveEntity(publicId, systemId); 119 } 120 121 128 public void startElement(String uri, String localName, 129 String rawName, Attributes attributes) 130 { 131 charBuffer = new StringBuffer (); 132 133 if (rawName.equals("input-data")) 134 { 135 app.loadFromXML(attributes); 136 } 137 else if (rawName.equals("group")) 138 { 139 currGroup = app.addGroup(attributes); 140 } 141 else if (rawName.equals("field")) 142 { 143 currField = currGroup.addField(attributes); 144 } 145 else if (rawName.equals("rule")) 146 { 147 currRule = currField.addRule(attributes); 148 } 149 } 150 151 157 public void characters(char[] mesgArray, int start, int length) 158 { 159 charBuffer.append(mesgArray, start, length); 160 } 161 162 168 public void endElement(String uri, String localName, 169 String rawName) 170 { 171 if (charBuffer.length() > 0) 172 { 173 String cdata = charBuffer.toString().trim(); 174 175 if ("rule".equals(rawName)) 176 { 177 currRule.setMessage(cdata); 178 } 179 else if ("required-message".equals(rawName)) 180 { 181 log.warn("The required-message element is deprecated! " + 182 "You should update your intake.xml file to use the " + 183 "'required' rule instead."); 184 currField.setIfRequiredMessage(cdata); 185 } 186 } 187 } 188 189 194 public void warning(SAXParseException spe) 195 { 196 StringBuffer sb = new StringBuffer (64); 197 sb.append("Parser Exception: Line "); 198 sb.append(spe.getLineNumber()); 199 sb.append(" Row "); 200 sb.append(spe.getColumnNumber()); 201 sb.append(" Msg: "); 202 sb.append(spe.getMessage()); 203 204 log.warn(sb.toString()); 205 } 206 207 212 public void error(SAXParseException spe) 213 { 214 StringBuffer sb = new StringBuffer (64); 215 sb.append("Parser Exception: Line "); 216 sb.append(spe.getLineNumber()); 217 sb.append(" Row "); 218 sb.append(spe.getColumnNumber()); 219 sb.append(" Msg: "); 220 sb.append(spe.getMessage()); 221 222 log.error(sb.toString()); 223 } 224 225 230 public void fatalError(SAXParseException spe) 231 { 232 StringBuffer sb = new StringBuffer (64); 233 sb.append("Parser Exception: Line "); 234 sb.append(spe.getLineNumber()); 235 sb.append(" Row "); 236 sb.append(spe.getColumnNumber()); 237 sb.append(" Msg: "); 238 sb.append(spe.getMessage()); 239 240 log.fatal(sb.toString()); 241 } 242 } 243 | Popular Tags |