1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.IOException ; 25 import java.io.PrintStream ; 26 import org.apache.tools.ant.*; 27 28 40 public class ConvertImport extends Task { 41 private String oldName; 42 private String newPath; 43 private String propertyPrefixName; 44 private File 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 fis = new FileInputStream (file); 53 try { 54 fis.read(bytes); 55 } finally { 56 fis.close(); 57 } 58 String xml = new String (bytes); 59 String oldXml = xml; 60 int end = 0; 61 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 } if (oldXml != xml) { 97 PrintStream ps = new PrintStream (file); 99 try { 100 ps.print(xml); 101 } finally { 102 ps.close(); 103 } 104 } 105 } catch (IOException ex) { 106 throw new BuildException(ex); 107 } 108 } 109 110 public String getOldName() { 111 return oldName; 112 } 113 114 public void setOldName(String oldName) { 115 this.oldName = oldName; 116 } 117 118 public String getNewPath() { 119 return newPath; 120 } 121 122 public void setNewPath(String newPath) { 123 this.newPath = newPath; 124 } 125 126 public String getPropertyPrefixName() { 127 return propertyPrefixName; 128 } 129 130 public void setPropertyPrefixName(String propertyPrefixName) { 131 this.propertyPrefixName = propertyPrefixName; 132 } 133 134 public File getFile() { 135 return file; 136 } 137 138 public void setFile(File file) { 139 this.file = file; 140 } 141 142 private String replaceFileName(String xml, int qStart, int qEnd) { 143 StringBuffer sb = new StringBuffer (); 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 155 private boolean isComment(String 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 |