KickJava   Java API By Example, From Geeks To Geeks.

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


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: SetIdentifier.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.Document;
23 import org.apache.lenya.cms.publication.DocumentBuildException;
24 import org.apache.lenya.cms.publication.DocumentBuilder;
25 import org.apache.lenya.cms.publication.DocumentException;
26 import org.apache.lenya.cms.publication.DublinCore;
27 import org.apache.lenya.cms.publication.Label;
28 import org.apache.lenya.cms.publication.Publication;
29 import org.apache.lenya.cms.publication.SiteTree;
30 import org.apache.lenya.cms.publication.SiteTreeException;
31 import org.apache.lenya.cms.publication.SiteTreeNode;
32 import org.apache.tools.ant.BuildException;
33
34 /**
35  * anttask to set the document-id in the dc:identifier of all existing files corresponding to this
36  * document-id
37  */

38 public class SetIdentifier extends PublicationTask {
39
40     private String JavaDoc area;
41     private String JavaDoc documentid;
42
43     /**
44      *
45      */

46     public SetIdentifier() {
47         super();
48     }
49
50     /**
51      * @return String The area.
52      */

53     public String JavaDoc getArea() {
54         return area;
55     }
56
57     /**
58      * @return String The document-id.
59      */

60     public String JavaDoc getDocumentid() {
61         return documentid;
62     }
63
64     /**
65      * @param string
66      * The area.
67      */

68     public void setArea(String JavaDoc string) {
69         area = string;
70     }
71
72     /**
73      * @param string
74      * The document-id.
75      */

76     public void setDocumentid(String JavaDoc string) {
77         documentid = string;
78     }
79
80     /**
81      * write the document id in the DC Identifier of a document corresponding to this url
82      *
83      * @param publication
84      * The publication the document belongs to.
85      * @param url
86      * The URL of the form /{publication-id}/...
87      * @throws DocumentBuildException
88      * when something went wrong when building the cms document.
89      * @throws DocumentException
90      * when something went wrong when getting the DublinCore.
91      */

92     public void writeDCIdentifier(Publication publication, String JavaDoc url)
93         throws DocumentBuildException, DocumentException {
94         assert url != null;
95
96         Document document = null;
97         document = publication.getDocumentBuilder().buildDocument(publication, url);
98         DublinCore dublincore = document.getDublinCore();
99         dublincore.setValue("identifier", documentid);
100         dublincore.save();
101     }
102
103     /**
104      * @see org.apache.tools.ant.Task#execute()
105      */

106     public void execute() throws BuildException {
107         log("document-id " + this.getDocumentid());
108         log("area " + this.getArea());
109
110         Publication publication = getPublication();
111
112         String JavaDoc language = null;
113         String JavaDoc url = null;
114         SiteTree tree;
115
116         try {
117             tree = publication.getTree(area);
118         } catch (SiteTreeException e) {
119             throw new BuildException(e);
120         }
121         SiteTreeNode node = tree.getNode(documentid);
122         Label[] labels = node.getLabels();
123
124         DocumentBuilder builder = publication.getDocumentBuilder();
125
126         try {
127             if (labels.length < 1) {
128                 log("no languages found for the node with id : " + node.getId());
129                 url = builder.buildCanonicalUrl(publication, area, documentid);
130                 writeDCIdentifier(publication, url);
131             } else {
132                 for (int i = 0; i < labels.length; i++) {
133                     language = labels[i].getLanguage();
134                     url = builder.buildCanonicalUrl(publication, area, documentid, language);
135                     writeDCIdentifier(publication, url);
136                 }
137             }
138         } catch (DocumentException e1) {
139             throw new BuildException(e1);
140         } catch (DocumentBuildException e2) {
141             throw new BuildException(e2);
142         }
143     }
144
145 }
146
Popular Tags