View Javadoc
1 package org.apache.turbine.services.intake.transform; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.io.BufferedReader; 58 import java.io.FileReader; 59 import org.apache.turbine.services.intake.xmlmodel.AppData; 60 import org.apache.turbine.services.intake.xmlmodel.Rule; 61 import org.apache.turbine.services.intake.xmlmodel.XmlField; 62 import org.apache.turbine.services.intake.xmlmodel.XmlGroup; 63 import org.apache.xerces.parsers.SAXParser; 64 import org.xml.sax.Attributes; 65 import org.xml.sax.InputSource; 66 import org.xml.sax.SAXParseException; 67 import org.xml.sax.helpers.DefaultHandler; 68 69 /*** 70 * A Class that is used to parse an input 71 * xml schema file and creates and AppData java structure. 72 * It uses apache Xerces to do the xml parsing. 73 * 74 * @author <a href="mailto:jmcnally@collab.net>;John McNally</a> 75 * @version $Id: XmlToAppData.java,v 1.3 2002/07/11 16:53:27 mpoeschl Exp $ 76 */ 77 public class XmlToAppData extends DefaultHandler 78 { 79 private AppData app; 80 private XmlGroup currGroup; 81 private XmlField currField; 82 private Rule currRule; 83 private String currElement; 84 85 /*** 86 * Default custructor 87 */ 88 public XmlToAppData() 89 { 90 app = new AppData(); 91 } 92 93 94 /*** 95 * Parse and xml input file and returns a newly 96 * created and populated AppData structure 97 */ 98 public AppData parseFile(String xmlFile) 99 throws Exception 100 { 101 return parseFile(xmlFile, false); 102 } 103 104 /*** 105 * Parse and xml input file and returns a newly 106 * created and populated AppData structure 107 */ 108 public AppData parseFile(String xmlFile, boolean skipValidation) 109 throws Exception 110 { 111 SAXParser parser = new SAXParser(); 112 113 // set the Resolver for the database DTD 114 DTDResolver dtdResolver = new DTDResolver(); 115 parser.setEntityResolver(dtdResolver); 116 117 // We don't use an external content handler - we use this object 118 parser.setContentHandler(this); 119 parser.setErrorHandler(this); 120 121 // Validate the input file 122 parser.setFeature( 123 "http://apache.org/xml/features/validation/dynamic", true); 124 parser.setFeature("http://xml.org/sax/features/validation", true); 125 126 FileReader fr = new FileReader (xmlFile); 127 BufferedReader br = new BufferedReader (fr); 128 try 129 { 130 InputSource is = new InputSource (br); 131 parser.parse(is); 132 } 133 finally 134 { 135 br.close(); 136 } 137 138 return app; 139 } 140 141 142 143 /*** 144 * Handles opening elements of the xml file. 145 */ 146 public void startElement(String uri, String localName, 147 String rawName, Attributes attributes) 148 { 149 currElement = rawName; 150 if (rawName.equals("input-data")) 151 { 152 app.loadFromXML(attributes); 153 } 154 else if (rawName.equals("group")) 155 { 156 currGroup = app.addGroup(attributes); 157 } 158 else if (rawName.equals("field")) 159 { 160 currField = currGroup.addField(attributes); 161 } 162 else if (rawName.equals("rule")) 163 { 164 currRule = currField.addRule(attributes); 165 } 166 } 167 168 169 /*** 170 * Handles the character data, which we are using to specify the 171 * error message. 172 */ 173 public void characters(char[] mesgArray, int start, int length) 174 { 175 String cdata = new String(mesgArray, start, length).trim(); 176 if ( "rule".equals(currElement) && cdata.length() > 0) 177 { 178 currRule.setMessage(cdata); 179 } 180 if ( "required-message".equals(currElement) && cdata.length() > 0) 181 { 182 currField.setIfRequiredMessage(cdata); 183 } 184 } 185 186 /*** 187 * Callback function for the xml parser to give warnings. 188 * 189 * @param spe a <code>SAXParseException</code> value 190 */ 191 public void warning(SAXParseException spe) 192 { 193 System.out.println("Warning Line: " + spe.getLineNumber() + 194 " Row: " + spe.getColumnNumber() + 195 " Msg: " + spe.getMessage()); 196 } 197 198 /*** 199 * Callback function for the xml parser to give errors. 200 * 201 * @param spe a <code>SAXParseException</code> value 202 */ 203 public void error(SAXParseException spe) 204 { 205 System.out.println("Error Line: " + spe.getLineNumber() + 206 " Row: " + spe.getColumnNumber() + 207 " Msg: " + spe.getMessage()); 208 } 209 210 /*** 211 * Callback function for the xml parser to give fatalErrors. 212 * 213 * @param spe a <code>SAXParseException</code> value 214 */ 215 public void fatalError(SAXParseException spe) 216 { 217 System.out.println("Fatal Error Line: " + spe.getLineNumber() + 218 " Row: " + spe.getColumnNumber() + 219 " Msg: " + spe.getMessage()); 220 } 221 }

This page was automatically generated by Maven