You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2003/12/11 17:55:41 UTC
cvs commit: cocoon-lenya/src/java/org/apache/lenya/cms/publication CollectionImpl.java DefaultDocument.java DocumentBuilder.java DefaultDocumentBuilder.java
andreas 2003/12/11 08:55:41
Modified: src/java/org/apache/lenya/cms/publication
CollectionImpl.java DefaultDocument.java
DocumentBuilder.java DefaultDocumentBuilder.java
Log:
- removed buildCollection() from DocumentBuilder
- added lazy loading to CollectionImpl
Revision Changes Path
1.5 +61 -24 cocoon-lenya/src/java/org/apache/lenya/cms/publication/CollectionImpl.java
Index: CollectionImpl.java
===================================================================
RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/publication/CollectionImpl.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- CollectionImpl.java 10 Dec 2003 09:41:22 -0000 1.4
+++ CollectionImpl.java 11 Dec 2003 16:55:41 -0000 1.5
@@ -64,6 +64,7 @@
import org.apache.lenya.xml.DocumentHelper;
import org.apache.lenya.xml.NamespaceHelper;
+import org.apache.log4j.Category;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
@@ -73,6 +74,8 @@
* @author <a href="mailto:andreas@apache.org">Andreas Hartmann</a>
*/
public class CollectionImpl extends DefaultDocument implements Collection {
+
+ private static final Category log = Category.getInstance(CollectionImpl.class);
/**
* Ctor.
@@ -84,7 +87,6 @@
protected CollectionImpl(Publication publication, String id, String area)
throws DocumentException {
super(publication, id, area);
- load();
}
/**
@@ -98,23 +100,32 @@
protected CollectionImpl(Publication publication, String id, String area, String language)
throws DocumentException {
super(publication, id, area, language);
- load();
}
- private List documents = new ArrayList();
+ private List documentsList = new ArrayList();
+
+ /**
+ * Returns the list that holds the documents. Use this method to invoke lazy loading.
+ * @return A list.
+ * @throws DocumentException when something went wrong.
+ */
+ protected List documents() throws DocumentException {
+ load();
+ return documentsList;
+ }
/**
* @see org.apache.lenya.cms.publication.Collection#getDocuments()
*/
public Document[] getDocuments() throws DocumentException {
- return (Document[]) documents.toArray(new Document[documents.size()]);
+ return (Document[]) documents().toArray(new Document[documents().size()]);
}
/**
* @see org.apache.lenya.cms.publication.Collection#add(org.apache.lenya.cms.publication.Document)
*/
public void add(Document document) throws DocumentException {
- documents.add(document);
+ documents().add(document);
save();
}
@@ -122,7 +133,7 @@
* @see org.apache.lenya.cms.publication.Collection#add(int, org.apache.lenya.cms.publication.Document)
*/
public void add(int position, Document document) throws DocumentException {
- documents.add(position, document);
+ documents().add(position, document);
save();
}
@@ -130,11 +141,11 @@
* @see org.apache.lenya.cms.publication.Collection#remove(org.apache.lenya.cms.publication.Document)
*/
public void remove(Document document) throws DocumentException {
- if (!documents.contains(document)) {
+ if (!documents().contains(document)) {
throw new DocumentException(
"Collection [" + this +"] does not contain document [" + document + "]");
}
- documents.remove(document);
+ documents().remove(document);
save();
}
@@ -146,6 +157,7 @@
*/
protected void load() throws DocumentException {
if (!isLoaded) {
+ log.debug("Loading: ", new DocumentException());
NamespaceHelper helper;
try {
helper = getNamespaceHelper();
@@ -154,17 +166,10 @@
Element[] documentElements =
helper.getChildren(collectionElement, ELEMENT_DOCUMENT);
- DocumentBuilder builder = getPublication().getDocumentBuilder();
for (int i = 0; i < documentElements.length; i++) {
- String documentId = documentElements[i].getAttribute(ATTRIBUTE_ID);
- String url =
- builder.buildCanonicalUrl(
- getPublication(),
- getArea(),
- documentId,
- getLanguage());
- Document document = builder.buildDocument(getPublication(), url);
- documents.add(document);
+ Element documentElement = documentElements[i];
+ Document document = loadDocument(documentElement);
+ documentsList.add(document);
}
} catch (DocumentException e) {
throw e;
@@ -176,6 +181,25 @@
}
/**
+ * Loads a document from an XML element.
+ * @param documentElement The XML element.
+ * @return A document.
+ * @throws DocumentBuildException when something went wrong.
+ */
+ protected Document loadDocument(Element documentElement) throws DocumentBuildException {
+ DocumentBuilder builder = getPublication().getDocumentBuilder();
+ String documentId = documentElement.getAttribute(ATTRIBUTE_ID);
+ String url =
+ builder.buildCanonicalUrl(
+ getPublication(),
+ getArea(),
+ documentId,
+ getLanguage());
+ Document document = builder.buildDocument(getPublication(), url);
+ return document;
+ }
+
+ /**
* Saves the XML source of this collection.
* @throws DocumentException when something went wrong.
*/
@@ -191,8 +215,7 @@
Document[] documents = getDocuments();
for (int i = 0; i < documents.length; i++) {
- Element documentElement = helper.createElement(ELEMENT_DOCUMENT);
- documentElement.setAttribute(ATTRIBUTE_ID, documents[i].getId());
+ Element documentElement = createDocumentElement(documents[i], helper);
collectionElement.appendChild(documentElement);
}
@@ -206,6 +229,19 @@
}
/**
+ * Creates an element to store a document.
+ * @param helper The namespace helper of the document.
+ * @return An XML element.
+ * @throws DocumentException when something went wrong.
+ */
+ protected Element createDocumentElement(Document document, NamespaceHelper helper)
+ throws DocumentException {
+ Element documentElement = helper.createElement(ELEMENT_DOCUMENT);
+ documentElement.setAttribute(ATTRIBUTE_ID, document.getId());
+ return documentElement;
+ }
+
+ /**
* Returns the namespace helper for the XML source.
* @return A namespace helper.
* @throws DocumentException when something went wrong.
@@ -236,25 +272,26 @@
* @see org.apache.lenya.cms.publication.Collection#contains(org.apache.lenya.cms.publication.Document)
*/
public boolean contains(Document document) throws DocumentException {
- return documents.contains(document);
+ return documents().contains(document);
}
/**
* @see org.apache.lenya.cms.publication.Collection#clear()
*/
public void clear() throws DocumentException {
- documents.clear();
+ documents().clear();
}
/**
* @see org.apache.lenya.cms.publication.Collection#getFirstPosition(org.apache.lenya.cms.publication.Document)
*/
public int getFirstPosition(Document document) throws DocumentException {
+ load();
if (!contains(document)) {
throw new DocumentException(
"The collection [" + this +"] does not contain the document [" + document + "]");
}
- return documents.indexOf(document);
+ return documents().indexOf(document);
}
}
1.37 +5 -1 cocoon-lenya/src/java/org/apache/lenya/cms/publication/DefaultDocument.java
Index: DefaultDocument.java
===================================================================
RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/publication/DefaultDocument.java,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -r1.36 -r1.37
--- DefaultDocument.java 4 Dec 2003 18:07:14 -0000 1.36
+++ DefaultDocument.java 11 Dec 2003 16:55:41 -0000 1.37
@@ -65,6 +65,7 @@
* @author <a href="mailto:andreas@apache.org">Andreas Hartmann</a>
*/
public class DefaultDocument implements Document {
+
private String id;
private Publication publication;
private DublinCore dublincore;
@@ -87,6 +88,8 @@
/**
* Creates a new instance of DefaultDocument.
+ * The language of the document is the default language of
+ * the publication.
* @param publication The publication the document belongs to.
* @param id The document ID (starting with a slash).
* @param area The area.
@@ -100,6 +103,7 @@
this.publication = publication;
setArea(area);
+ setLanguage(publication.getDefaultLanguage());
this.dublincore = new DublinCoreProxy(this);
1.10 +1 -12 cocoon-lenya/src/java/org/apache/lenya/cms/publication/DocumentBuilder.java
Index: DocumentBuilder.java
===================================================================
RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/publication/DocumentBuilder.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- DocumentBuilder.java 4 Dec 2003 14:17:21 -0000 1.9
+++ DocumentBuilder.java 11 Dec 2003 16:55:41 -0000 1.10
@@ -76,17 +76,6 @@
throws DocumentBuildException;
/**
- * Builds a collection document.
- *
- * @param publication The publication the document belongs to.
- * @param url The URL of the form /{publication-id}/{area}/{document-id}{language-suffix}.{extension}.
- * @return A collection.
- * @throws DocumentBuildException when something went wrong.
- */
- Collection buildCollection(Publication publication, String url)
- throws DocumentBuildException;
-
- /**
* Checks if an URL corresponds to a CMS document.
*
* @param publication The publication the document belongs to.
1.27 +18 -19 cocoon-lenya/src/java/org/apache/lenya/cms/publication/DefaultDocumentBuilder.java
Index: DefaultDocumentBuilder.java
===================================================================
RCS file: /home/cvs/cocoon-lenya/src/java/org/apache/lenya/cms/publication/DefaultDocumentBuilder.java,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -r1.26 -r1.27
--- DefaultDocumentBuilder.java 4 Dec 2003 14:17:21 -0000 1.26
+++ DefaultDocumentBuilder.java 11 Dec 2003 16:55:41 -0000 1.27
@@ -98,7 +98,7 @@
}
DefaultDocument document =
- new DefaultDocument(publication, documentId, info.getArea(), language);
+ createDocument(publication, info.getArea(), documentId, language);
document.setExtension(extension);
document.setDocumentURL(originalURL);
@@ -106,25 +106,24 @@
}
/**
- * @see org.apache.lenya.cms.publication.DocumentBuilder#buildCollection(org.apache.lenya.cms.publication.Publication, java.lang.String)
+ * Creates a new document object.
+ * Override this method to create specific document objects,
+ * e.g., for different document IDs.
+ * @param publication The publication.
+ * @param area The area.
+ * @param documentId The document ID.
+ * @param language The language.
+ * @return A document.
+ * @throws DocumentException when something went wrong.
*/
- public Collection buildCollection(Publication publication, String url)
+ protected DefaultDocument createDocument(
+ Publication publication,
+ String area,
+ String documentId,
+ String language)
throws DocumentBuildException {
- Document document = buildDocument(publication, url);
- CollectionImpl collection;
- try {
- collection =
- new CollectionImpl(
- publication,
- document.getId(),
- document.getArea(),
- document.getLanguage());
- } catch (DocumentException e) {
- throw new DocumentBuildException(e);
- }
- collection.setExtension(document.getExtension());
- collection.setDocumentURL(document.getDocumentURL());
- return collection;
+ DefaultDocument document = new DefaultDocument(publication, documentId, area, language);
+ return document;
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: lenya-cvs-unsubscribe@cocoon.apache.org
For additional commands, e-mail: lenya-cvs-help@cocoon.apache.org