|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| BaseGrammarReaderController.java | 12.5% | 16.7% | 28.6% | 17.9% |
|
||||||||||||||
| 1 | // | |
| 2 | //Copyright (c) 2003, 2004, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. | |
| 3 | //All rights reserved. | |
| 4 | // | |
| 5 | //Redistribution and use in source and binary forms, with or without modification, | |
| 6 | //are permitted provided that the following conditions are met: | |
| 7 | // | |
| 8 | //* Redistributions of source code must retain the above copyright notice, | |
| 9 | //this list of conditions and the following disclaimer. | |
| 10 | //* Redistributions in binary form must reproduce the above copyright notice, | |
| 11 | //this list of conditions and the following disclaimer in the documentation | |
| 12 | //and/or other materials provided with the distribution. | |
| 13 | //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. | |
| 14 | //nor the names of its contributors may be used to endorse or promote products | |
| 15 | //derived from this software without specific prior written permission. | |
| 16 | // | |
| 17 | //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 18 | //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 19 | //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
| 20 | //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 21 | //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 22 | //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | |
| 23 | //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 24 | //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | //POSSIBILITY OF SUCH DAMAGE. | |
| 27 | // | |
| 28 | package org.objectledge.xml; | |
| 29 | ||
| 30 | import org.xml.sax.EntityResolver; | |
| 31 | import org.xml.sax.InputSource; | |
| 32 | import org.xml.sax.Locator; | |
| 33 | import org.xml.sax.SAXException; | |
| 34 | ||
| 35 | import com.sun.msv.reader.GrammarReaderController; | |
| 36 | ||
| 37 | /** | |
| 38 | * GrammarReaderController that logs all warnings and throws exceptions on errors. | |
| 39 | * | |
| 40 | * @author <a href="mailto:dgajda@caltha.pl">Damian Gajda</a> | |
| 41 | * @version $Id: BaseGrammarReaderController.java,v 1.3 2005/02/21 16:27:51 zwierzem Exp $ | |
| 42 | */ | |
| 43 | public abstract class BaseGrammarReaderController implements GrammarReaderController | |
| 44 | { | |
| 45 | /** Loaded grammar URI. */ | |
| 46 | private String grammarUri; | |
| 47 | ||
| 48 | /** Entity resolution is delegated to this object, it can be <code>null</null>. */ | |
| 49 | private EntityResolver entityResolver; | |
| 50 | ||
| 51 | /** Used to compose information of location of errors and warnings. */ | |
| 52 | private StringBuilder locationMessage = new StringBuilder(64); | |
| 53 | ||
| 54 | /** | |
| 55 | * Creates a grammar reader controller. | |
| 56 | * | |
| 57 | * @param grammarUri URI of a loaded grammar - provided for better warning description | |
| 58 | * @param entityResolver optional entity resolver. | |
| 59 | */ | |
| 60 | 5934 | public BaseGrammarReaderController( |
| 61 | String grammarUri, | |
| 62 | EntityResolver entityResolver) | |
| 63 | { | |
| 64 | 5934 | this.grammarUri = grammarUri; |
| 65 | 5934 | this.entityResolver = entityResolver; |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * Creates a grammar reader controller without entity resolver. | |
| 70 | * | |
| 71 | * @param grammarUri URI of a loaded grammar - provided for better warning description | |
| 72 | */ | |
| 73 | 0 | public BaseGrammarReaderController(String grammarUri) |
| 74 | { | |
| 75 | 0 | this(grammarUri, null); |
| 76 | } | |
| 77 | ||
| 78 | //------------------------------------------------------------------------ | |
| 79 | // EntityResolver methods | |
| 80 | ||
| 81 | /** | |
| 82 | * Resolves an entity, uses externaly set EntityResolver or returns <code>null</code>. | |
| 83 | * | |
| 84 | * @param publicId public id of an entity | |
| 85 | * @param systemId system id of an entity | |
| 86 | * @return resolved entity's input source | |
| 87 | * @throws java.io.IOException on IO errors while resolving entity | |
| 88 | * @throws SAXException on parsing errors while resolving entity | |
| 89 | */ | |
| 90 | 184 | public InputSource resolveEntity( String publicId, String systemId ) |
| 91 | throws java.io.IOException, SAXException | |
| 92 | { | |
| 93 | 184 | if(entityResolver!=null) |
| 94 | { | |
| 95 | 0 | return entityResolver.resolveEntity(publicId, systemId); |
| 96 | } | |
| 97 | else | |
| 98 | { | |
| 99 | 184 | return null; |
| 100 | } | |
| 101 | } | |
| 102 | ||
| 103 | //------------------------------------------------------------------------ | |
| 104 | // GrammarReaderController methods | |
| 105 | ||
| 106 | /** | |
| 107 | * Receives warning notification - does nothing. | |
| 108 | * | |
| 109 | * @param loc warning location info | |
| 110 | * @param errorMessage warning message | |
| 111 | */ | |
| 112 | 0 | public void warning(Locator[] loc, String errorMessage) |
| 113 | { | |
| 114 | // pass to the log? | |
| 115 | } | |
| 116 | ||
| 117 | /** | |
| 118 | * Receives error notification - does nothing. | |
| 119 | * | |
| 120 | * @param loc error location info | |
| 121 | * @param errorMessage error message | |
| 122 | * @param nestedException exception nested in error information | |
| 123 | */ | |
| 124 | 0 | public void error( Locator[] loc, String errorMessage, Exception nestedException ) |
| 125 | { | |
| 126 | // TODO pass the log? | |
| 127 | } | |
| 128 | ||
| 129 | //------------------------------------------------------------------------ | |
| 130 | // Utility methods | |
| 131 | ||
| 132 | /** | |
| 133 | * Prepares a location message. | |
| 134 | * | |
| 135 | * @param type message type - 'error' or 'warning' | |
| 136 | * @param loc locator of the error or warning | |
| 137 | * @param errorMessage a message | |
| 138 | * @return formatted location message | |
| 139 | */ | |
| 140 | 0 | protected String getLocationMessage(String type, Locator[] loc, String errorMessage) |
| 141 | { | |
| 142 | // init buffer | |
| 143 | 0 | locationMessage.setLength(0); |
| 144 | ||
| 145 | ||
| 146 | 0 | locationMessage.append(type); |
| 147 | 0 | locationMessage.append(" on schema '").append(grammarUri).append("' ; "); |
| 148 | ||
| 149 | 0 | locationMessage.append(errorMessage); |
| 150 | 0 | locationMessage.append(" ; "); |
| 151 | ||
| 152 | 0 | if(loc == null || loc.length == 0) |
| 153 | { | |
| 154 | 0 | locationMessage.append("location unknown"); |
| 155 | } | |
| 156 | else | |
| 157 | { | |
| 158 | 0 | for( int i=0; i<loc.length; i++ ) |
| 159 | { | |
| 160 | 0 | getLocation(loc[i]); |
| 161 | } | |
| 162 | } | |
| 163 | ||
| 164 | 0 | return locationMessage.toString(); |
| 165 | } | |
| 166 | ||
| 167 | 0 | private void getLocation(Locator loc) |
| 168 | { | |
| 169 | 0 | if(loc.getColumnNumber()>=0) |
| 170 | { | |
| 171 | 0 | locationMessage.append("column:"); |
| 172 | 0 | locationMessage.append(loc.getColumnNumber()); |
| 173 | } | |
| 174 | ||
| 175 | 0 | locationMessage.append(" line:"); |
| 176 | 0 | locationMessage.append(loc.getLineNumber()); |
| 177 | 0 | locationMessage.append(" "); |
| 178 | 0 | locationMessage.append(loc.getSystemId()); |
| 179 | 0 | locationMessage.append(" "); |
| 180 | } | |
| 181 | } |
|
||||||||||