KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > ant > TreePublisher


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: TreePublisher.java 160149 2005-04-05 09:51:54Z michi $ */
19
20 package org.apache.lenya.cms.ant;
21
22 import org.apache.lenya.cms.publication.Label;
23 import org.apache.lenya.cms.publication.Publication;
24 import org.apache.lenya.cms.publication.SiteTree;
25 import org.apache.lenya.cms.publication.SiteTreeException;
26 import org.apache.lenya.cms.publication.SiteTreeNode;
27 import org.apache.lenya.cms.publishing.ParentNodeNotFoundException;
28 import org.apache.lenya.cms.publishing.PublishingException;
29 import org.apache.tools.ant.BuildException;
30
31 /**
32  * Ant task to publish the tree, adding a node for the new published document in the live tree
33  */

34 public class TreePublisher extends PublicationTask {
35     private String JavaDoc documentid;
36     private String JavaDoc language;
37
38     /**
39      * Creates a new instance of TreePublisher
40      */

41     public TreePublisher() {
42     }
43
44     /**
45      * Returns the document id
46      *
47      * @return DOCUMENT ME!
48      */

49     protected String JavaDoc getDocumentid() {
50         return documentid;
51     }
52
53     /**
54      * Sets the document id
55      *
56      * @param documentid DOCUMENT ME!
57      */

58     public void setDocumentid(String JavaDoc documentid) {
59         this.documentid = documentid;
60     }
61
62     /**
63      * Get the language of the document to be published
64      *
65      * @return a <code>String</code> containing the ISO string for this language, e.g. "de", "en"
66      */

67     public String JavaDoc getLanguage() {
68         return language;
69     }
70
71     /**
72      * Set the language of the document to be published
73      *
74      * @param string the ISO string for this language, e.g. "de", "en"
75      */

76     public void setLanguage(String JavaDoc string) {
77         language = string;
78     }
79
80     /**
81      * adds a node for the published document in the live tree
82      *
83      * @param documentId The id of the published document
84      * @param language the language for which this document is to be published. Can be null if all
85      * languages are to be published.
86      *
87      * @throws PublishingException if the publication failed.
88      */

89     public void publish(String JavaDoc documentId, String JavaDoc language) throws PublishingException {
90         SiteTree authoringTree = null;
91         SiteTree liveTree = null;
92
93         try {
94             authoringTree = getPublication().getTree(Publication.AUTHORING_AREA);
95             liveTree = getPublication().getTree(Publication.LIVE_AREA);
96
97             SiteTreeNode authoringNode = authoringTree.getNode(documentId);
98             SiteTreeNode[] siblings = authoringNode.getNextSiblings();
99             String JavaDoc parentId = authoringNode.getAbsoluteParentId();
100             SiteTreeNode sibling = null;
101             String JavaDoc siblingDocId = null;
102             for (int i = 0; i < siblings.length; i++) {
103                 String JavaDoc docId = parentId + "/" + siblings[i].getId();
104                 sibling = liveTree.getNode(docId);
105                 if (sibling != null) {
106                     siblingDocId = docId;
107                     break;
108                 }
109             }
110
111             if (language == null) {
112                 // no language was specified. Simply publish the
113
// node including all languages.
114
try {
115                     liveTree.addNode(authoringNode, siblingDocId);
116                 } catch (SiteTreeException e1) {
117                     throw new ParentNodeNotFoundException("Couldn't add document: " + documentId
118                             + " to live tree.", e1);
119                 }
120             } else {
121                 // a language was specified. Let's see if this
122
// node even has an entry for the specified
123
// language.
124
Label label = authoringNode.getLabel(language);
125                 if (label != null) {
126                     // check if this node has already been
127
// published
128
SiteTreeNode liveNode = liveTree.getNode(documentId);
129                     if (liveNode != null) {
130                         // if the node already exists in the live
131
// tree simply insert the label in the
132
// live tree
133
liveTree.setLabel(documentId, label);
134                     } else {
135                         // if the node doesn't exist, add it and
136
// add the specified label to it.
137
Label[] labels = { label };
138                         try {
139                             liveTree.addNode(documentId, labels, authoringNode.visibleInNav(),
140                                     authoringNode.getHref(),authoringNode.getSuffix(),
141                                     authoringNode.hasLink(),siblingDocId);
142                         } catch (SiteTreeException e1) {
143                             throw new ParentNodeNotFoundException("Couldn't add document: "
144                                     + documentId + " to live tree.", e1);
145                         }
146                     }
147                 } else {
148                     // the node that we're trying to publish
149
// doesn't have this language
150
throw new PublishingException("The node " + documentId
151                             + " doesn't contain a label for language " + language);
152                 }
153             }
154             liveTree.save();
155         } catch (PublishingException e) {
156             throw e;
157         } catch (Exception JavaDoc e) {
158             throw new PublishingException("Couldn't publish to live tree :", e);
159         }
160     }
161
162     /**
163      * Executes the task
164      *
165      * @throws BuildException DOCUMENT ME!
166      */

167     public void execute() throws BuildException {
168         try {
169             log("document id: " + getDocumentid());
170             log("language: " + getLanguage());
171
172             publish(getDocumentid(), getLanguage());
173         } catch (Exception JavaDoc e) {
174             throw new BuildException(e);
175         }
176     }
177 }
178
Popular Tags