1 17 18 19 20 package org.apache.lenya.cms.publication; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.ArrayList ; 25 import java.util.List ; 26 27 import javax.xml.parsers.ParserConfigurationException ; 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 ; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.NodeList ; 36 import org.xml.sax.SAXException ; 37 38 41 public class CollectionImpl extends DefaultDocument implements Collection { 42 43 private static final Category log = Category.getInstance(CollectionImpl.class); 44 45 52 public CollectionImpl(Publication publication, String id, String area) throws DocumentException { 53 super(publication, id, area); 54 } 55 56 64 public CollectionImpl(Publication publication, String id, String area, String language) throws DocumentException { 65 super(publication, id, area, language); 66 } 67 68 private List documentsList = new ArrayList (); 69 70 75 protected List documents() throws DocumentException { 76 load(); 77 return documentsList; 78 } 79 80 83 public Document[] getDocuments() throws DocumentException { 84 return (Document[]) documents().toArray(new Document[documents().size()]); 85 } 86 87 90 public void add(Document document) throws DocumentException { 91 documents().add(document); 92 save(); 93 } 94 95 98 public void add(int position, Document document) throws DocumentException { 99 documents().add(position, document); 100 save(); 101 } 102 103 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 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 collectionElement = helper.getDocument().getDocumentElement(); 129 Element [] documentElements = 130 helper.getChildren(collectionElement, ELEMENT_DOCUMENT); 131 132 for (int i = 0; i < documentElements.length; i++) { 133 Element documentElement = documentElements[i]; 134 Document document = loadDocument(documentElement); 135 documentsList.add(document); 136 } 137 } catch (DocumentException e) { 138 throw e; 139 } catch (Exception e) { 140 throw new DocumentException(e); 141 } 142 isLoaded = true; 143 } 144 } 145 146 152 protected Document loadDocument(Element documentElement) throws DocumentBuildException { 153 DocumentBuilder builder = getPublication().getDocumentBuilder(); 154 String documentId = documentElement.getAttribute(ATTRIBUTE_ID); 155 String 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 169 public void save() throws DocumentException { 170 try { 171 172 NamespaceHelper helper = getNamespaceHelper(); 173 Element 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 [] 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 emptyTextNodes = XPathAPI.selectNodeList(collectionElement, "text()"); 185 for (int i = 0; i < emptyTextNodes.getLength(); i++) { 186 Node 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 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 e) { 200 throw new DocumentException(e); 201 } 202 } 203 204 211 protected Element createDocumentElement(Document document, NamespaceHelper helper) 212 throws DocumentException { 213 Element documentElement = helper.createElement(ELEMENT_DOCUMENT); 214 documentElement.setAttributeNS(null, ATTRIBUTE_ID, document.getId()); 215 return documentElement; 216 } 217 218 226 protected NamespaceHelper getNamespaceHelper() 227 throws DocumentException, ParserConfigurationException , SAXException , IOException { 228 229 NamespaceHelper helper; 230 231 if (exists()) { 232 File file = getFile(); 233 org.w3c.dom.Document 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 248 public boolean contains(Document document) throws DocumentException { 249 return documents().contains(document); 250 } 251 252 255 public void clear() throws DocumentException { 256 documents().clear(); 257 } 258 259 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 274 public int size() throws DocumentException { 275 return documents().size(); 276 } 277 278 } 279 | Popular Tags |