KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > ConvertImport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.nbbuild;
21
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.PrintStream JavaDoc;
26 import org.apache.tools.ant.*;
27
28 /**
29  * It replaces paths in import element of buildscript. It is for
30  * converting xtest builscript of modules to test distribution layout. <br>
31  *
32  * Parameters:
33  * <ul>
34  * <li> oldname - name of build script (for example 'cfg-qa-functiona.xml')
35  * <li> newpath - new path if file (for example '../templates/cfg-qa-functional.xml')
36  * <li> attribute prefix - property name (example fir dist 'dist.dir' it add ${dist.dir}/ prefix)
37  * <li> file - build script
38  * </ul>
39  */

40 public class ConvertImport extends Task {
41     private String JavaDoc oldName;
42     private String JavaDoc newPath;
43     private String JavaDoc propertyPrefixName;
44     private File JavaDoc file;
45     int endOfComment;
46     public void execute() throws BuildException {
47         if (!file.exists()) {
48             throw new BuildException("File " + file + " doesn't exist.");
49         }
50         byte bytes[] = new byte[(int)file.length()];
51         try {
52             FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
53             try {
54                 fis.read(bytes);
55             } finally {
56                 fis.close();
57             }
58             String JavaDoc xml = new String JavaDoc(bytes);
59             String JavaDoc oldXml = xml;
60             int end = 0;
61             // <import ... file=" "/>
62
for (int offset = 0 ; offset < xml.length() ; offset = end + 1) {
63                 
64                 int start = xml.indexOf("<import ",offset);
65                 if (start == -1) {
66                     break;
67                 }
68                 if (isComment(xml,offset,start)) {
69                     end = endOfComment;
70                     continue;
71                 }
72                 end = xml.indexOf("/>",start);
73                 if (end == -1) {
74                     continue;
75                 }
76                 int fileIndex = xml.indexOf("file",start);
77                 int q1 = xml.indexOf("\"",fileIndex);
78                 int q2 = xml.indexOf("\'",fileIndex);
79                 int qStart = (q1 != -1 && ( q2 > q1 || q2 == -1)) ? q1 : q2;
80                 if (qStart == -1 ) {
81                     throw new BuildException("Invalid xml " + file);
82                 }
83                 char qCh = (qStart == q1) ? '"' : '\'';
84                 int qEnd = xml.indexOf(qCh,qStart + 1);
85                 if (qEnd == -1 || qEnd > end) {
86                    throw new BuildException("Invalid xml : " + file);
87                 }
88                 
89                 int nameIdx = xml.indexOf(oldName,qCh);
90                 if (nameIdx != -1 && nameIdx < qEnd) {
91                     xml = replaceFileName(xml,qStart,qEnd);
92                     end = xml.indexOf("/>",start);
93                 }
94                 
95             } // while
96
if (oldXml != xml) {
97                 // changed file
98
PrintStream JavaDoc ps = new PrintStream JavaDoc(file);
99                 try {
100                     ps.print(xml);
101                 } finally {
102                     ps.close();
103                 }
104             }
105         } catch (IOException JavaDoc ex) {
106             throw new BuildException(ex);
107         }
108     }
109
110     public String JavaDoc getOldName() {
111         return oldName;
112     }
113
114     public void setOldName(String JavaDoc oldName) {
115         this.oldName = oldName;
116     }
117
118     public String JavaDoc getNewPath() {
119         return newPath;
120     }
121
122     public void setNewPath(String JavaDoc newPath) {
123         this.newPath = newPath;
124     }
125
126     public String JavaDoc getPropertyPrefixName() {
127         return propertyPrefixName;
128     }
129
130     public void setPropertyPrefixName(String JavaDoc propertyPrefixName) {
131         this.propertyPrefixName = propertyPrefixName;
132     }
133
134     public File JavaDoc getFile() {
135         return file;
136     }
137
138     public void setFile(File JavaDoc file) {
139         this.file = file;
140     }
141
142     private String JavaDoc replaceFileName(String JavaDoc xml, int qStart, int qEnd) {
143         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
144         sb.append(xml.substring(0,qStart + 1));
145         if (propertyPrefixName != null) {
146             sb.append("${" + propertyPrefixName + "}/");
147         }
148         sb.append(getNewPath());
149         sb.append(xml.substring(qEnd));
150         return sb.toString();
151     }
152
153     /** check if position pos is inside xml comment
154      */

155     private boolean isComment(String JavaDoc xml, int offset, int position) {
156         boolean isComment = false;
157         while (offset < position) {
158             int i = -1;
159             if (isComment) {
160                 i = xml.indexOf("-->",offset);
161                 endOfComment = i + 2;
162             } else {
163                 i = xml.indexOf("<!--",offset);
164             }
165             if (i < position && i != -1) {
166                 isComment = !isComment;
167                 offset = i;
168             } else {
169                 break;
170             }
171         }
172         return isComment;
173     }
174     
175 }
176
Popular Tags