1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| package org.apache.xindice.client.xmldb; |
21 |
| |
22 |
| import org.apache.xindice.client.xmldb.resources.XMLResourceImpl; |
23 |
| import org.apache.xindice.xml.NodeSource; |
24 |
| import org.apache.xindice.xml.SymbolTable; |
25 |
| import org.apache.xindice.xml.TextWriter; |
26 |
| import org.apache.xindice.xml.dom.DOMCompressor; |
27 |
| import org.apache.xindice.xml.dom.DocumentImpl; |
28 |
| |
29 |
| import org.w3c.dom.Document; |
30 |
| import org.w3c.dom.Element; |
31 |
| import org.w3c.dom.Node; |
32 |
| import org.w3c.dom.NodeList; |
33 |
| import org.xmldb.api.base.Resource; |
34 |
| import org.xmldb.api.base.ResourceIterator; |
35 |
| import org.xmldb.api.base.ResourceSet; |
36 |
| import org.xmldb.api.base.XMLDBException; |
37 |
| import org.xmldb.api.modules.XMLResource; |
38 |
| |
39 |
| import java.util.ArrayList; |
40 |
| import java.util.Collections; |
41 |
| import java.util.List; |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| public class ResourceSetImpl implements ResourceSet { |
50 |
| |
51 |
| public static final String RESOURCE_SET_NS = "http://www.xmldb.org/xapi/ResourceSet"; |
52 |
| |
53 |
| protected List resources; |
54 |
| protected org.xmldb.api.base.Collection collection; |
55 |
| private SymbolTable symbols; |
56 |
| private byte[] bytes; |
57 |
| |
58 |
| |
59 |
357
| public ResourceSetImpl(org.xmldb.api.base.Collection collection,
|
60 |
| Document doc) throws XMLDBException { |
61 |
357
| this.collection = collection;
|
62 |
| |
63 |
357
| if (doc != null) {
|
64 |
357
| initResources(doc);
|
65 |
| } else { |
66 |
0
| resources = Collections.synchronizedList(new ArrayList());
|
67 |
| } |
68 |
| } |
69 |
| |
70 |
0
| public ResourceSetImpl(org.xmldb.api.base.Collection collection,
|
71 |
| SymbolTable symbols, byte[] bytes) throws XMLDBException { |
72 |
0
| this.collection = collection;
|
73 |
0
| this.symbols = symbols;
|
74 |
0
| this.bytes = bytes;
|
75 |
| |
76 |
0
| initResources(new DocumentImpl(bytes, symbols, null));
|
77 |
| } |
78 |
| |
79 |
357
| protected void initResources(Document document) throws XMLDBException {
|
80 |
357
| NodeList nodes = document.getDocumentElement().getChildNodes();
|
81 |
357
| this.resources = Collections.synchronizedList(new ArrayList(nodes.getLength()));
|
82 |
| |
83 |
357
| int i = 0;
|
84 |
357
| while (i < nodes.getLength()) {
|
85 |
531
| Node n = nodes.item(i);
|
86 |
| |
87 |
531
| String documentId = null;
|
88 |
531
| if (n instanceof Element) {
|
89 |
531
| documentId = ((Element) n).getAttributeNS(NodeSource.SOURCE_NS, NodeSource.SOURCE_KEY);
|
90 |
| } |
91 |
| |
92 |
531
| XMLResource resource;
|
93 |
531
| if (bytes != null) {
|
94 |
0
| DocumentImpl doc = new DocumentImpl();
|
95 |
0
| doc.setSymbols(symbols);
|
96 |
0
| doc.importNode(n, true);
|
97 |
0
| doc.appendChild(n);
|
98 |
0
| byte[] b = DOMCompressor.compress(doc, symbols);
|
99 |
0
| resource = new XMLResourceImpl(null, documentId, collection, symbols, b);
|
100 |
| } else { |
101 |
531
| resource = new XMLResourceImpl(null, documentId, collection, TextWriter.toString(n));
|
102 |
| } |
103 |
| |
104 |
531
| i++;
|
105 |
531
| resources.add(resource);
|
106 |
| } |
107 |
| } |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
0
| public ResourceIterator getIterator() throws XMLDBException {
|
118 |
0
| return new ResourceIteratorImpl(resources);
|
119 |
| } |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
282
| public Resource getResource(long index) throws XMLDBException {
|
130 |
282
| return (XMLResource) resources.get((int) index);
|
131 |
| } |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
177
| public long getSize() throws XMLDBException {
|
140 |
177
| return resources.size();
|
141 |
| } |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
0
| public void addResource(Resource res) throws XMLDBException {
|
149 |
0
| resources.add(res);
|
150 |
| } |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
0
| public void clear() throws XMLDBException {
|
158 |
0
| resources.clear();
|
159 |
| } |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
0
| public void removeResource(long index) throws XMLDBException {
|
169 |
0
| resources.remove((int) index);
|
170 |
| } |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
0
| public Resource getMembersAsResource() throws XMLDBException {
|
181 |
| |
182 |
| |
183 |
| |
184 |
0
| Document doc = new DocumentImpl();
|
185 |
| |
186 |
0
| Element set = doc.createElementNS(RESOURCE_SET_NS, "xapi:resourceSet");
|
187 |
0
| set.setAttributeNS(RESOURCE_SET_NS, "xapi:collectionURI",
|
188 |
| "xmldb:xindice://" + ((XindiceCollection) collection).getCanonicalName()); |
189 |
0
| set.setAttribute("xmlns:xapi", RESOURCE_SET_NS);
|
190 |
0
| doc.appendChild(set);
|
191 |
| |
192 |
0
| int i = 0;
|
193 |
0
| while (i < resources.size()) {
|
194 |
0
| XMLResource res = (XMLResource) resources.get(i);
|
195 |
0
| Element resource = doc.createElementNS(RESOURCE_SET_NS,
|
196 |
| "xapi:resource"); |
197 |
0
| resource.setAttributeNS(RESOURCE_SET_NS, "xapi:documentID",
|
198 |
| res.getDocumentId()); |
199 |
| |
200 |
0
| resource.appendChild(doc.importNode(
|
201 |
| ((Document) res.getContentAsDOM()).getDocumentElement(), true)); |
202 |
| |
203 |
0
| set.appendChild(resource);
|
204 |
| |
205 |
0
| i++;
|
206 |
| } |
207 |
| |
208 |
0
| return new XMLResourceImpl(null, null, collection, TextWriter.toString(doc));
|
209 |
| } |
210 |
| } |