KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > codegen > jmerge > PropertyMerger


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: PropertyMerger.java,v 1.5 2005/06/08 06:15:57 nickb Exp $
16  */

17 package org.eclipse.emf.codegen.jmerge;
18
19
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedHashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.regex.Matcher JavaDoc;
29 import java.util.regex.Pattern JavaDoc;
30
31 import org.eclipse.core.runtime.IPlatformRunnable;
32
33
34 /**
35  * This implements the method {@link #run},
36  * which is called just like main during headless workbench invocation.
37  */

38 public class PropertyMerger implements IPlatformRunnable
39 {
40   protected String JavaDoc sourceProperties;
41   protected String JavaDoc targetProperties;
42   protected Map JavaDoc sourceToTargetMap = new LinkedHashMap JavaDoc();
43   protected Map JavaDoc targetToSourceMap = new LinkedHashMap JavaDoc();
44
45   /**
46    * This creates an empty instances, when used as a runnable.
47    */

48   public PropertyMerger()
49   {
50   }
51
52   public String JavaDoc getSourceProperties()
53   {
54     return sourceProperties;
55   }
56
57   public void setSourceProperties(String JavaDoc sourceProperties)
58   {
59     this.sourceProperties = sourceProperties;
60   }
61
62   public String JavaDoc getTargetProperties()
63   {
64     return targetProperties;
65   }
66
67   public void setTargetProperties(String JavaDoc targetProperties)
68   {
69     this.targetProperties = targetProperties;
70   }
71
72   public Map JavaDoc getSourceToTargetMap()
73   {
74     return sourceToTargetMap;
75   }
76
77   /**
78    * Create a JDOM from a URI.
79    */

80   public String JavaDoc createPropertiesForURI(String JavaDoc uri)
81   {
82     try
83     {
84       URL JavaDoc url = null;
85       try
86       {
87         url = new URL JavaDoc(uri);
88       }
89       catch (MalformedURLException JavaDoc exception)
90       {
91         url = new URL JavaDoc("file:" + uri);
92       }
93       if (url != null)
94       {
95         BufferedInputStream JavaDoc bufferedInputStream = new BufferedInputStream JavaDoc(url.openStream());
96         byte [] input = new byte [bufferedInputStream.available()];
97         bufferedInputStream.read(input);
98         bufferedInputStream.close();
99         return new String JavaDoc(input, "ISO-8859-1");
100       }
101     }
102     catch (IOException JavaDoc exception)
103     {
104     }
105
106     return null;
107   }
108
109   public String JavaDoc createPropertiesForInputStream(InputStream JavaDoc inputStream)
110   {
111     try
112     {
113       BufferedInputStream JavaDoc bufferedInputStream = new BufferedInputStream JavaDoc(inputStream);
114       byte [] input = new byte [bufferedInputStream.available()];
115       bufferedInputStream.read(input);
116       bufferedInputStream.close();
117       return new String JavaDoc(input, "ISO-8859-1");
118     }
119     catch (IOException JavaDoc exception)
120     {
121     }
122     return null;
123   }
124
125   protected static Pattern JavaDoc nlPattern = Pattern.compile("([\\n][\\r]?|[\\r][\\n]?)", Pattern.MULTILINE);
126   
127   public void merge()
128   {
129     Matcher JavaDoc matcher = nlPattern.matcher(targetProperties);
130     String JavaDoc 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 JavaDoc 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 JavaDoc sourcePropertyFragments = parse(sourceProperties);
168     Map JavaDoc targetPropertyFragments = parse(targetProperties);
169     
170     StringBuffer JavaDoc result = new StringBuffer JavaDoc(targetProperties);
171     for (Iterator JavaDoc i = sourcePropertyFragments.entrySet().iterator(); i.hasNext(); )
172     {
173       Map.Entry JavaDoc entry = (Map.Entry JavaDoc)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 JavaDoc propertyLine = Pattern.compile("\\s*(\\S+)\\s*=.*", Pattern.MULTILINE);
184
185   public Map JavaDoc parse(String JavaDoc properties)
186   {
187     Map JavaDoc result = new LinkedHashMap JavaDoc();
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 JavaDoc property = properties.substring(i, eol + 1);
209       Matcher JavaDoc 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 JavaDoc 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 ///////////////////////////////// HEADLESS INVOCATION /////////////////////////////////////
233

234   /**
235    * This is called with the command line arguments of a headless workbench invocation.
236    */

237   public Object JavaDoc run(Object JavaDoc object)
238   {
239     try
240     {
241       // Three arguments are expected: the .xml jControlModel URI, the source java URI, and the target java URI.
242
//
243
String JavaDoc[] arguments = (String JavaDoc[])object;
244
245       // Create the source and target JDOMs.
246
//
247
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 JavaDoc(0);
256     }
257     catch (Exception JavaDoc exception)
258     {
259       // exception.printStackTrace();
260
return new Integer JavaDoc(1);
261     }
262   }
263 }
264
Popular Tags