1 17 package org.eclipse.emf.codegen.jmerge; 18 19 20 import java.io.BufferedInputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.util.Iterator ; 26 import java.util.LinkedHashMap ; 27 import java.util.Map ; 28 import java.util.regex.Matcher ; 29 import java.util.regex.Pattern ; 30 31 import org.eclipse.core.runtime.IPlatformRunnable; 32 33 34 38 public class PropertyMerger implements IPlatformRunnable 39 { 40 protected String sourceProperties; 41 protected String targetProperties; 42 protected Map sourceToTargetMap = new LinkedHashMap (); 43 protected Map targetToSourceMap = new LinkedHashMap (); 44 45 48 public PropertyMerger() 49 { 50 } 51 52 public String getSourceProperties() 53 { 54 return sourceProperties; 55 } 56 57 public void setSourceProperties(String sourceProperties) 58 { 59 this.sourceProperties = sourceProperties; 60 } 61 62 public String getTargetProperties() 63 { 64 return targetProperties; 65 } 66 67 public void setTargetProperties(String targetProperties) 68 { 69 this.targetProperties = targetProperties; 70 } 71 72 public Map getSourceToTargetMap() 73 { 74 return sourceToTargetMap; 75 } 76 77 80 public String createPropertiesForURI(String uri) 81 { 82 try 83 { 84 URL url = null; 85 try 86 { 87 url = new URL (uri); 88 } 89 catch (MalformedURLException exception) 90 { 91 url = new URL ("file:" + uri); 92 } 93 if (url != null) 94 { 95 BufferedInputStream bufferedInputStream = new BufferedInputStream (url.openStream()); 96 byte [] input = new byte [bufferedInputStream.available()]; 97 bufferedInputStream.read(input); 98 bufferedInputStream.close(); 99 return new String (input, "ISO-8859-1"); 100 } 101 } 102 catch (IOException exception) 103 { 104 } 105 106 return null; 107 } 108 109 public String createPropertiesForInputStream(InputStream inputStream) 110 { 111 try 112 { 113 BufferedInputStream bufferedInputStream = new BufferedInputStream (inputStream); 114 byte [] input = new byte [bufferedInputStream.available()]; 115 bufferedInputStream.read(input); 116 bufferedInputStream.close(); 117 return new String (input, "ISO-8859-1"); 118 } 119 catch (IOException exception) 120 { 121 } 122 return null; 123 } 124 125 protected static Pattern nlPattern = Pattern.compile("([\\n][\\r]?|[\\r][\\n]?)", Pattern.MULTILINE); 126 127 public void merge() 128 { 129 Matcher matcher = nlPattern.matcher(targetProperties); 130 String nl = null; 131 if (matcher.find()) 132 { 133 nl = matcher.group(1); 134 135 matcher = nlPattern.matcher(sourceProperties); 136 if (matcher.find()) 137 { 138 String sourceNL = matcher.group(1); 139 if (!sourceNL.equals(nl)) 140 { 141 sourceProperties = sourceProperties.replaceAll(sourceNL, nl); 142 } 143 } 144 } 145 else 146 { 147 matcher = nlPattern.matcher(sourceProperties); 148 if (matcher.find()) 149 { 150 nl = matcher.group(1); 151 } 152 } 153 154 if (nl != null) 155 { 156 if (!targetProperties.endsWith(nl)) 157 { 158 targetProperties += nl; 159 } 160 161 if (!sourceProperties.endsWith(nl)) 162 { 163 sourceProperties += nl; 164 } 165 } 166 167 Map sourcePropertyFragments = parse(sourceProperties); 168 Map targetPropertyFragments = parse(targetProperties); 169 170 StringBuffer result = new StringBuffer (targetProperties); 171 for (Iterator i = sourcePropertyFragments.entrySet().iterator(); i.hasNext(); ) 172 { 173 Map.Entry entry = (Map.Entry )i.next(); 174 if (!targetPropertyFragments.containsKey(entry.getKey())) 175 { 176 result.append(entry.getValue()); 177 } 178 } 179 180 targetProperties = result.toString(); 181 } 182 183 protected static Pattern propertyLine = Pattern.compile("\\s*(\\S+)\\s*=.*", Pattern.MULTILINE); 184 185 public Map parse(String properties) 186 { 187 Map result = new LinkedHashMap (); 188 int i = 0; 189 while (i < properties.length()) 190 { 191 int eol = properties.indexOf("\n", i); 192 if (eol != -1) 193 { 194 if (eol + 1 < properties.length() && properties.charAt(eol + 1) == '\r') 195 { 196 ++eol; 197 } 198 } 199 else 200 { 201 eol = properties.indexOf("\r", i); 202 if (eol == -1) 203 { 204 eol = properties.length() - 1; 205 } 206 } 207 208 String property = properties.substring(i, eol + 1); 209 Matcher matcher = propertyLine.matcher(property); 210 if (matcher.find() && matcher.groupCount() >= 1) 211 { 212 int begin = matcher.start(1); 213 int end = matcher.end(1); 214 String propertyName = property.substring(begin, end); 215 if (propertyName.indexOf("#") == -1) 216 { 217 result.put(propertyName, property); 218 } 219 else if (propertyName.startsWith("#")) 220 { 221 result.put(propertyName.substring(1), property); 222 } 223 } 224 225 i = eol + 1; 226 } 227 228 return result; 229 } 230 231 232 234 237 public Object run(Object object) 238 { 239 try 240 { 241 String [] arguments = (String [])object; 244 245 sourceProperties = createPropertiesForURI(arguments[0]); 248 targetProperties = createPropertiesForURI(arguments[1]); 249 250 merge(); 251 252 System.out.println("**********************************************"); 253 System.out.println(targetProperties); 254 255 return new Integer (0); 256 } 257 catch (Exception exception) 258 { 259 return new Integer (1); 261 } 262 } 263 } 264 | Popular Tags |