KickJava   Java API By Example, From Geeks To Geeks.

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


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: RenameLabelTask.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.SiteTree;
23 import org.apache.lenya.cms.publication.DocumentException;
24 import org.apache.lenya.cms.publication.Label;
25 import org.apache.lenya.cms.publication.SiteTreeException;
26 import org.apache.lenya.cms.publication.SiteTreeNode;
27 import org.apache.tools.ant.BuildException;
28
29 /**
30  * Ant task to rename a label in an existing node in a tree.
31  */

32 public class RenameLabelTask extends PublicationTask {
33     private String JavaDoc documentid;
34     private String JavaDoc labelName;
35     private String JavaDoc area;
36     private String JavaDoc language;
37
38     /**
39      * Creates a new instance of InsertLabelTask
40      */

41     public RenameLabelTask() {
42         super();
43     }
44
45     /**
46      * Get the area of the site tree.
47      *
48      * @return the area of the tree.
49      */

50     protected String JavaDoc getArea() {
51         return area;
52     }
53
54     /**
55      * Set the area.
56      *
57      * @param area the area.
58      */

59     public void setArea(String JavaDoc area) {
60         this.area = area;
61     }
62
63     /**
64      * Set the value of the area of the tree.
65      *
66      * @param area the area of the tree.
67      */

68     public void setAbsolutetreepath(String JavaDoc area) {
69         this.area = area;
70     }
71
72     /**
73      * Return the document-id corresponding to the node to delete.
74      *
75      * @return string The document-id.
76      */

77     protected String JavaDoc getDocumentid() {
78         return documentid;
79     }
80
81     /**
82      * Set the value of the document-id corresponding to the node to delete.
83      *
84      * @param string The document-id.
85      */

86     public void setDocumentid(String JavaDoc string) {
87         documentid = string;
88     }
89
90     /**
91      * Get the name of the label.
92      *
93      * @return the labelName
94      */

95     public String JavaDoc getLabelName() {
96         return labelName;
97     }
98
99     /**
100      * Set the labelName.
101      *
102      * @param labelName the name of the label
103      */

104     public void setLabelName(String JavaDoc labelName) {
105         this.labelName = labelName;
106     }
107
108     /**
109      * Get the language.
110      *
111      * @return the language
112      */

113     public String JavaDoc getLanguage() {
114         return language;
115     }
116
117     /**
118      * Set the language.
119      *
120      * @param language the language
121      */

122     public void setLanguage(String JavaDoc language) {
123         this.language = language;
124     }
125
126     /**
127      * Rename a label in an existing node in the tree.
128      *
129      * @param documentid the document-id of the document.
130      * @param labelName the new name of the label.
131      * @param language the language of the label that is to be renamed.
132      * @param area determines in which sitetree the label is to be renamed
133      *
134      * @throws SiteTreeException if an error occurs.
135      */

136     public void renameLabel(
137         String JavaDoc documentid,
138         String JavaDoc labelName,
139         String JavaDoc language,
140         String JavaDoc area)
141         throws SiteTreeException, DocumentException {
142
143         SiteTree tree = getPublication().getTree(area);
144         SiteTreeNode node = tree.getNode(documentid);
145         if (node == null) {
146             throw new DocumentException(
147                 "Document-id " + documentid + " not found.");
148         }
149         Label label = node.getLabel(language);
150         if (label == null) {
151             throw new DocumentException(
152                 "Label for language " + language + " not found.");
153         }
154     // FIXME: This is somewhat of a hack. The change of the label
155
// name should not be done by removing the label and readding
156
// it. Instead the node should probably have a setLabel method
157
// which could be invoked by the Label.setLabel() method.
158
tree.removeLabel(documentid, label);
159         label.setLabel(labelName);
160         tree.addLabel(documentid, label);
161         tree.save();
162     }
163
164     /** (non-Javadoc)
165      * @see org.apache.tools.ant.Task#execute()
166      */

167     public void execute() throws BuildException {
168         try {
169             log("document-id corresponding to the node: " + getDocumentid());
170             log("label name: " + getLabelName());
171             log("language: " + getLanguage());
172             log("area: " + getArea());
173             renameLabel(
174                 getDocumentid(),
175                 getLabelName(),
176                 getLanguage(),
177                 getArea());
178         } catch (Exception JavaDoc e) {
179             throw new BuildException(e);
180         }
181     }
182 }
183
Popular Tags