KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > jbuilder > build > XMLCBuildTask


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  */

22 package org.enhydra.kelp.jbuilder.build;
23
24 // JBuilder imports
25
import com.borland.primetime.build.BuildTask;
26 import com.borland.primetime.build.BuildProcess;
27 import com.borland.primetime.node.Project;
28 import com.borland.primetime.node.TextFileNode;
29 import com.borland.jbuilder.node.JBProject;
30
31 // Kelp imports
32
import org.enhydra.kelp.common.event.WriteEvent;
33 import org.enhydra.kelp.common.event.WriteListener;
34 import org.enhydra.kelp.common.node.OtterXMLCNode;
35 import org.enhydra.kelp.common.node.OtterProject;
36 import org.enhydra.kelp.common.xmlc.XMLCNodeBuilder;
37 import org.enhydra.kelp.jbuilder.node.PrimeProject;
38
39 // Standard imports
40
import java.util.Vector JavaDoc;
41 import java.util.ResourceBundle JavaDoc;
42
43
44 /**
45  * BuildTask implementation for calling XMLC.
46  *
47  */

48 public class XMLCBuildTask implements BuildTask, WriteListener {
49
50
51     static ResourceBundle JavaDoc res =
52         ResourceBundle.getBundle("org.enhydra.kelp.jbuilder.Res"); // nores
53
public static final Object JavaDoc SHARED_KEY = new Object JavaDoc();
54     private BuildProcess buildProcess;
55     private Vector JavaDoc nodeVector = new Vector JavaDoc();
56     private Vector JavaDoc writeListeners = new Vector JavaDoc();
57     private XMLCNodeBuilder nodeBuilder;
58     private int messageCount = 0;
59     private int allNodeCount = 0;
60
61     /**
62      * Constructor declaration
63      *
64      */

65     public XMLCBuildTask() {}
66
67     public void increment() {
68         allNodeCount++;
69     }
70
71     /**
72      * Generate the DOM class using XMLC.
73      */

74     public boolean build(BuildProcess buildProcess, Project project,
75                          boolean rebuild) {
76
77
78         this.buildProcess = buildProcess;
79         OtterProject otterProject = new PrimeProject((JBProject) project);
80         int nodeCount = nodeVector.size();
81         int compileCount = nodeCount;
82         OtterXMLCNode current = null;
83
84
85         if ((allNodeCount < 3) || otterProject.isXMLCBuild()) {
86
87             // If build not selected, only continue for single node build
88
} else {
89             nodeVector.clear();
90             nodeCount = 0;
91             compileCount = 0;
92         }
93
94
95         // remove upToDate nodes from vector if not a rebuild.
96
if (!rebuild) {
97             for (int i = 0; i < nodeCount; i++) {
98                 current = (OtterXMLCNode) nodeVector.elementAt(i);
99                 if (upToDate(current)) {
100                     nodeVector.setElementAt(new Object JavaDoc(), i);
101                     compileCount--;
102                 }
103             }
104         }
105         OtterXMLCNode[] nodes = new OtterXMLCNode[compileCount];
106
107         compileCount = 0;
108         for (int i = 0; i < nodeCount; i++) {
109             if (nodeVector.elementAt(i) instanceof OtterXMLCNode) {
110                 if (compileCount < nodes.length) {
111                     nodes[compileCount] =
112                         (OtterXMLCNode) nodeVector.elementAt(i);
113                     compileCount++;
114                 }
115             }
116         }
117         WriteListener[] w = new WriteListener[1];
118         boolean recomp = false;
119
120         w[0] = this;
121         buildProcess.fireBuildStatus(res.getString("Generating_DOM"), true);
122         nodeBuilder = new XMLCNodeBuilder(this, true);
123         nodeBuilder.setProject(otterProject);
124         nodeBuilder.setNodes(nodes);
125         nodeBuilder.setEcho(false);
126         nodeBuilder.buildInCurrentThread();
127         for (int i = 0; i < nodes.length; i++) {
128             recomp = nodes[i].getMetaDataHandler().getRecompilation();
129             nodes[i].replaceGeneratedSource(recomp);
130         }
131         return true;
132     }
133
134     /**
135      * Build messages.
136      *
137      */

138     public void onWrite(WriteEvent e) {
139         if (buildProcess != null && messageCount < 200) {
140             buildProcess.fireBuildMessage(e.getString(), e.getString());
141             messageCount++;
142             if (messageCount == 200) {
143                 String JavaDoc message = res.getString("Reached_maximum_XMLC");
144
145                 buildProcess.fireBuildMessage(message, message);
146             }
147         }
148     }
149
150     /**
151      * Collect list of nodes to build.
152      *
153      */

154     public void addNode(OtterXMLCNode n) {
155         nodeVector.addElement(n);
156     }
157
158     /**
159      * Check if node up to date.
160      *
161      */

162     private boolean upToDate(OtterXMLCNode xmlcNode) {
163         boolean built = false;
164         TextFileNode docNode = null;
165
166         docNode = (TextFileNode) xmlcNode.getNativeNode();
167         if (docNode.isModified()) {
168             try {
169                 docNode.save();
170             } catch (Exception JavaDoc e) {
171                 e.printStackTrace();
172             }
173         } else if (!xmlcNode.getClassFile().exists()) {}
174         else if (xmlcNode.getClassFile().lastModified()
175                  > docNode.getUrl().getFileObject().lastModified()) {
176             built = true;
177         }
178         return built;
179     }
180
181 }
182
Popular Tags