KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > publication > CollectionImpl


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: CollectionImpl.java 43109 2004-07-10 22:59:55Z andreas $ */
19
20 package org.apache.lenya.cms.publication;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.xml.parsers.ParserConfigurationException JavaDoc;
28
29 import org.apache.lenya.xml.DocumentHelper;
30 import org.apache.lenya.xml.NamespaceHelper;
31 import org.apache.log4j.Category;
32 import org.apache.xpath.XPathAPI;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36 import org.xml.sax.SAXException JavaDoc;
37
38 /**
39  * Implementation of a Collection.
40  */

41 public class CollectionImpl extends DefaultDocument implements Collection {
42     
43     private static final Category log = Category.getInstance(CollectionImpl.class);
44
45     /**
46      * Ctor.
47      * @param publication A publication.
48      * @param id The document ID.
49      * @param area The area the document belongs to.
50      * @throws DocumentException when something went wrong.
51      */

52     public CollectionImpl(Publication publication, String JavaDoc id, String JavaDoc area) throws DocumentException {
53         super(publication, id, area);
54     }
55
56     /**
57      * Ctor.
58      * @param publication A publication.
59      * @param id The document ID.
60      * @param area The area the document belongs to.
61      * @param language The language of the document.
62      * @throws DocumentException when something went wrong.
63      */

64     public CollectionImpl(Publication publication, String JavaDoc id, String JavaDoc area, String JavaDoc language) throws DocumentException {
65         super(publication, id, area, language);
66     }
67
68     private List JavaDoc documentsList = new ArrayList JavaDoc();
69     
70     /**
71      * Returns the list that holds the documents. Use this method to invoke lazy loading.
72      * @return A list.
73      * @throws DocumentException when something went wrong.
74      */

75     protected List JavaDoc documents() throws DocumentException {
76         load();
77         return documentsList;
78     }
79
80     /**
81      * @see org.apache.lenya.cms.publication.Collection#getDocuments()
82      */

83     public Document[] getDocuments() throws DocumentException {
84         return (Document[]) documents().toArray(new Document[documents().size()]);
85     }
86
87     /**
88      * @see org.apache.lenya.cms.publication.Collection#add(org.apache.lenya.cms.publication.Document)
89      */

90     public void add(Document document) throws DocumentException {
91         documents().add(document);
92         save();
93     }
94
95     /**
96      * @see org.apache.lenya.cms.publication.Collection#add(int, org.apache.lenya.cms.publication.Document)
97      */

98     public void add(int position, Document document) throws DocumentException {
99         documents().add(position, document);
100         save();
101     }
102
103     /**
104      * @see org.apache.lenya.cms.publication.Collection#remove(org.apache.lenya.cms.publication.Document)
105      */

106     public void remove(Document document) throws DocumentException {
107         if (!documents().contains(document)) {
108             throw new DocumentException(
109                 "Collection [" + this +"] does not contain document [" + document + "]");
110         }
111         documents().remove(document);
112         save();
113     }
114
115     private boolean isLoaded = false;
116
117     /**
118      * Loads the collection from its XML source.
119      * @throws DocumentException when something went wrong.
120      */

121     protected void load() throws DocumentException {
122         if (!isLoaded) {
123             log.debug("Loading: ", new DocumentException());
124             NamespaceHelper helper;
125             try {
126                 helper = getNamespaceHelper();
127
128                 Element JavaDoc collectionElement = helper.getDocument().getDocumentElement();
129                 Element JavaDoc[] documentElements =
130                     helper.getChildren(collectionElement, ELEMENT_DOCUMENT);
131
132                 for (int i = 0; i < documentElements.length; i++) {
133                     Element JavaDoc documentElement = documentElements[i];
134                     Document document = loadDocument(documentElement);
135                     documentsList.add(document);
136                 }
137             } catch (DocumentException e) {
138                 throw e;
139             } catch (Exception JavaDoc e) {
140                 throw new DocumentException(e);
141             }
142             isLoaded = true;
143         }
144     }
145
146     /**
147      * Loads a document from an XML element.
148      * @param documentElement The XML element.
149      * @return A document.
150      * @throws DocumentBuildException when something went wrong.
151      */

152     protected Document loadDocument(Element JavaDoc documentElement) throws DocumentBuildException {
153         DocumentBuilder builder = getPublication().getDocumentBuilder();
154         String JavaDoc documentId = documentElement.getAttribute(ATTRIBUTE_ID);
155         String JavaDoc url =
156             builder.buildCanonicalUrl(
157                 getPublication(),
158                 getArea(),
159                 documentId,
160                 getLanguage());
161         Document document = builder.buildDocument(getPublication(), url);
162         return document;
163     }
164
165     /**
166      * Saves the XML source of this collection.
167      * @throws DocumentException when something went wrong.
168      */

169     public void save() throws DocumentException {
170         try {
171             
172             NamespaceHelper helper = getNamespaceHelper();
173             Element JavaDoc collectionElement = helper.getDocument().getDocumentElement();
174             if (collectionElement.getAttributeNS(null, ATTRIBUTE_ID) == null | collectionElement.getAttribute(ATTRIBUTE_ID).equals("")) {
175                 collectionElement.setAttributeNS(null, ATTRIBUTE_ID, this.getId());
176             }
177             Element JavaDoc[] existingDocumentElements = helper.getChildren(collectionElement, ELEMENT_DOCUMENT);
178             for (int i = 0; i < existingDocumentElements.length; i++) {
179                 collectionElement.removeChild(existingDocumentElements[i]);
180             }
181             
182             collectionElement.normalize();
183             
184             NodeList JavaDoc emptyTextNodes = XPathAPI.selectNodeList(collectionElement, "text()");
185             for (int i = 0; i < emptyTextNodes.getLength(); i++) {
186                 Node JavaDoc node = emptyTextNodes.item(i);
187                 node = collectionElement.removeChild(node);
188             }
189
190             Document[] documents = getDocuments();
191             for (int i = 0; i < documents.length; i++) {
192                 Element JavaDoc documentElement = createDocumentElement(documents[i], helper);
193                 collectionElement.appendChild(documentElement);
194             }
195             DocumentHelper.writeDocument(helper.getDocument(), getFile());
196             
197         } catch (DocumentException e) {
198             throw e;
199         } catch (Exception JavaDoc e) {
200             throw new DocumentException(e);
201         }
202     }
203
204     /**
205      * Creates an element to store a document.
206      * @param helper The namespace helper of the document.
207      * @param document The document.
208      * @return An XML element.
209      * @throws DocumentException when something went wrong.
210      */

211     protected Element JavaDoc createDocumentElement(Document document, NamespaceHelper helper)
212         throws DocumentException {
213         Element JavaDoc documentElement = helper.createElement(ELEMENT_DOCUMENT);
214         documentElement.setAttributeNS(null, ATTRIBUTE_ID, document.getId());
215         return documentElement;
216     }
217
218     /**
219      * Returns the namespace helper for the XML source.
220      * @return A namespace helper.
221      * @throws DocumentException when something went wrong.
222      * @throws ParserConfigurationException when something went wrong.
223      * @throws SAXException when something went wrong.
224      * @throws IOException when something went wrong.
225      */

226     protected NamespaceHelper getNamespaceHelper()
227         throws DocumentException, ParserConfigurationException JavaDoc, SAXException JavaDoc, IOException JavaDoc {
228
229         NamespaceHelper helper;
230
231         if (exists()) {
232             File JavaDoc file = getFile();
233             org.w3c.dom.Document JavaDoc document = DocumentHelper.readDocument(file);
234             helper = new NamespaceHelper(Collection.NAMESPACE, Collection.DEFAULT_PREFIX, document);
235         } else {
236             helper =
237                 new NamespaceHelper(
238                     Collection.NAMESPACE,
239                     Collection.DEFAULT_PREFIX,
240                     ELEMENT_COLLECTION);
241         }
242         return helper;
243     }
244
245     /**
246      * @see org.apache.lenya.cms.publication.Collection#contains(org.apache.lenya.cms.publication.Document)
247      */

248     public boolean contains(Document document) throws DocumentException {
249         return documents().contains(document);
250     }
251
252     /**
253      * @see org.apache.lenya.cms.publication.Collection#clear()
254      */

255     public void clear() throws DocumentException {
256         documents().clear();
257     }
258
259     /**
260      * @see org.apache.lenya.cms.publication.Collection#getFirstPosition(org.apache.lenya.cms.publication.Document)
261      */

262     public int getFirstPosition(Document document) throws DocumentException {
263         load();
264         if (!contains(document)) {
265             throw new DocumentException(
266                 "The collection [" + this +"] does not contain the document [" + document + "]");
267         }
268         return documents().indexOf(document);
269     }
270
271     /**
272      * @see org.apache.lenya.cms.publication.Collection#size()
273      */

274     public int size() throws DocumentException {
275         return documents().size();
276     }
277
278 }
279
Popular Tags