001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.nbbuild;
043:
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.io.IOException;
047: import java.io.PrintStream;
048: import org.apache.tools.ant.*;
049:
050: /**
051: * It replaces paths in import element of buildscript. It is for
052: * converting xtest builscript of modules to test distribution layout. <br>
053: *
054: * Parameters:
055: * <ul>
056: * <li> oldname - name of build script (for example 'cfg-qa-functiona.xml')
057: * <li> newpath - new path if file (for example '../templates/cfg-qa-functional.xml')
058: * <li> attribute prefix - property name (example fir dist 'dist.dir' it add ${dist.dir}/ prefix)
059: * <li> file - build script
060: * </ul>
061: */
062: public class ConvertImport extends Task {
063: private String oldName;
064: private String newPath;
065: private String propertyPrefixName;
066: private File file;
067: int endOfComment;
068:
069: public void execute() throws BuildException {
070: if (!file.exists()) {
071: throw new BuildException("File " + file + " doesn't exist.");
072: }
073: byte bytes[] = new byte[(int) file.length()];
074: try {
075: FileInputStream fis = new FileInputStream(file);
076: try {
077: fis.read(bytes);
078: } finally {
079: fis.close();
080: }
081: String xml = new String(bytes);
082: String oldXml = xml;
083: int end = 0;
084: // <import ... file=" "/>
085: for (int offset = 0; offset < xml.length(); offset = end + 1) {
086:
087: int start = xml.indexOf("<import ", offset);
088: if (start == -1) {
089: break;
090: }
091: if (isComment(xml, offset, start)) {
092: end = endOfComment;
093: continue;
094: }
095: end = xml.indexOf("/>", start);
096: if (end == -1) {
097: continue;
098: }
099: int fileIndex = xml.indexOf("file", start);
100: int q1 = xml.indexOf("\"", fileIndex);
101: int q2 = xml.indexOf("\'", fileIndex);
102: int qStart = (q1 != -1 && (q2 > q1 || q2 == -1)) ? q1
103: : q2;
104: if (qStart == -1) {
105: throw new BuildException("Invalid xml " + file);
106: }
107: char qCh = (qStart == q1) ? '"' : '\'';
108: int qEnd = xml.indexOf(qCh, qStart + 1);
109: if (qEnd == -1 || qEnd > end) {
110: throw new BuildException("Invalid xml : " + file);
111: }
112:
113: int nameIdx = xml.indexOf(oldName, qCh);
114: if (nameIdx != -1 && nameIdx < qEnd) {
115: xml = replaceFileName(xml, qStart, qEnd);
116: end = xml.indexOf("/>", start);
117: }
118:
119: } // while
120: if (oldXml != xml) {
121: // changed file
122: PrintStream ps = new PrintStream(file);
123: try {
124: ps.print(xml);
125: } finally {
126: ps.close();
127: }
128: }
129: } catch (IOException ex) {
130: throw new BuildException(ex);
131: }
132: }
133:
134: public String getOldName() {
135: return oldName;
136: }
137:
138: public void setOldName(String oldName) {
139: this .oldName = oldName;
140: }
141:
142: public String getNewPath() {
143: return newPath;
144: }
145:
146: public void setNewPath(String newPath) {
147: this .newPath = newPath;
148: }
149:
150: public String getPropertyPrefixName() {
151: return propertyPrefixName;
152: }
153:
154: public void setPropertyPrefixName(String propertyPrefixName) {
155: this .propertyPrefixName = propertyPrefixName;
156: }
157:
158: public File getFile() {
159: return file;
160: }
161:
162: public void setFile(File file) {
163: this .file = file;
164: }
165:
166: private String replaceFileName(String xml, int qStart, int qEnd) {
167: StringBuffer sb = new StringBuffer();
168: sb.append(xml.substring(0, qStart + 1));
169: if (propertyPrefixName != null) {
170: sb.append("${" + propertyPrefixName + "}/");
171: }
172: sb.append(getNewPath());
173: sb.append(xml.substring(qEnd));
174: return sb.toString();
175: }
176:
177: /** check if position pos is inside xml comment
178: */
179: private boolean isComment(String xml, int offset, int position) {
180: boolean isComment = false;
181: while (offset < position) {
182: int i = -1;
183: if (isComment) {
184: i = xml.indexOf("-->", offset);
185: endOfComment = i + 2;
186: } else {
187: i = xml.indexOf("<!--", offset);
188: }
189: if (i < position && i != -1) {
190: isComment = !isComment;
191: offset = i;
192: } else {
193: break;
194: }
195: }
196: return isComment;
197: }
198:
199: }
|