| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
package tsukuba_bunko.resource; |
| 20 |
|
|
| 21 |
|
import java.io.IOException; |
| 22 |
|
|
| 23 |
|
import java.net.URL; |
| 24 |
|
|
| 25 |
|
import java.util.Iterator; |
| 26 |
|
import java.util.List; |
| 27 |
|
|
| 28 |
|
import javax.xml.parsers.ParserConfigurationException; |
| 29 |
|
import javax.xml.parsers.SAXParserFactory; |
| 30 |
|
|
| 31 |
|
import org.xml.sax.Attributes; |
| 32 |
|
import org.xml.sax.ContentHandler; |
| 33 |
|
import org.xml.sax.Locator; |
| 34 |
|
import org.xml.sax.SAXException; |
| 35 |
|
import org.xml.sax.XMLReader; |
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
public class ResourceLoader implements ContentHandler { |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
65 |
private XMLReader _parser = null; |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
65 |
private DeserializerMapping _mapping = DeserializerMapping.newInstance(); |
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
65 |
private ResourceDeserializer _currentDeserializer = null; |
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
65 |
private int _level = 0; |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
|
| 69 |
65 |
private List _stack = null; |
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
65 |
private StringBuffer _text = null; |
| 75 |
|
|
| 76 |
|
|
| 77 |
|
|
| 78 |
|
|
| 79 |
|
|
| 80 |
65 |
private Resources _resources = null; |
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
|
| 87 |
|
|
| 88 |
|
public ResourceLoader() |
| 89 |
|
throws ParserConfigurationException, SAXException |
| 90 |
|
{ |
| 91 |
65 |
super(); |
| 92 |
|
|
| 93 |
65 |
SAXParserFactory factory = SAXParserFactory.newInstance(); |
| 94 |
65 |
factory.setNamespaceAware( true ); |
| 95 |
65 |
_parser = factory.newSAXParser().getXMLReader(); |
| 96 |
65 |
_parser.setContentHandler( this ); |
| 97 |
65 |
} |
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
public void setDeserializerMapping( DeserializerMapping mapping ) |
| 105 |
|
{ |
| 106 |
0 |
_mapping = mapping; |
| 107 |
0 |
} |
| 108 |
|
|
| 109 |
|
|
| 110 |
|
|
| 111 |
|
|
| 112 |
|
|
| 113 |
|
public DeserializerMapping getDeserializerMapping() |
| 114 |
|
{ |
| 115 |
0 |
return _mapping; |
| 116 |
|
} |
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
public void loadResource( URL resourceURL, Resources resources ) |
| 124 |
|
throws IOException, IllegalResourceException |
| 125 |
|
{ |
| 126 |
|
try { |
| 127 |
65 |
_resources = resources; |
| 128 |
65 |
_parser.parse( resourceURL.toString() ); |
| 129 |
40 |
_resources = null; |
| 130 |
|
} |
| 131 |
25 |
catch( SAXException se ) { |
| 132 |
25 |
throw new IllegalResourceException( se ); |
| 133 |
40 |
} |
| 134 |
40 |
} |
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
protected ResourceDeserializer getResourceDeserializer( String namespaceURI, String localName, String qName, Attributes attrs ) |
| 145 |
|
throws SAXException |
| 146 |
|
{ |
| 147 |
475 |
if( _mapping == null ) { |
| 148 |
0 |
return null; |
| 149 |
|
} |
| 150 |
|
|
| 151 |
475 |
String dataType = attrs.getValue( "type" ); |
| 152 |
475 |
if( dataType != null ) { |
| 153 |
120 |
ResourceDeserializer deserializer = _mapping.getResourceDeserializer( dataType ); |
| 154 |
120 |
if( deserializer == null ) { |
| 155 |
0 |
throw new SAXException( "no deserializer for \"" + dataType + "\"" ); |
| 156 |
|
} |
| 157 |
120 |
return deserializer; |
| 158 |
|
} |
| 159 |
|
else { |
| 160 |
355 |
return null; |
| 161 |
|
} |
| 162 |
|
} |
| 163 |
|
|
| 164 |
|
|
| 165 |
|
|
| 166 |
|
|
| 167 |
|
|
| 168 |
|
protected String getCurrentPath() |
| 169 |
|
{ |
| 170 |
140 |
StringBuffer buffer = new StringBuffer(); |
| 171 |
140 |
Iterator itr = _stack.iterator(); |
| 172 |
140 |
if( itr.hasNext() ) { |
| 173 |
140 |
itr.next(); |
| 174 |
|
} |
| 175 |
|
|
| 176 |
140 |
if( itr.hasNext() ) { |
| 177 |
140 |
buffer.append( itr.next() ); |
| 178 |
|
} |
| 179 |
595 |
while( itr.hasNext() ) { |
| 180 |
455 |
buffer.append( "." ); |
| 181 |
455 |
buffer.append( itr.next() ); |
| 182 |
455 |
} |
| 183 |
140 |
return new String(buffer); |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
|
| 187 |
|
|
| 188 |
|
|
| 189 |
|
|
| 190 |
|
public void setDocumentLocator( Locator locator ) |
| 191 |
|
{ |
| 192 |
65 |
} |
| 193 |
|
|
| 194 |
|
public void startPrefixMapping( String namespaceURI, String prefix ) |
| 195 |
|
throws SAXException |
| 196 |
|
{ |
| 197 |
65 |
if( _currentDeserializer != null ) { |
| 198 |
0 |
_currentDeserializer.startPrefixMapping( namespaceURI, prefix ); |
| 199 |
|
} |
| 200 |
65 |
} |
| 201 |
|
|
| 202 |
|
public void endPrefixMapping( String namespaceURI ) |
| 203 |
|
throws SAXException |
| 204 |
|
{ |
| 205 |
40 |
if( _currentDeserializer != null ) { |
| 206 |
0 |
_currentDeserializer.endPrefixMapping( namespaceURI ); |
| 207 |
|
} |
| 208 |
40 |
} |
| 209 |
|
|
| 210 |
|
public void startDocument() |
| 211 |
|
{ |
| 212 |
65 |
_stack = new java.util.ArrayList(); |
| 213 |
65 |
if( _mapping == null ) { |
| 214 |
0 |
_mapping = DeserializerMapping.newInstance(); |
| 215 |
|
} |
| 216 |
65 |
} |
| 217 |
|
|
| 218 |
|
public void endDocument() |
| 219 |
|
{ |
| 220 |
40 |
_stack = null; |
| 221 |
40 |
} |
| 222 |
|
|
| 223 |
|
public void startElement( String namespaceURI, String localName, String qName, Attributes attrs ) |
| 224 |
|
throws SAXException |
| 225 |
|
{ |
| 226 |
565 |
if( _currentDeserializer != null ) { |
| 227 |
90 |
_level++; |
| 228 |
90 |
_currentDeserializer.startElement( namespaceURI, localName, qName, attrs ); |
| 229 |
70 |
} |
| 230 |
|
else { |
| 231 |
475 |
_stack.add( qName ); |
| 232 |
|
|
| 233 |
475 |
ResourceDeserializer deserializer = getResourceDeserializer( namespaceURI, localName, qName, attrs ); |
| 234 |
475 |
if( deserializer != null ) { |
| 235 |
120 |
_currentDeserializer = deserializer; |
| 236 |
120 |
deserializer.setDeserializerMapping( _mapping ); |
| 237 |
120 |
_level++; |
| 238 |
120 |
deserializer.startDocument(); |
| 239 |
120 |
deserializer.startElement( namespaceURI, localName, qName, attrs ); |
| 240 |
120 |
} |
| 241 |
|
else { |
| 242 |
355 |
_text = new StringBuffer(); |
| 243 |
|
} |
| 244 |
|
} |
| 245 |
545 |
} |
| 246 |
|
|
| 247 |
|
public void endElement( String namespaceURI, String localName, String qName ) |
| 248 |
|
throws SAXException |
| 249 |
|
{ |
| 250 |
410 |
if( _currentDeserializer != null ) { |
| 251 |
170 |
_level--; |
| 252 |
170 |
_currentDeserializer.endElement( namespaceURI, localName, qName ); |
| 253 |
165 |
if( _level == 0 ) { |
| 254 |
95 |
_currentDeserializer.endDocument(); |
| 255 |
95 |
_resources.setResource( getCurrentPath(), _currentDeserializer.getValue() ); |
| 256 |
95 |
_stack.remove( _stack.size() - 1 ); |
| 257 |
95 |
_currentDeserializer = null; |
| 258 |
95 |
_currentDeserializer = null; |
| 259 |
95 |
} |
| 260 |
|
} |
| 261 |
240 |
else if( _text != null ) { |
| 262 |
45 |
_resources.setResource( getCurrentPath(), new String(_text) ); |
| 263 |
45 |
_stack.remove( _stack.size() - 1 ); |
| 264 |
45 |
} |
| 265 |
|
else { |
| 266 |
195 |
_stack.remove( _stack.size() - 1 ); |
| 267 |
|
} |
| 268 |
405 |
_text = null; |
| 269 |
405 |
} |
| 270 |
|
|
| 271 |
|
public void processingInstruction( String target, String data ) |
| 272 |
|
throws SAXException |
| 273 |
|
{ |
| 274 |
0 |
if( _currentDeserializer != null ) { |
| 275 |
0 |
_currentDeserializer.processingInstruction( target, data ); |
| 276 |
|
} |
| 277 |
0 |
} |
| 278 |
|
|
| 279 |
|
public void skippedEntity( String name ) |
| 280 |
|
throws SAXException |
| 281 |
|
{ |
| 282 |
0 |
if( _currentDeserializer != null ) { |
| 283 |
0 |
_currentDeserializer.skippedEntity( name ); |
| 284 |
|
} |
| 285 |
0 |
} |
| 286 |
|
|
| 287 |
|
public void characters( char[] ch, int begin, class="keyword">int length ) |
| 288 |
|
throws SAXException |
| 289 |
|
{ |
| 290 |
910 |
if( _currentDeserializer != null ) { |
| 291 |
260 |
_currentDeserializer.characters( ch, begin, length ); |
| 292 |
260 |
} |
| 293 |
650 |
else if( _text != null ) { |
| 294 |
355 |
_text.append( ch, begin, length ); |
| 295 |
|
} |
| 296 |
910 |
} |
| 297 |
|
|
| 298 |
|
public void ignorableWhitespace( char[] ch, int begin, class="keyword">int length ) |
| 299 |
|
throws SAXException |
| 300 |
|
{ |
| 301 |
0 |
if( _currentDeserializer != null ) { |
| 302 |
0 |
_currentDeserializer.ignorableWhitespace( ch, begin, length ); |
| 303 |
|
} |
| 304 |
0 |
} |
| 305 |
|
} |