KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > ant > taskdefs > DomlValidateTask


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: DomlValidateTask.java,v 1.1 2004/09/03 13:43:13 sinisa Exp $
22  */

23
24 /*
25  *
26  * @author Nenad Vico
27  * @since LBS1.8
28  * @version $Revision: 1.1 $
29  *
30  */

31 package org.enhydra.ant.taskdefs;
32
33 import java.io.File JavaDoc;
34 import java.io.FileNotFoundException JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.util.Properties JavaDoc;
37 import javax.xml.transform.OutputKeys JavaDoc;
38 import org.apache.tools.ant.BuildException;
39 import org.apache.tools.ant.Task;
40 import org.enhydra.dods.xml.XmlUtil;
41 import org.enhydra.dods.xml.XmlUtilException;
42
43 /**
44  * This class represent task for tarnsformation doml file for validation.
45  */

46 public class DomlValidateTask extends Task {
47     
48     private String JavaDoc doml;
49     private String JavaDoc projRoot;
50     private String JavaDoc template;
51     private String JavaDoc force;
52
53     /**
54      * constructor with index id and unique
55      */

56     public DomlValidateTask() {}
57
58     /**
59      * The setter for the "doml" attribute
60      *
61      */

62     public void setDoml(String JavaDoc doml) {
63         this.doml = doml;
64     }
65   
66     /**
67      * The setter for the "projRoot" attribute
68      *
69      */

70     public void setProjRoot(String JavaDoc projRoot) {
71         this.projRoot = projRoot;
72     }
73     
74     /**
75      * The setter for the "template" attribute
76      *
77      */

78     public void setTemplate(String JavaDoc temp) {
79         this.template = temp;
80     }
81
82     /**
83      * The setter for the "force" attribute
84      *
85      */

86     public void setForce(String JavaDoc force) {
87         this.force = force;
88     }
89
90     public void execute() throws BuildException {
91         try {
92             if (doml == null) {
93                 doml = getDomlFile();
94             }
95             if (projRoot == null) {
96                 projRoot = getProjectRoot();
97             }
98             if (template == null) {
99                 template = "standard";
100             }
101             copyDoml();
102         } catch (Exception JavaDoc e) {
103             e.printStackTrace();
104         }
105     }
106     
107     public void copyDoml() throws FileNotFoundException JavaDoc, IOException JavaDoc {
108         File JavaDoc domlFile = new File JavaDoc(doml);
109
110         if (!domlFile.exists()) {
111             throw new FileNotFoundException JavaDoc(doml + " file not found");
112         }
113         File JavaDoc projFile = new File JavaDoc(projRoot);
114
115         if (!projFile.exists()) {
116             throw new FileNotFoundException JavaDoc(projRoot + " not found");
117         }
118         try {
119             if (!force.equals("true")) { // not forcing output
120
int dotIndex = doml.indexOf(".");
121                 String JavaDoc update = projRoot
122                         + doml.substring(doml.lastIndexOf(File.separator),
123                                 doml.lastIndexOf(".")) + ".uptodate";
124                 File JavaDoc updateFile = new File JavaDoc(update);
125
126                 if (domlFile.lastModified() < updateFile.lastModified()) {
127                     return;
128                 }
129             }
130         } catch (StringIndexOutOfBoundsException JavaDoc ex) {}
131         Properties JavaDoc prop = new Properties JavaDoc();
132
133         prop.setProperty(OutputKeys.DOCTYPE_SYSTEM,
134                 "file:///" + System.getProperty("DODS_HOME") + File.separator
135                 + "build" + File.separator + "dtd" + File.separator + "doml.dtd");
136         prop.setProperty(OutputKeys.VERSION, "1.0");
137         prop.setProperty(OutputKeys.ENCODING, "UTF-8");
138         prop.setProperty(OutputKeys.STANDALONE, "no");
139         try {
140             XmlUtil xmlUtil = new XmlUtil(doml);
141
142             xmlUtil.setSingleAttribute("/doml/database@templateset", null,
143                     template);
144             xmlUtil.store(projRoot + File.separator + domlFile.getName(), prop);
145         } catch (XmlUtilException e) {
146             e.printStackTrace();
147         }
148     }
149
150     /**
151      * get uses project root
152      * @return project root
153      */

154     public static String JavaDoc getProjectRoot() {
155         String JavaDoc projectRoot = null;
156
157         projectRoot = System.getProperty("PROJECT_ROOT");
158         if (projectRoot == null) {
159             projectRoot = ".";
160         }
161         return projectRoot;
162     }
163  
164     /**
165      * get uses doml file
166      * @return uses doml file
167      */

168     public static String JavaDoc getDomlFile() {
169         String JavaDoc strDoml = null;
170
171         strDoml = System.getProperty("DOML_FILE");
172         String JavaDoc domlFile = null;
173
174         if (strDoml == null) {
175             File JavaDoc current = new File JavaDoc(".");
176             String JavaDoc[] files = current.list();
177
178             out:
179             for (int i = 0; i < files.length; i++) {
180                 if (files[i].toLowerCase().endsWith(".doml")) {
181                     domlFile = files[i];
182                     break out;
183                 } else {
184                     domlFile = null;
185                 }
186             }
187             strDoml = domlFile;
188         }
189         return strDoml;
190     }
191
192     public static void main(String JavaDoc[] args) {
193         DomlValidateTask validate = new DomlValidateTask();
194
195         try {
196             validate.copyDoml();
197         } catch (Exception JavaDoc e) {
198             e.printStackTrace();
199         }
200     }
201 }
202
Popular Tags