1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
package tsukuba_bunko.resource; |
11 |
|
|
12 |
|
import java.util.List; |
13 |
|
|
14 |
|
import org.xml.sax.Attributes; |
15 |
|
import org.xml.sax.ContentHandler; |
16 |
|
import org.xml.sax.SAXException; |
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
public class ListDeserializer extends BasicDeserializer { |
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
160 |
private List _list = null; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
160 |
private ResourceDeserializer _itemDeserializer = null; |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
160 |
private ContentHandler _itemHandler = null; |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
160 |
private String _itemType = null; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
160 |
private int _level = 0; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
public ListDeserializer() |
55 |
|
{ |
56 |
160 |
super(); |
57 |
160 |
} |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
public void setTypeName( String typeName ) |
64 |
|
{ |
65 |
160 |
super.setTypeName( typeName ); |
66 |
160 |
int index = typeName.indexOf( '/' ); |
67 |
160 |
if( index == -1 ) { |
68 |
0 |
throw new IllegalArgumentException( "illegal list item type. \"" + typeName + "\"" ); |
69 |
|
} |
70 |
|
else { |
71 |
160 |
_itemType = typeName.substring( index + 1 ); |
72 |
|
} |
73 |
160 |
} |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
public void startDocument() |
80 |
|
{ |
81 |
30 |
_list = null; |
82 |
30 |
_level = 0; |
83 |
30 |
} |
84 |
|
|
85 |
|
public void endDocument() |
86 |
|
{ |
87 |
20 |
setValue( _list ); |
88 |
20 |
_list = null; |
89 |
20 |
} |
90 |
|
|
91 |
|
public void startPrefixMapping( String namespaceURI, String prefix ) |
92 |
|
throws SAXException |
93 |
|
{ |
94 |
0 |
if( _itemHandler != null ) { |
95 |
0 |
_itemHandler.startPrefixMapping( namespaceURI, prefix ); |
96 |
|
} |
97 |
0 |
} |
98 |
|
|
99 |
|
public void endPrefixMapping( String namespaceURI ) |
100 |
|
throws SAXException |
101 |
|
{ |
102 |
0 |
if( _itemHandler != null ) { |
103 |
0 |
_itemHandler.endPrefixMapping( namespaceURI ); |
104 |
|
} |
105 |
0 |
} |
106 |
|
|
107 |
|
public void startElement( String namespaceURI, String localName, String qName, Attributes attrs ) |
108 |
|
throws SAXException |
109 |
|
{ |
110 |
110 |
if( _list == null ) { |
111 |
30 |
_list = new java.util.ArrayList(); |
112 |
30 |
_itemDeserializer = _mapping.getResourceDeserializer( _itemType ); |
113 |
30 |
if( _itemDeserializer == null ) { |
114 |
0 |
throw new SAXException( "no deserializer for \"" + _itemType + "\"" ); |
115 |
|
} |
116 |
|
} |
117 |
80 |
else if( _itemHandler == null ) { |
118 |
75 |
if( localName.equals("item") ) { |
119 |
70 |
_level++; |
120 |
70 |
_itemHandler = _itemDeserializer; |
121 |
70 |
_itemHandler.startElement( namespaceURI, localName, qName, attrs ); |
122 |
70 |
} |
123 |
|
else { |
124 |
5 |
throw new SAXException( "illegal structure. : non-item element \"" + qName + "\"" ); |
125 |
|
} |
126 |
|
} |
127 |
|
else { |
128 |
5 |
_level++; |
129 |
5 |
_itemHandler.startElement( namespaceURI, localName, qName, attrs ); |
130 |
|
} |
131 |
100 |
} |
132 |
|
|
133 |
|
public void endElement( String namespaceURI, String localName, String qName ) |
134 |
|
throws SAXException |
135 |
|
{ |
136 |
|
try { |
137 |
90 |
if( _itemHandler != null ) { |
138 |
70 |
_level--; |
139 |
70 |
_itemHandler.endElement( namespaceURI, localName, qName ); |
140 |
65 |
if( _level == 0 ) { |
141 |
65 |
_list.add( _itemDeserializer.getValue() ); |
142 |
65 |
_itemHandler = null; |
143 |
|
} |
144 |
|
} |
145 |
5 |
}catch( Exception e ) { |
146 |
5 |
e.printStackTrace(); |
147 |
85 |
} |
148 |
90 |
} |
149 |
|
|
150 |
|
public void processingInstruction( String target, String data ) |
151 |
|
throws SAXException |
152 |
|
{ |
153 |
0 |
if( _itemHandler != null ) { |
154 |
0 |
_itemHandler.processingInstruction( target, data ); |
155 |
|
} |
156 |
0 |
} |
157 |
|
|
158 |
|
public void skippedEntity( String name ) |
159 |
|
throws SAXException |
160 |
|
{ |
161 |
0 |
if( _itemHandler != null ) { |
162 |
0 |
_itemHandler.skippedEntity( name ); |
163 |
|
} |
164 |
0 |
} |
165 |
|
|
166 |
|
public void characters( char[] ch, int begin, class="keyword">int length ) |
167 |
|
throws SAXException |
168 |
|
{ |
169 |
170 |
if( _itemHandler != null ) { |
170 |
75 |
_itemHandler.characters( ch, begin, length ); |
171 |
|
} |
172 |
170 |
} |
173 |
|
|
174 |
|
public void ignorableWhitespace( char[] ch, int begin, class="keyword">int length ) |
175 |
|
throws SAXException |
176 |
|
{ |
177 |
0 |
if( _itemHandler != null ) { |
178 |
0 |
_itemHandler.ignorableWhitespace( ch, begin, length ); |
179 |
|
} |
180 |
0 |
} |
181 |
|
} |