You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ds...@apache.org on 2009/07/21 20:51:41 UTC

svn commit: r796467 [2/25] - in /felix/trunk/sigil: common/core.tests/src/org/apache/felix/sigil/core/ common/core/src/org/apache/felix/sigil/bnd/ common/core/src/org/apache/felix/sigil/config/ common/core/src/org/apache/felix/sigil/core/ common/core/s...

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/config/BldAttr.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/config/BldAttr.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/config/BldAttr.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/config/BldAttr.java Tue Jul 21 18:51:33 2009
@@ -19,32 +19,34 @@
 
 package org.apache.felix.sigil.config;
 
-public class BldAttr {
-	// Sigil attributes
-	
-	public static final String KIND_ATTRIBUTE = "kind";
-	
-	public static final String RESOLVE_ATTRIBUTE = "resolve";
-	public static final String RESOLVE_AUTO = "auto";
-	public static final String RESOLVE_COMPILE = "compile";
-	public static final String RESOLVE_RUNTIME = "runtime";
-	public static final String RESOLVE_IGNORE = "ignore";
-	
-	public static final String PUBLISH_ATTRIBUTE = "publish";
-	public static final String PUBTYPE_ATTRIBUTE = "type";
-	public static final String PATH_ATTRIBUTE = "path";
-	public static final Object ZONE_ATTRIBUTE = "zone";
-	
-	// Sigil options
-	
-	public static final String OPTION_ADD_IMPORTS = "addMissingImports";
-	public static final String OPTION_OMIT_IMPORTS = "omitUnusedImports";
-	
-	// OSGi attributes
-	
-	public static final String RESOLUTION_ATTRIBUTE = "resolution";
-	public static final String RESOLUTION_OPTIONAL = "optional";
-	
-	public static final String VERSION_ATTRIBUTE = "version";
+
+public class BldAttr
+{
+    // Sigil attributes
+
+    public static final String KIND_ATTRIBUTE = "kind";
+
+    public static final String RESOLVE_ATTRIBUTE = "resolve";
+    public static final String RESOLVE_AUTO = "auto";
+    public static final String RESOLVE_COMPILE = "compile";
+    public static final String RESOLVE_RUNTIME = "runtime";
+    public static final String RESOLVE_IGNORE = "ignore";
+
+    public static final String PUBLISH_ATTRIBUTE = "publish";
+    public static final String PUBTYPE_ATTRIBUTE = "type";
+    public static final String PATH_ATTRIBUTE = "path";
+    public static final Object ZONE_ATTRIBUTE = "zone";
+
+    // Sigil options
+
+    public static final String OPTION_ADD_IMPORTS = "addMissingImports";
+    public static final String OPTION_OMIT_IMPORTS = "omitUnusedImports";
+
+    // OSGi attributes
+
+    public static final String RESOLUTION_ATTRIBUTE = "resolution";
+    public static final String RESOLUTION_OPTIONAL = "optional";
+
+    public static final String VERSION_ATTRIBUTE = "version";
 
 }

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/config/BldConfig.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/config/BldConfig.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/config/BldConfig.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/config/BldConfig.java Tue Jul 21 18:51:33 2009
@@ -19,6 +19,7 @@
 
 package org.apache.felix.sigil.config;
 
+
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
@@ -31,415 +32,535 @@
 
 import org.apache.felix.sigil.core.util.QuoteUtil;
 
-public class BldConfig {
-	
-	// control properties
-	public static final String C_BUNDLES = "-bundles";
-	public static final String C_REPOSITORIES = "-repositories";
-
-	// string properties
-	public static final String S_ACTIVATOR = "-activator";
-	public static final String S_DEFAULTS = "-defaults";
-	public static final String S_ID = "id";
-	public static final String S_SYM_NAME = "name";
-	public static final String S_VERSION = "version";
-	public static final String[] STRING_KEYS = { S_ACTIVATOR, S_DEFAULTS, S_ID, S_SYM_NAME, S_VERSION };
-
-	// list properties
-	public static final String L_COMPOSITES = "-composites";
-	public static final String L_CONTENTS = "-contents";
-	public static final String L_DL_CONTENTS = "-downloads";
-	public static final String L_SRC_CONTENTS = "-sourcedirs";
-	public static final String L_RESOURCES = "-resources";
-	public static final String[] LIST_KEYS = {
-		L_COMPOSITES, L_CONTENTS, L_DL_CONTENTS, L_SRC_CONTENTS, L_RESOURCES };
-
-	// map properties
-	public static final String M_EXPORTS = "-exports";
-	public static final String M_IMPORTS = "-imports";
-	public static final String M_REQUIRES = "-requires";
-	public static final String M_FRAGMENT = "-fragment";
-	public static final String M_LIBS = "-libs";
-	public static final String[] MAP_KEYS = { M_EXPORTS, M_IMPORTS, M_REQUIRES, M_FRAGMENT, M_LIBS };
-	
-	// property properties
-	public static final String P_HEADER = "header";
-	public static final String P_OPTION = "option";
-	public static final String P_PACKAGE_VERSION = "package";
-	public static final String P_BUNDLE_VERSION = "bundle";
-	public static final String[] PROP_KEYS = { P_HEADER, P_OPTION, P_PACKAGE_VERSION, P_BUNDLE_VERSION };
-
-	// private constants
-	private static final String LIST_REGEX = ",\\s*";
-	private static final String MAPATTR_REGEX = ";\\s*";
-	private static final String MAPATTR_SEP = ";";
-	private static final String SUBKEY_SEP = ";";
-
-	// configuration is stored in typed maps
-	private Map<String, String> string = new TreeMap<String, String>();
-	private Map<String, List<String>> list = new TreeMap<String, List<String>>();
-	private Map<String, Map<String, Map<String, String>>> map = new TreeMap<String, Map<String,Map<String,String>>>();
-	private Map<String, BldConfig> config = new TreeMap<String, BldConfig>();
-	private Map<String, Properties> property = new TreeMap<String, Properties>();
-	
-	// default config - not modified or saved
-	private BldConfig dflt;
-	
-	private Properties unknown = new Properties();
-	private String comment = "";
-	
-	public BldConfig() {
-	}
-		
-	public BldConfig(Properties p) throws IOException {
-		merge(p);
-	}
-	
-	public void setDefault(BldConfig dflt) {
-		this.dflt = dflt;
-	}
-
-	public void setComment(String comment) {
-		this.comment = comment;
-	}
-	
-	public Properties getUnknown() {
-		return unknown;
-	}
-
-	public String getString(String id, String key) {
-		if (id != null && config.containsKey(id)) {
-			String value = config.get(id).getString(null, key);
-			if (value != null)
-				return value;
-		}
-		return string.containsKey(key) ? string.get(key) : (dflt != null ? dflt.getString(id, key) : null);
-	}
-
-	public List<String> getList(String id, String key) {
-		if (id != null && config.containsKey(id)) {
-			List<String> value = config.get(id).getList(null, key);
-			if (value != null)
-				return value;
-		}
-		return list.containsKey(key) ? list.get(key) : (dflt != null ? dflt.getList(id, key) : Collections.<String>emptyList());
-	}
-
-	public Map<String, Map<String,String>> getMap(String id, String key) {
-		if (id != null && config.containsKey(id)) {
-			Map<String, Map<String,String>> value = config.get(id).getMap(null, key);
-			if (value != null)
-				return value;
-		}
-		return map.containsKey(key) ? map.get(key)
-				: (dflt != null ? dflt.getMap(id, key) : Collections.<String, Map<String,String>>emptyMap());
-	}
-	
-	public void setString(String id, String key, String value) {
-		if (!value.equals(getString(id, key))) {
-			if (id != null) {
-    			if (!config.containsKey(id))
-    				config.put(id, new BldConfig());
-    			config.get(id).setString(null, key, value);
-			} else {
-				String dval = (dflt == null ? dflt.getString(null, key) : null);
-				if (value.equals("") && (dval == null || dval.equals(""))) {
-    		        string.remove(key);	
-    			} else {
-            		string.put(key, value);
-    			}
-			}
-		}
-	}
-
-	public void setList(String id, String key, List<String> value) {
-		if (!value.equals(getList(id, key))) {
-			if (id != null) {
-    			if (!config.containsKey(id))
-    				config.put(id, new BldConfig());
-    			config.get(id).setList(null, key, value);
-			} else if (value.isEmpty() && (dflt == null || dflt.getList(null, key).isEmpty())) {
-		        list.remove(key);	
-			} else {
-        		list.put(key, value);
-			}
-		}
-	}
-
-	public void setMap(String id, String key, Map<String, Map<String,String>> value) {
-		if (!value.equals(getMap(id, key))) {
-			if (id != null) {
-    			if (!config.containsKey(id))
-    				config.put(id, new BldConfig());
-    			config.get(id).setMap(null, key, value);
-			} else if (value.isEmpty() && (dflt == null || dflt.getMap(null, key).isEmpty())) {
-		        map.remove(key);	
-			} else {
-        		map.put(key, value);
-			}
-		}
-	}
-	
-	public Properties getProps(String id, String key) {
-    	// merge main and sub properties
-		Properties props = new Properties();
-		
-		if (dflt != null)
-    		props.putAll(dflt.getProps(id, key));
-		
-		if (property.containsKey(key))
-			props.putAll(property.get(key));
-
-		if (id != null && config.containsKey(id)) {
-			Properties p2 = config.get(id).getProps(null, key);
-			if (p2 != null)
-				props.putAll(p2);
-		}
-
-		return props;
-	}
-	
-	// only sets one property at a time
-	public void setProp(String id, String key, String k2, String v2) {
-		if (v2 == null)
-			return;
-		Properties props = getProps(id, key);
-		if (!v2.equals(props.getProperty(key))) {
-			if (id != null) {
-    			if (!config.containsKey(id))
-    				config.put(id, new BldConfig());
-    			config.get(id).setProp(null, key, k2, v2);
-			} else {
-        		if (property.containsKey(key)) {
-        			property.get(key).put(k2, v2);
-        		} else {
-            		Properties value = new Properties();
-            		value.put(k2, v2);
-            		property.put(key, value);
-        		}
-			}
-		}
-	}
-
-	/**
-	 * write config in Property file format.
-	 * This allows us to make it prettier than Properties.store().
-	 */
-	public void write(final PrintWriter out) {
-		out.println(comment);
-		
-		// Note: don't add date stamp, or file will differ each time it's saved.
-		out.println("# sigil project file, saved by plugin.\n");
-		
-		dump("", new Properties() {
-			private static final long serialVersionUID = 1L; //appease eclipse
-			@Override
-			public Object put(Object key, Object value) {
-				if (value instanceof String) {
-					out.println(key + ": " + value);
-					out.println("");
-				} else if (value instanceof List) {
-					out.println(key + ": \\");
-					for (Object k : (List<?>) value) {
-						out.println("\t" + k + ", \\");
-					}
-					out.println("");
-				}
-				else if (value instanceof Map) {
-					out.println(key + ": \\");
-					StringBuilder b = new StringBuilder();
-					for (Map.Entry<?, ?> e : ((Map<?,?>) value).entrySet()) {
-						b.append("\t");
-						b.append(e.getKey());
-						Map<?, ?> v = (Map<?, ?>) e.getValue();
-						if (!v.isEmpty()) {
-							for (Map.Entry<?, ?> e2 : v.entrySet()) {
-    							b.append(MAPATTR_SEP);
-								b.append(e2.getKey());
-								b.append("=");
-								String v2 = e2.getValue().toString();
-								if (v2.contains(",")) {
-									b.append("\"");
-									b.append(v2);
-									b.append("\"");
-								}
-								else {
-									b.append(v2);
-								}
-							}
-						}
-						b.append (", \\\n"); 
-					}
-					out.println(b.toString());
-				}
-				return null;
-			}
-		});
-		out.println("# end");
-	}
-
-	/**
-	 * dump config in pseudo Properties format.
-	 * Note: some values are not Strings (they're List<String>).
-	 */
-	private void dump(String prefix, Properties p) {
-		for (String key : string.keySet()) {
-			p.put(prefix + key, string.get(key));
-		}
-		
-		for (String key : list.keySet()) {
-			List<String> list2 = list.get(key);
-			p.put(prefix + key, list2);
-		}
-
-		for (String key : map.keySet()) {
-			Map<String, Map<String,String>> map2 = map.get(key);
-			p.put(prefix + key, map2);
-		}
-
-		for (String key : property.keySet()) {
-			Properties props = property.get(key);
-			for (Object k2 : props.keySet()) {
-				p.put(prefix + key + SUBKEY_SEP + k2, props.get(k2));
-			}
-		}
-
-		for (String key : config.keySet()) {
-			BldConfig config2 = config.get(key);
-			config2.dump(key + SUBKEY_SEP + prefix, p);
-		}
-
-		for (Object key : unknown.keySet()) {
-			String value = unknown.getProperty((String)key);
-			if (value.length() > 0)
-    			p.put(prefix + key, value);
-		}
-	}
-
-	/**
-	 * merges properties into current configuration.
-	 * @param p
-	 * @throws IOException 
-	 */
-	public void merge(Properties p) throws IOException {
-		if (p.isEmpty())
-			return;
-		
-		final List<String> strings = Arrays.asList(STRING_KEYS);
-		final List<String> lists = Arrays.asList(LIST_KEYS);
-		final List<String> maps = Arrays.asList(MAP_KEYS);
-
-		List<String> bundleKeys = new ArrayList<String>();
-		List<String> repoKeys = new ArrayList<String>();
-
-		String bundles = p.getProperty(C_BUNDLES);
-		if (bundles != null) {
-			bundleKeys.addAll(Arrays.asList(bundles.split(LIST_REGEX)));
-			list.put(C_BUNDLES, bundleKeys);
-		}
-
-		String repos = p.getProperty(C_REPOSITORIES);
-		if (repos != null) {
-			for ( String s : repos.split(LIST_REGEX) ) {
-				repoKeys.add(s.trim());
-			}
-			list.put(C_REPOSITORIES, repoKeys);
-		}
-
-		List<String> subKeys = new ArrayList<String>();
-		subKeys.addAll(Arrays.asList(PROP_KEYS));
-		subKeys.addAll(bundleKeys);
-		subKeys.addAll(repoKeys);
-
-		Map<String, Properties> sub = new TreeMap<String, Properties>();
-
-		for (Object k : p.keySet()) {
-			String key = (String) k;
-			if (key.equals(C_BUNDLES) || key.equals(C_REPOSITORIES))
-				continue;
-			
-			String value = p.getProperty(key);
-			String[] keys = key.split(SUBKEY_SEP, 2);
-
-			if (keys.length > 1) {
-				Properties p2 = sub.get(keys[0]);
-				if (p2 == null) {
-					p2 = new Properties();
-					sub.put(keys[0], p2);
-					if (!subKeys.contains(keys[0])) {
-						unknown.setProperty(keys[0] + SUBKEY_SEP, "");
-					}
-				}
-				p2.setProperty(keys[1], value);
-			} else if (strings.contains(key)) {
-				if (!string.containsKey(key))
-					string.put(key, value);
-			} else if (lists.contains(key)) {
-				if (!list.containsKey(key)) {
-					ArrayList<String> list2 = new ArrayList<String>();
-					for (String s : value.split(LIST_REGEX)) {
-						if ( s.trim().length() > 0 ) {
-							list2.add(s.trim());
-						}
-					}
-					if ( !list2.isEmpty() ) {
-						list.put(key, list2);
-					}
-				}
-			} else if (maps.contains(key)) {
-				if (!map.containsKey(key)) {
-					Map<String, Map<String,String>> map2 = new TreeMap<String, Map<String,String>>();
-
-					for (String subValue : QuoteUtil.split(value)) {
-						if (subValue.trim().length() > 0) {
-							String[] split = subValue.split(MAPATTR_REGEX);
-							Map<String,String> map3 = new TreeMap<String,String>();
-							for (int i = 1; i < split.length; ++i){
-								String[] keyVal = split[i].split(":?=", 2);
-								if (keyVal.length != 2) {
-								    throw new IOException("attribute missing '=':" + subValue);
-								}
-								map3.put(keyVal[0], keyVal[1]);
-							}
-							map2.put(split[0], map3);
-						}
-					}
-
-					map.put(key, map2);
-				}
-			} else {
-				unknown.setProperty(key, value);
-			}
-		}
-		
-		for (String subKey : sub.keySet()) {
-			Properties props = sub.get(subKey);
-			if (!props.isEmpty()) {
-				if (bundleKeys.contains(subKey)) {
-					BldConfig config2 = new BldConfig(props);
-					Properties unkProps = config2.getUnknown();
-					
-					if (config2.map.containsKey(M_IMPORTS))
-						unkProps.setProperty(M_IMPORTS, "");
-						
-					if (config2.map.containsKey(M_REQUIRES))
-						unkProps.setProperty(M_REQUIRES, "");
-						
-					for (Object unk : unkProps.keySet()) {
-						unknown.setProperty(subKey + SUBKEY_SEP + unk, "");
-					}
-					config.put(subKey, config2);
-				} else {
-					property.put(subKey, props);
-				}
-			}
-		}
-	}
-	
-	@Override
-	public String toString() {
-		return "string: " + string + " list:" + list + " map: " + map + " prop: " + property + " config:" + config;
-	}
 
-}
+public class BldConfig
+{
 
+    // control properties
+    public static final String C_BUNDLES = "-bundles";
+    public static final String C_REPOSITORIES = "-repositories";
+
+    // string properties
+    public static final String S_ACTIVATOR = "-activator";
+    public static final String S_DEFAULTS = "-defaults";
+    public static final String S_ID = "id";
+    public static final String S_SYM_NAME = "name";
+    public static final String S_VERSION = "version";
+    public static final String[] STRING_KEYS =
+        { S_ACTIVATOR, S_DEFAULTS, S_ID, S_SYM_NAME, S_VERSION };
+
+    // list properties
+    public static final String L_COMPOSITES = "-composites";
+    public static final String L_CONTENTS = "-contents";
+    public static final String L_DL_CONTENTS = "-downloads";
+    public static final String L_SRC_CONTENTS = "-sourcedirs";
+    public static final String L_RESOURCES = "-resources";
+    public static final String[] LIST_KEYS =
+        { L_COMPOSITES, L_CONTENTS, L_DL_CONTENTS, L_SRC_CONTENTS, L_RESOURCES };
+
+    // map properties
+    public static final String M_EXPORTS = "-exports";
+    public static final String M_IMPORTS = "-imports";
+    public static final String M_REQUIRES = "-requires";
+    public static final String M_FRAGMENT = "-fragment";
+    public static final String M_LIBS = "-libs";
+    public static final String[] MAP_KEYS =
+        { M_EXPORTS, M_IMPORTS, M_REQUIRES, M_FRAGMENT, M_LIBS };
+
+    // property properties
+    public static final String P_HEADER = "header";
+    public static final String P_OPTION = "option";
+    public static final String P_PACKAGE_VERSION = "package";
+    public static final String P_BUNDLE_VERSION = "bundle";
+    public static final String[] PROP_KEYS =
+        { P_HEADER, P_OPTION, P_PACKAGE_VERSION, P_BUNDLE_VERSION };
+
+    // private constants
+    private static final String LIST_REGEX = ",\\s*";
+    private static final String MAPATTR_REGEX = ";\\s*";
+    private static final String MAPATTR_SEP = ";";
+    private static final String SUBKEY_SEP = ";";
+
+    // configuration is stored in typed maps
+    private Map<String, String> string = new TreeMap<String, String>();
+    private Map<String, List<String>> list = new TreeMap<String, List<String>>();
+    private Map<String, Map<String, Map<String, String>>> map = new TreeMap<String, Map<String, Map<String, String>>>();
+    private Map<String, BldConfig> config = new TreeMap<String, BldConfig>();
+    private Map<String, Properties> property = new TreeMap<String, Properties>();
+
+    // default config - not modified or saved
+    private BldConfig dflt;
+
+    private Properties unknown = new Properties();
+    private String comment = "";
+
+
+    public BldConfig()
+    {
+    }
+
+
+    public BldConfig( Properties p ) throws IOException
+    {
+        merge( p );
+    }
+
+
+    public void setDefault( BldConfig dflt )
+    {
+        this.dflt = dflt;
+    }
+
+
+    public void setComment( String comment )
+    {
+        this.comment = comment;
+    }
+
+
+    public Properties getUnknown()
+    {
+        return unknown;
+    }
+
+
+    public String getString( String id, String key )
+    {
+        if ( id != null && config.containsKey( id ) )
+        {
+            String value = config.get( id ).getString( null, key );
+            if ( value != null )
+                return value;
+        }
+        return string.containsKey( key ) ? string.get( key ) : ( dflt != null ? dflt.getString( id, key ) : null );
+    }
+
+
+    public List<String> getList( String id, String key )
+    {
+        if ( id != null && config.containsKey( id ) )
+        {
+            List<String> value = config.get( id ).getList( null, key );
+            if ( value != null )
+                return value;
+        }
+        return list.containsKey( key ) ? list.get( key ) : ( dflt != null ? dflt.getList( id, key ) : Collections
+            .<String> emptyList() );
+    }
+
+
+    public Map<String, Map<String, String>> getMap( String id, String key )
+    {
+        if ( id != null && config.containsKey( id ) )
+        {
+            Map<String, Map<String, String>> value = config.get( id ).getMap( null, key );
+            if ( value != null )
+                return value;
+        }
+        return map.containsKey( key ) ? map.get( key ) : ( dflt != null ? dflt.getMap( id, key ) : Collections
+            .<String, Map<String, String>> emptyMap() );
+    }
+
+
+    public void setString( String id, String key, String value )
+    {
+        if ( !value.equals( getString( id, key ) ) )
+        {
+            if ( id != null )
+            {
+                if ( !config.containsKey( id ) )
+                    config.put( id, new BldConfig() );
+                config.get( id ).setString( null, key, value );
+            }
+            else
+            {
+                String dval = ( dflt == null ? dflt.getString( null, key ) : null );
+                if ( value.equals( "" ) && ( dval == null || dval.equals( "" ) ) )
+                {
+                    string.remove( key );
+                }
+                else
+                {
+                    string.put( key, value );
+                }
+            }
+        }
+    }
+
+
+    public void setList( String id, String key, List<String> value )
+    {
+        if ( !value.equals( getList( id, key ) ) )
+        {
+            if ( id != null )
+            {
+                if ( !config.containsKey( id ) )
+                    config.put( id, new BldConfig() );
+                config.get( id ).setList( null, key, value );
+            }
+            else if ( value.isEmpty() && ( dflt == null || dflt.getList( null, key ).isEmpty() ) )
+            {
+                list.remove( key );
+            }
+            else
+            {
+                list.put( key, value );
+            }
+        }
+    }
+
+
+    public void setMap( String id, String key, Map<String, Map<String, String>> value )
+    {
+        if ( !value.equals( getMap( id, key ) ) )
+        {
+            if ( id != null )
+            {
+                if ( !config.containsKey( id ) )
+                    config.put( id, new BldConfig() );
+                config.get( id ).setMap( null, key, value );
+            }
+            else if ( value.isEmpty() && ( dflt == null || dflt.getMap( null, key ).isEmpty() ) )
+            {
+                map.remove( key );
+            }
+            else
+            {
+                map.put( key, value );
+            }
+        }
+    }
+
+
+    public Properties getProps( String id, String key )
+    {
+        // merge main and sub properties
+        Properties props = new Properties();
+
+        if ( dflt != null )
+            props.putAll( dflt.getProps( id, key ) );
+
+        if ( property.containsKey( key ) )
+            props.putAll( property.get( key ) );
+
+        if ( id != null && config.containsKey( id ) )
+        {
+            Properties p2 = config.get( id ).getProps( null, key );
+            if ( p2 != null )
+                props.putAll( p2 );
+        }
+
+        return props;
+    }
+
+
+    // only sets one property at a time
+    public void setProp( String id, String key, String k2, String v2 )
+    {
+        if ( v2 == null )
+            return;
+        Properties props = getProps( id, key );
+        if ( !v2.equals( props.getProperty( key ) ) )
+        {
+            if ( id != null )
+            {
+                if ( !config.containsKey( id ) )
+                    config.put( id, new BldConfig() );
+                config.get( id ).setProp( null, key, k2, v2 );
+            }
+            else
+            {
+                if ( property.containsKey( key ) )
+                {
+                    property.get( key ).put( k2, v2 );
+                }
+                else
+                {
+                    Properties value = new Properties();
+                    value.put( k2, v2 );
+                    property.put( key, value );
+                }
+            }
+        }
+    }
+
+
+    /**
+     * write config in Property file format.
+     * This allows us to make it prettier than Properties.store().
+     */
+    public void write( final PrintWriter out )
+    {
+        out.println( comment );
+
+        // Note: don't add date stamp, or file will differ each time it's saved.
+        out.println( "# sigil project file, saved by plugin.\n" );
+
+        dump( "", new Properties()
+        {
+            private static final long serialVersionUID = 1L; //appease eclipse
+
+
+            @Override
+            public Object put( Object key, Object value )
+            {
+                if ( value instanceof String )
+                {
+                    out.println( key + ": " + value );
+                    out.println( "" );
+                }
+                else if ( value instanceof List )
+                {
+                    out.println( key + ": \\" );
+                    for ( Object k : ( List<?> ) value )
+                    {
+                        out.println( "\t" + k + ", \\" );
+                    }
+                    out.println( "" );
+                }
+                else if ( value instanceof Map )
+                {
+                    out.println( key + ": \\" );
+                    StringBuilder b = new StringBuilder();
+                    for ( Map.Entry<?, ?> e : ( ( Map<?, ?> ) value ).entrySet() )
+                    {
+                        b.append( "\t" );
+                        b.append( e.getKey() );
+                        Map<?, ?> v = ( Map<?, ?> ) e.getValue();
+                        if ( !v.isEmpty() )
+                        {
+                            for ( Map.Entry<?, ?> e2 : v.entrySet() )
+                            {
+                                b.append( MAPATTR_SEP );
+                                b.append( e2.getKey() );
+                                b.append( "=" );
+                                String v2 = e2.getValue().toString();
+                                if ( v2.contains( "," ) )
+                                {
+                                    b.append( "\"" );
+                                    b.append( v2 );
+                                    b.append( "\"" );
+                                }
+                                else
+                                {
+                                    b.append( v2 );
+                                }
+                            }
+                        }
+                        b.append( ", \\\n" );
+                    }
+                    out.println( b.toString() );
+                }
+                return null;
+            }
+        } );
+        out.println( "# end" );
+    }
+
+
+    /**
+     * dump config in pseudo Properties format.
+     * Note: some values are not Strings (they're List<String>).
+     */
+    private void dump( String prefix, Properties p )
+    {
+        for ( String key : string.keySet() )
+        {
+            p.put( prefix + key, string.get( key ) );
+        }
+
+        for ( String key : list.keySet() )
+        {
+            List<String> list2 = list.get( key );
+            p.put( prefix + key, list2 );
+        }
+
+        for ( String key : map.keySet() )
+        {
+            Map<String, Map<String, String>> map2 = map.get( key );
+            p.put( prefix + key, map2 );
+        }
+
+        for ( String key : property.keySet() )
+        {
+            Properties props = property.get( key );
+            for ( Object k2 : props.keySet() )
+            {
+                p.put( prefix + key + SUBKEY_SEP + k2, props.get( k2 ) );
+            }
+        }
+
+        for ( String key : config.keySet() )
+        {
+            BldConfig config2 = config.get( key );
+            config2.dump( key + SUBKEY_SEP + prefix, p );
+        }
+
+        for ( Object key : unknown.keySet() )
+        {
+            String value = unknown.getProperty( ( String ) key );
+            if ( value.length() > 0 )
+                p.put( prefix + key, value );
+        }
+    }
+
+
+    /**
+     * merges properties into current configuration.
+     * @param p
+     * @throws IOException 
+     */
+    public void merge( Properties p ) throws IOException
+    {
+        if ( p.isEmpty() )
+            return;
+
+        final List<String> strings = Arrays.asList( STRING_KEYS );
+        final List<String> lists = Arrays.asList( LIST_KEYS );
+        final List<String> maps = Arrays.asList( MAP_KEYS );
+
+        List<String> bundleKeys = new ArrayList<String>();
+        List<String> repoKeys = new ArrayList<String>();
+
+        String bundles = p.getProperty( C_BUNDLES );
+        if ( bundles != null )
+        {
+            bundleKeys.addAll( Arrays.asList( bundles.split( LIST_REGEX ) ) );
+            list.put( C_BUNDLES, bundleKeys );
+        }
+
+        String repos = p.getProperty( C_REPOSITORIES );
+        if ( repos != null )
+        {
+            for ( String s : repos.split( LIST_REGEX ) )
+            {
+                repoKeys.add( s.trim() );
+            }
+            list.put( C_REPOSITORIES, repoKeys );
+        }
+
+        List<String> subKeys = new ArrayList<String>();
+        subKeys.addAll( Arrays.asList( PROP_KEYS ) );
+        subKeys.addAll( bundleKeys );
+        subKeys.addAll( repoKeys );
+
+        Map<String, Properties> sub = new TreeMap<String, Properties>();
+
+        for ( Object k : p.keySet() )
+        {
+            String key = ( String ) k;
+            if ( key.equals( C_BUNDLES ) || key.equals( C_REPOSITORIES ) )
+                continue;
+
+            String value = p.getProperty( key );
+            String[] keys = key.split( SUBKEY_SEP, 2 );
+
+            if ( keys.length > 1 )
+            {
+                Properties p2 = sub.get( keys[0] );
+                if ( p2 == null )
+                {
+                    p2 = new Properties();
+                    sub.put( keys[0], p2 );
+                    if ( !subKeys.contains( keys[0] ) )
+                    {
+                        unknown.setProperty( keys[0] + SUBKEY_SEP, "" );
+                    }
+                }
+                p2.setProperty( keys[1], value );
+            }
+            else if ( strings.contains( key ) )
+            {
+                if ( !string.containsKey( key ) )
+                    string.put( key, value );
+            }
+            else if ( lists.contains( key ) )
+            {
+                if ( !list.containsKey( key ) )
+                {
+                    ArrayList<String> list2 = new ArrayList<String>();
+                    for ( String s : value.split( LIST_REGEX ) )
+                    {
+                        if ( s.trim().length() > 0 )
+                        {
+                            list2.add( s.trim() );
+                        }
+                    }
+                    if ( !list2.isEmpty() )
+                    {
+                        list.put( key, list2 );
+                    }
+                }
+            }
+            else if ( maps.contains( key ) )
+            {
+                if ( !map.containsKey( key ) )
+                {
+                    Map<String, Map<String, String>> map2 = new TreeMap<String, Map<String, String>>();
+
+                    for ( String subValue : QuoteUtil.split( value ) )
+                    {
+                        if ( subValue.trim().length() > 0 )
+                        {
+                            String[] split = subValue.split( MAPATTR_REGEX );
+                            Map<String, String> map3 = new TreeMap<String, String>();
+                            for ( int i = 1; i < split.length; ++i )
+                            {
+                                String[] keyVal = split[i].split( ":?=", 2 );
+                                if ( keyVal.length != 2 )
+                                {
+                                    throw new IOException( "attribute missing '=':" + subValue );
+                                }
+                                map3.put( keyVal[0], keyVal[1] );
+                            }
+                            map2.put( split[0], map3 );
+                        }
+                    }
+
+                    map.put( key, map2 );
+                }
+            }
+            else
+            {
+                unknown.setProperty( key, value );
+            }
+        }
+
+        for ( String subKey : sub.keySet() )
+        {
+            Properties props = sub.get( subKey );
+            if ( !props.isEmpty() )
+            {
+                if ( bundleKeys.contains( subKey ) )
+                {
+                    BldConfig config2 = new BldConfig( props );
+                    Properties unkProps = config2.getUnknown();
+
+                    if ( config2.map.containsKey( M_IMPORTS ) )
+                        unkProps.setProperty( M_IMPORTS, "" );
+
+                    if ( config2.map.containsKey( M_REQUIRES ) )
+                        unkProps.setProperty( M_REQUIRES, "" );
+
+                    for ( Object unk : unkProps.keySet() )
+                    {
+                        unknown.setProperty( subKey + SUBKEY_SEP + unk, "" );
+                    }
+                    config.put( subKey, config2 );
+                }
+                else
+                {
+                    property.put( subKey, props );
+                }
+            }
+        }
+    }
+
+
+    @Override
+    public String toString()
+    {
+        return "string: " + string + " list:" + list + " map: " + map + " prop: " + property + " config:" + config;
+    }
+
+}

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/config/BldConverter.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/config/BldConverter.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/config/BldConverter.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/config/BldConverter.java Tue Jul 21 18:51:33 2009
@@ -19,6 +19,7 @@
 
 package org.apache.felix.sigil.config;
 
+
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -47,418 +48,486 @@
 
 import aQute.lib.osgi.Constants;
 
-public class BldConverter {
-	private static final String classpathFormat = "<classpathentry kind=\"%s\" path=\"%s\"/>";
-	private BldConfig config;
-	private Properties packageDefaults;
-	private TreeSet<String> packageWildDefaults;
-
-	public BldConverter(BldConfig config) {
-		this.config = config;
-	}
-
-	/**
-	 * converts to an ISigilBundle.
-	 * @param id
-	 * @param bundle
-	 * @return
-	 */
-	public ISigilBundle getBundle(String id, IBldBundle bundle) {
-
-		ISigilBundle sigilBundle = new SigilBundle();
-		IBundleModelElement info = new BundleModelElement();
-		sigilBundle.setBundleInfo(info);
-
-		// exports
-		// FIXME: UI doesn't understand export wildcard packages
-		for (IPackageExport export : bundle.getExports()) {
-			IPackageExport clone = (IPackageExport) export.clone();
-			clone.setParent(null);
-			info.addExport(clone);
-		}
-
-		// imports
-		for (IPackageImport import1 : bundle.getImports()) {
-			IPackageImport clone = (IPackageImport) import1.clone();
-			clone.setParent(null);
-			info.addImport(clone);
-		}
-
-		// requires
-		for (IRequiredBundle require : bundle.getRequires()) {
-			IRequiredBundle clone = (IRequiredBundle) require.clone();
-			clone.setParent(null);
-			info.addRequiredBundle(clone);
-		}
-
-		// fragment
-		IRequiredBundle fragment = bundle.getFragmentHost();
-		if (fragment != null) {
-			info.setFragmentHost(fragment);
-		}
-		
-		// contents
-		for (String pkg : bundle.getContents()) {
-			sigilBundle.addPackage(pkg);
-		}
-		
-		// downloads
-		for (String pkg : bundle.getDownloadContents()) {
-			sigilBundle.addDownloadPackage(pkg);
-		}
-		
-		// sources
-		for (String source : config.getList(null, BldConfig.L_SRC_CONTENTS) ) {
-			sigilBundle.addClasspathEntry(String.format(classpathFormat, "src", source));
-		}
-
-		// libs
-		Map<String, Map<String, String>> libs = bundle.getLibs();
-		
-		for (String path : libs.keySet()) {
-			Map<String, String> attr = libs.get(path);
-			String kind = attr.get(BldAttr.KIND_ATTRIBUTE);
-			String publish = attr.get(BldAttr.PUBLISH_ATTRIBUTE);
-			
-			if (publish != null) {
-        		// FIXME: UI doesn't understand publish=name
-				BldCore.error("Can't convert -libs publish=" + publish);
-				continue;	
-			}
-			
-			if ("classpath".equals(kind)) {
-    			sigilBundle.addClasspathEntry(String.format(classpathFormat, "lib", path));
-			} else {
-				BldCore.error("Can't convert -libs kind=" + kind);
-			}
-		}
-
-		// resources
-		// FIXME: UI doesn't support -resources: path1=path2
-		Map<String, String> resources = bundle.getResources();
-		for (String resource : resources.keySet()) {
-			String fsPath = resources.get(resource);
-			if (!"".equals(fsPath)) {
-    			BldCore.error("FIXME: can't convert resource: " + resource + "=" + fsPath);
-			}
-			sigilBundle.addSourcePath(new Path(resource));
-		}
-		
-		////////////////////
-		// simple headers
-
-		info.setSymbolicName(bundle.getSymbolicName());
-
-		info.setVersion(Version.parseVersion(bundle.getVersion()));
-
-		String activator = bundle.getActivator();
-		if (activator != null)
-			info.setActivator(activator);
-		
-		Properties headers = config.getProps(id, BldConfig.P_HEADER);
-		String header;
-
-		header = headers.getProperty("CATEGORY");
-		if (header != null)
-			info.setCategory(header);
-
-		header = headers.getProperty(Constants.BUNDLE_CONTACTADDRESS);
-		if (header != null)
-			info.setContactAddress(header);
-
-		header = headers.getProperty(Constants.BUNDLE_COPYRIGHT);
-		if (header != null)
-			info.setCopyright(header);
-
-		header = headers.getProperty(Constants.BUNDLE_DESCRIPTION);
-		if (header != null)
-			info.setDescription(header);
-
-		header = headers.getProperty(Constants.BUNDLE_VENDOR);
-		if (header != null)
-			info.setVendor(header);
-
-		header = headers.getProperty(Constants.BUNDLE_NAME);
-		if (header != null)
-			info.setName(header);
-
-		header = headers.getProperty(Constants.BUNDLE_DOCURL);
-		if (header != null)
-			info.setDocURI(URI.create(header));
-
-		header = headers.getProperty(Constants.BUNDLE_LICENSE);
-		if (header != null)
-			info.setDocURI(URI.create(header));
-
-		return sigilBundle;
-	}
-
-	private VersionRange defaultVersion(VersionRange current, String defaultRange) {
-		if (current.equals(VersionRange.ANY_VERSION) ||
-			current.equals(VersionRange.parseVersionRange(defaultRange))) {
-			return null;
-		}
-		return current;
-	}
-	
-	// FIXME - copied from BldProject
-	private String getDefaultPackageVersion(String name) {
-		if (packageDefaults == null) {
-    		packageDefaults = config.getProps(null, BldConfig.P_PACKAGE_VERSION);
-    		packageWildDefaults = new TreeSet<String>();
-    		
-    		for (Object key : packageDefaults.keySet()) {
-    			String pkg = (String)key;
-    			if (pkg.endsWith("*")) {
-    				packageWildDefaults.add(pkg.substring(0, pkg.length() - 1));
-    			}
-    		}
-		}
-		
-		String version = packageDefaults.getProperty(name);
-		
-		if (version == null) {
-			for (String pkg : packageWildDefaults) {
-				if (name.startsWith(pkg)) {
-					version = packageDefaults.getProperty(pkg + "*");
-					// break; -- don't break, as we want the longest match
-				}
-			}
-		}
-		
-	    return version;
-	}
-	
-	
-	/**
-	 * converts from an ISigilBundle.
-	 * 
-	 * @param id
-	 * @param bundle
-	 */
-	public void setBundle(String id, ISigilBundle bundle) {
-		IBundleModelElement info = bundle.getBundleInfo();
-		String bundleVersion = config.getString(id, BldConfig.S_VERSION);
-
-		// exports
-		Map<String, Map<String, String>> exports = new TreeMap<String, Map<String,String>>();
-		for (IPackageExport export : info.getExports()) {
-			Map<String, String> map2 = new TreeMap<String, String>();
-			String version = export.getVersion().toString();
-			if (!version.equals(bundleVersion))
-				map2.put(BldAttr.VERSION_ATTRIBUTE, version);
-			exports.put(export.getPackageName(), map2);
-		}
-		
-		if (!exports.isEmpty() || !config.getMap(id, BldConfig.M_EXPORTS).isEmpty()) {
-			config.setMap(id, BldConfig.M_EXPORTS, exports);
-		}
-
-		// imports
-		Map<String, Map<String, String>> imports = new TreeMap<String, Map<String,String>>();
-
-		// FIXME: default version logic is wrong here
-		//    if the version to be saved is the same as the default version,
-		//    then we should _remove_ the version from the value being saved,
-		//    since config.getMap() does not apply default versions.
-		for (IPackageImport import1 : info.getImports()) {
-			Map<String, String> map2 = new TreeMap<String, String>();
-			String name = import1.getPackageName();
-			VersionRange versions = defaultVersion(import1.getVersions(), getDefaultPackageVersion(name));
-			
-			boolean isDependency = import1.isDependency();
-			Map<String, String> selfImport = exports.get(name);
-			
-			if (selfImport != null) {
-    			// avoid saving self-import attributes, e.g.
-    			// org.cauldron.newton.example.fractal.engine;resolve=auto;version=1.0.0
-				isDependency = true;
-				
-				if (versions != null) {
-    				String exportVersion = selfImport.get(BldAttr.VERSION_ATTRIBUTE);
-    				if (exportVersion == null)
-    					exportVersion = bundleVersion;
-    				
-    				if (exportVersion.equals(versions.toString())) {
-    					versions = null;
-    				}
-				}
-			}
-
-			if (versions != null) {
-				map2.put(BldAttr.VERSION_ATTRIBUTE, versions.toString());
-			}
-			
-			if (import1.isOptional()) {
-				map2.put(BldAttr.RESOLUTION_ATTRIBUTE, BldAttr.RESOLUTION_OPTIONAL);
-			}
-			
-			String resolve = BldProject.getResolve(import1, isDependency);
-			if (resolve != null)
-				map2.put(BldAttr.RESOLVE_ATTRIBUTE, resolve);
-			
-			imports.put(name, map2);
-		}
-		if (!imports.isEmpty() || !config.getMap(id, BldConfig.M_IMPORTS).isEmpty()) {
-			config.setMap(id, BldConfig.M_IMPORTS, imports);
-		}
-
-		// requires
-		Properties defaultBundles = config.getProps(null, BldConfig.P_BUNDLE_VERSION);
-		Map<String, Map<String, String>> requires = new TreeMap<String, Map<String,String>>();
-		
-		for (IRequiredBundle require : info.getRequiredBundles()) {
-			Map<String, String> map2 = new TreeMap<String, String>();
-			String name = require.getSymbolicName();
-			VersionRange versions = defaultVersion(require.getVersions(), defaultBundles.getProperty(name));
-			if (versions != null)
-    			map2.put(BldAttr.VERSION_ATTRIBUTE, versions.toString());
-			requires.put(name, map2);
-		}
-		if (!requires.isEmpty() || !config.getMap(id, BldConfig.M_REQUIRES).isEmpty()) {
-			config.setMap(id, BldConfig.M_REQUIRES, requires);
-		}
-
-		// fragment
-		Map<String, Map<String, String>> fragments = new TreeMap<String, Map<String,String>>();
-		IRequiredBundle fragment = info.getFragmentHost();
-		if (fragment != null) {
-			Map<String, String> map2 = new TreeMap<String, String>();
-			String name = fragment.getSymbolicName();
-			VersionRange versions = defaultVersion(fragment.getVersions(), defaultBundles.getProperty(name));
-			if (versions != null)
-    			map2.put(BldAttr.VERSION_ATTRIBUTE, versions.toString());
-			fragments.put(name, map2);
-		}
-		if (!fragments.isEmpty() || !config.getMap(id, BldConfig.M_FRAGMENT).isEmpty()) {
-			config.setMap(id, BldConfig.M_FRAGMENT, fragments);
-		}
-	
-		// contents
-		List<String> contents = new ArrayList<String>();
-		for (String pkg : bundle.getPackages()) {
-			contents.add(pkg);
-		}
-		if (!contents.isEmpty() || !config.getList(id, BldConfig.L_CONTENTS).isEmpty()) {
-			config.setList(id, BldConfig.L_CONTENTS, contents);
-		}
-		
-		// dl contents
-		List<String> dlcontents = new ArrayList<String>();
-		for (String pkg : bundle.getDownloadPackages()) {
-			dlcontents.add(pkg);
-		}
-		if (!dlcontents.isEmpty() || !config.getList(id, BldConfig.L_DL_CONTENTS).isEmpty()) {
-			config.setList(id, BldConfig.L_DL_CONTENTS, dlcontents);
-		}
-
-		// libs
-		Map<String, Map<String, String>> libs = new TreeMap<String, Map<String,String>>();
-		List<String> sources = new ArrayList<String>();
-
-		// classpathEntries map to -libs or -sources
-		for (String entry : bundle.getClasspathEntrys()) {
-			// <classpathentry kind="lib" path="lib/dependee.jar"/>
-			// <classpathentry kind="src" path="src"/>
-			final String regex = ".* kind=\"([^\"]+)\" path=\"([^\"]+)\".*";
-			Pattern pattern = Pattern.compile(regex);
-			Matcher matcher = pattern.matcher(entry);
-			if (matcher.matches()) {
-				String kind = matcher.group(1);
-				String path = matcher.group(2);
-				if (kind.equals("lib")) {
-    				Map<String, String> map2 = new TreeMap<String, String>();
-    				map2.put(BldAttr.KIND_ATTRIBUTE, "classpath");
-    				libs.put(path, map2);
-				} else if (kind.equals("src")) {
-					sources.add(path);
-				} else {
-    				BldCore.error("unknown classpathentry kind=" + kind);
-				}
-			} else {
-				BldCore.error("can't match classpathEntry in: " + entry);
-			}
-		}
-
-		if (!libs.isEmpty() || !config.getMap(id, BldConfig.M_LIBS).isEmpty()) {
-			config.setMap(id, BldConfig.M_LIBS, libs);
-		}
-
-		if (!sources.isEmpty() || !config.getList(id, BldConfig.L_SRC_CONTENTS).isEmpty()) {
-			config.setList(id, BldConfig.L_SRC_CONTENTS, sources);
-		}
-
-		// composites
-		ArrayList<String> composites = new ArrayList<String>();
-		for (ISCAComposite composite : bundle.getComposites()) {
-			String path = composite.getLocation().toString();
-			// TODO relativize
-			composites.add(path);
-		}
-		
-		if (!composites.isEmpty() || !config.getList(id, BldConfig.L_COMPOSITES).isEmpty()) {
-			Collections.sort(composites);
-			config.setList(id, BldConfig.L_COMPOSITES, composites);
-		}
-		
-		// resources
-		ArrayList<String> resources = new ArrayList<String>();
-		for (IPath ipath : bundle.getSourcePaths()) {
-			resources.add(ipath.toString());
-		}
-		
-		if (!resources.isEmpty() || !config.getList(id, BldConfig.L_RESOURCES).isEmpty()) {
-    		Collections.sort(resources);
-			config.setList(id, BldConfig.L_RESOURCES, resources);
-		}
-		
-		if (info.getSourceLocation() != null) {
-			BldCore.error("SourceLocation conversion not yet implemented.");
-		}
-
-		if (!info.getLibraryImports().isEmpty()) {
-			BldCore.error("LibraryImports conversion not yet implemented.");
-		}
-
-		////////////////////
-		// simple headers
-
-		List<String> ids = config.getList(null, BldConfig.C_BUNDLES);
-		String idBsn = id != null ? id : ids.get(0);
-		String oldBsn = config.getString(id, BldConfig.S_SYM_NAME);
-		String bsn = info.getSymbolicName();
-		
-		if (!bsn.equals(idBsn) || oldBsn != null)
-			config.setString(id, BldConfig.S_SYM_NAME, bsn);
-
-		String version = info.getVersion().toString();
-		if (version != null)
-			config.setString(id, BldConfig.S_VERSION, version);
-
-		String activator = info.getActivator();
-		if (activator != null)
-    		config.setString(id, BldConfig.S_ACTIVATOR, activator);
-		
-		Properties headers = config.getProps(null, BldConfig.P_HEADER);
-		
-		setHeader(headers, id, "CATEGORY", info.getCategory());
-		setHeader(headers, id, Constants.BUNDLE_CONTACTADDRESS, info.getContactAddress());
-		setHeader(headers, id, Constants.BUNDLE_COPYRIGHT, info.getCopyright());
-		setHeader(headers, id, Constants.BUNDLE_DESCRIPTION, info.getDescription());
-		setHeader(headers, id, Constants.BUNDLE_VENDOR, info.getVendor());
-		setHeader(headers, id, Constants.BUNDLE_NAME, info.getName());
-
-		if (info.getDocURI() != null)
-			config.setProp(id, BldConfig.P_HEADER, Constants.BUNDLE_DOCURL, info.getDocURI().toString());
-
-		if (info.getLicenseURI() != null)
-			config.setProp(id, BldConfig.P_HEADER, Constants.BUNDLE_LICENSE, info.getLicenseURI().toString());
-	}
-	
-	private void setHeader(Properties headers, String id, String key, String value) {
-		if (value == null)
-			value = "";
-		if (!value.equals(headers.getProperty(key, "")))
-    		config.setProp(id, BldConfig.P_HEADER, key, value);
-	}
+
+public class BldConverter
+{
+    private static final String classpathFormat = "<classpathentry kind=\"%s\" path=\"%s\"/>";
+    private BldConfig config;
+    private Properties packageDefaults;
+    private TreeSet<String> packageWildDefaults;
+
+
+    public BldConverter( BldConfig config )
+    {
+        this.config = config;
+    }
+
+
+    /**
+     * converts to an ISigilBundle.
+     * @param id
+     * @param bundle
+     * @return
+     */
+    public ISigilBundle getBundle( String id, IBldBundle bundle )
+    {
+
+        ISigilBundle sigilBundle = new SigilBundle();
+        IBundleModelElement info = new BundleModelElement();
+        sigilBundle.setBundleInfo( info );
+
+        // exports
+        // FIXME: UI doesn't understand export wildcard packages
+        for ( IPackageExport export : bundle.getExports() )
+        {
+            IPackageExport clone = ( IPackageExport ) export.clone();
+            clone.setParent( null );
+            info.addExport( clone );
+        }
+
+        // imports
+        for ( IPackageImport import1 : bundle.getImports() )
+        {
+            IPackageImport clone = ( IPackageImport ) import1.clone();
+            clone.setParent( null );
+            info.addImport( clone );
+        }
+
+        // requires
+        for ( IRequiredBundle require : bundle.getRequires() )
+        {
+            IRequiredBundle clone = ( IRequiredBundle ) require.clone();
+            clone.setParent( null );
+            info.addRequiredBundle( clone );
+        }
+
+        // fragment
+        IRequiredBundle fragment = bundle.getFragmentHost();
+        if ( fragment != null )
+        {
+            info.setFragmentHost( fragment );
+        }
+
+        // contents
+        for ( String pkg : bundle.getContents() )
+        {
+            sigilBundle.addPackage( pkg );
+        }
+
+        // downloads
+        for ( String pkg : bundle.getDownloadContents() )
+        {
+            sigilBundle.addDownloadPackage( pkg );
+        }
+
+        // sources
+        for ( String source : config.getList( null, BldConfig.L_SRC_CONTENTS ) )
+        {
+            sigilBundle.addClasspathEntry( String.format( classpathFormat, "src", source ) );
+        }
+
+        // libs
+        Map<String, Map<String, String>> libs = bundle.getLibs();
+
+        for ( String path : libs.keySet() )
+        {
+            Map<String, String> attr = libs.get( path );
+            String kind = attr.get( BldAttr.KIND_ATTRIBUTE );
+            String publish = attr.get( BldAttr.PUBLISH_ATTRIBUTE );
+
+            if ( publish != null )
+            {
+                // FIXME: UI doesn't understand publish=name
+                BldCore.error( "Can't convert -libs publish=" + publish );
+                continue;
+            }
+
+            if ( "classpath".equals( kind ) )
+            {
+                sigilBundle.addClasspathEntry( String.format( classpathFormat, "lib", path ) );
+            }
+            else
+            {
+                BldCore.error( "Can't convert -libs kind=" + kind );
+            }
+        }
+
+        // resources
+        // FIXME: UI doesn't support -resources: path1=path2
+        Map<String, String> resources = bundle.getResources();
+        for ( String resource : resources.keySet() )
+        {
+            String fsPath = resources.get( resource );
+            if ( !"".equals( fsPath ) )
+            {
+                BldCore.error( "FIXME: can't convert resource: " + resource + "=" + fsPath );
+            }
+            sigilBundle.addSourcePath( new Path( resource ) );
+        }
+
+        ////////////////////
+        // simple headers
+
+        info.setSymbolicName( bundle.getSymbolicName() );
+
+        info.setVersion( Version.parseVersion( bundle.getVersion() ) );
+
+        String activator = bundle.getActivator();
+        if ( activator != null )
+            info.setActivator( activator );
+
+        Properties headers = config.getProps( id, BldConfig.P_HEADER );
+        String header;
+
+        header = headers.getProperty( "CATEGORY" );
+        if ( header != null )
+            info.setCategory( header );
+
+        header = headers.getProperty( Constants.BUNDLE_CONTACTADDRESS );
+        if ( header != null )
+            info.setContactAddress( header );
+
+        header = headers.getProperty( Constants.BUNDLE_COPYRIGHT );
+        if ( header != null )
+            info.setCopyright( header );
+
+        header = headers.getProperty( Constants.BUNDLE_DESCRIPTION );
+        if ( header != null )
+            info.setDescription( header );
+
+        header = headers.getProperty( Constants.BUNDLE_VENDOR );
+        if ( header != null )
+            info.setVendor( header );
+
+        header = headers.getProperty( Constants.BUNDLE_NAME );
+        if ( header != null )
+            info.setName( header );
+
+        header = headers.getProperty( Constants.BUNDLE_DOCURL );
+        if ( header != null )
+            info.setDocURI( URI.create( header ) );
+
+        header = headers.getProperty( Constants.BUNDLE_LICENSE );
+        if ( header != null )
+            info.setDocURI( URI.create( header ) );
+
+        return sigilBundle;
+    }
+
+
+    private VersionRange defaultVersion( VersionRange current, String defaultRange )
+    {
+        if ( current.equals( VersionRange.ANY_VERSION )
+            || current.equals( VersionRange.parseVersionRange( defaultRange ) ) )
+        {
+            return null;
+        }
+        return current;
+    }
+
+
+    // FIXME - copied from BldProject
+    private String getDefaultPackageVersion( String name )
+    {
+        if ( packageDefaults == null )
+        {
+            packageDefaults = config.getProps( null, BldConfig.P_PACKAGE_VERSION );
+            packageWildDefaults = new TreeSet<String>();
+
+            for ( Object key : packageDefaults.keySet() )
+            {
+                String pkg = ( String ) key;
+                if ( pkg.endsWith( "*" ) )
+                {
+                    packageWildDefaults.add( pkg.substring( 0, pkg.length() - 1 ) );
+                }
+            }
+        }
+
+        String version = packageDefaults.getProperty( name );
+
+        if ( version == null )
+        {
+            for ( String pkg : packageWildDefaults )
+            {
+                if ( name.startsWith( pkg ) )
+                {
+                    version = packageDefaults.getProperty( pkg + "*" );
+                    // break; -- don't break, as we want the longest match
+                }
+            }
+        }
+
+        return version;
+    }
+
+
+    /**
+     * converts from an ISigilBundle.
+     * 
+     * @param id
+     * @param bundle
+     */
+    public void setBundle( String id, ISigilBundle bundle )
+    {
+        IBundleModelElement info = bundle.getBundleInfo();
+        String bundleVersion = config.getString( id, BldConfig.S_VERSION );
+
+        // exports
+        Map<String, Map<String, String>> exports = new TreeMap<String, Map<String, String>>();
+        for ( IPackageExport export : info.getExports() )
+        {
+            Map<String, String> map2 = new TreeMap<String, String>();
+            String version = export.getVersion().toString();
+            if ( !version.equals( bundleVersion ) )
+                map2.put( BldAttr.VERSION_ATTRIBUTE, version );
+            exports.put( export.getPackageName(), map2 );
+        }
+
+        if ( !exports.isEmpty() || !config.getMap( id, BldConfig.M_EXPORTS ).isEmpty() )
+        {
+            config.setMap( id, BldConfig.M_EXPORTS, exports );
+        }
+
+        // imports
+        Map<String, Map<String, String>> imports = new TreeMap<String, Map<String, String>>();
+
+        // FIXME: default version logic is wrong here
+        //    if the version to be saved is the same as the default version,
+        //    then we should _remove_ the version from the value being saved,
+        //    since config.getMap() does not apply default versions.
+        for ( IPackageImport import1 : info.getImports() )
+        {
+            Map<String, String> map2 = new TreeMap<String, String>();
+            String name = import1.getPackageName();
+            VersionRange versions = defaultVersion( import1.getVersions(), getDefaultPackageVersion( name ) );
+
+            boolean isDependency = import1.isDependency();
+            Map<String, String> selfImport = exports.get( name );
+
+            if ( selfImport != null )
+            {
+                // avoid saving self-import attributes, e.g.
+                // org.cauldron.newton.example.fractal.engine;resolve=auto;version=1.0.0
+                isDependency = true;
+
+                if ( versions != null )
+                {
+                    String exportVersion = selfImport.get( BldAttr.VERSION_ATTRIBUTE );
+                    if ( exportVersion == null )
+                        exportVersion = bundleVersion;
+
+                    if ( exportVersion.equals( versions.toString() ) )
+                    {
+                        versions = null;
+                    }
+                }
+            }
+
+            if ( versions != null )
+            {
+                map2.put( BldAttr.VERSION_ATTRIBUTE, versions.toString() );
+            }
+
+            if ( import1.isOptional() )
+            {
+                map2.put( BldAttr.RESOLUTION_ATTRIBUTE, BldAttr.RESOLUTION_OPTIONAL );
+            }
+
+            String resolve = BldProject.getResolve( import1, isDependency );
+            if ( resolve != null )
+                map2.put( BldAttr.RESOLVE_ATTRIBUTE, resolve );
+
+            imports.put( name, map2 );
+        }
+        if ( !imports.isEmpty() || !config.getMap( id, BldConfig.M_IMPORTS ).isEmpty() )
+        {
+            config.setMap( id, BldConfig.M_IMPORTS, imports );
+        }
+
+        // requires
+        Properties defaultBundles = config.getProps( null, BldConfig.P_BUNDLE_VERSION );
+        Map<String, Map<String, String>> requires = new TreeMap<String, Map<String, String>>();
+
+        for ( IRequiredBundle require : info.getRequiredBundles() )
+        {
+            Map<String, String> map2 = new TreeMap<String, String>();
+            String name = require.getSymbolicName();
+            VersionRange versions = defaultVersion( require.getVersions(), defaultBundles.getProperty( name ) );
+            if ( versions != null )
+                map2.put( BldAttr.VERSION_ATTRIBUTE, versions.toString() );
+            requires.put( name, map2 );
+        }
+        if ( !requires.isEmpty() || !config.getMap( id, BldConfig.M_REQUIRES ).isEmpty() )
+        {
+            config.setMap( id, BldConfig.M_REQUIRES, requires );
+        }
+
+        // fragment
+        Map<String, Map<String, String>> fragments = new TreeMap<String, Map<String, String>>();
+        IRequiredBundle fragment = info.getFragmentHost();
+        if ( fragment != null )
+        {
+            Map<String, String> map2 = new TreeMap<String, String>();
+            String name = fragment.getSymbolicName();
+            VersionRange versions = defaultVersion( fragment.getVersions(), defaultBundles.getProperty( name ) );
+            if ( versions != null )
+                map2.put( BldAttr.VERSION_ATTRIBUTE, versions.toString() );
+            fragments.put( name, map2 );
+        }
+        if ( !fragments.isEmpty() || !config.getMap( id, BldConfig.M_FRAGMENT ).isEmpty() )
+        {
+            config.setMap( id, BldConfig.M_FRAGMENT, fragments );
+        }
+
+        // contents
+        List<String> contents = new ArrayList<String>();
+        for ( String pkg : bundle.getPackages() )
+        {
+            contents.add( pkg );
+        }
+        if ( !contents.isEmpty() || !config.getList( id, BldConfig.L_CONTENTS ).isEmpty() )
+        {
+            config.setList( id, BldConfig.L_CONTENTS, contents );
+        }
+
+        // dl contents
+        List<String> dlcontents = new ArrayList<String>();
+        for ( String pkg : bundle.getDownloadPackages() )
+        {
+            dlcontents.add( pkg );
+        }
+        if ( !dlcontents.isEmpty() || !config.getList( id, BldConfig.L_DL_CONTENTS ).isEmpty() )
+        {
+            config.setList( id, BldConfig.L_DL_CONTENTS, dlcontents );
+        }
+
+        // libs
+        Map<String, Map<String, String>> libs = new TreeMap<String, Map<String, String>>();
+        List<String> sources = new ArrayList<String>();
+
+        // classpathEntries map to -libs or -sources
+        for ( String entry : bundle.getClasspathEntrys() )
+        {
+            // <classpathentry kind="lib" path="lib/dependee.jar"/>
+            // <classpathentry kind="src" path="src"/>
+            final String regex = ".* kind=\"([^\"]+)\" path=\"([^\"]+)\".*";
+            Pattern pattern = Pattern.compile( regex );
+            Matcher matcher = pattern.matcher( entry );
+            if ( matcher.matches() )
+            {
+                String kind = matcher.group( 1 );
+                String path = matcher.group( 2 );
+                if ( kind.equals( "lib" ) )
+                {
+                    Map<String, String> map2 = new TreeMap<String, String>();
+                    map2.put( BldAttr.KIND_ATTRIBUTE, "classpath" );
+                    libs.put( path, map2 );
+                }
+                else if ( kind.equals( "src" ) )
+                {
+                    sources.add( path );
+                }
+                else
+                {
+                    BldCore.error( "unknown classpathentry kind=" + kind );
+                }
+            }
+            else
+            {
+                BldCore.error( "can't match classpathEntry in: " + entry );
+            }
+        }
+
+        if ( !libs.isEmpty() || !config.getMap( id, BldConfig.M_LIBS ).isEmpty() )
+        {
+            config.setMap( id, BldConfig.M_LIBS, libs );
+        }
+
+        if ( !sources.isEmpty() || !config.getList( id, BldConfig.L_SRC_CONTENTS ).isEmpty() )
+        {
+            config.setList( id, BldConfig.L_SRC_CONTENTS, sources );
+        }
+
+        // composites
+        ArrayList<String> composites = new ArrayList<String>();
+        for ( ISCAComposite composite : bundle.getComposites() )
+        {
+            String path = composite.getLocation().toString();
+            // TODO relativize
+            composites.add( path );
+        }
+
+        if ( !composites.isEmpty() || !config.getList( id, BldConfig.L_COMPOSITES ).isEmpty() )
+        {
+            Collections.sort( composites );
+            config.setList( id, BldConfig.L_COMPOSITES, composites );
+        }
+
+        // resources
+        ArrayList<String> resources = new ArrayList<String>();
+        for ( IPath ipath : bundle.getSourcePaths() )
+        {
+            resources.add( ipath.toString() );
+        }
+
+        if ( !resources.isEmpty() || !config.getList( id, BldConfig.L_RESOURCES ).isEmpty() )
+        {
+            Collections.sort( resources );
+            config.setList( id, BldConfig.L_RESOURCES, resources );
+        }
+
+        if ( info.getSourceLocation() != null )
+        {
+            BldCore.error( "SourceLocation conversion not yet implemented." );
+        }
+
+        if ( !info.getLibraryImports().isEmpty() )
+        {
+            BldCore.error( "LibraryImports conversion not yet implemented." );
+        }
+
+        ////////////////////
+        // simple headers
+
+        List<String> ids = config.getList( null, BldConfig.C_BUNDLES );
+        String idBsn = id != null ? id : ids.get( 0 );
+        String oldBsn = config.getString( id, BldConfig.S_SYM_NAME );
+        String bsn = info.getSymbolicName();
+
+        if ( !bsn.equals( idBsn ) || oldBsn != null )
+            config.setString( id, BldConfig.S_SYM_NAME, bsn );
+
+        String version = info.getVersion().toString();
+        if ( version != null )
+            config.setString( id, BldConfig.S_VERSION, version );
+
+        String activator = info.getActivator();
+        if ( activator != null )
+            config.setString( id, BldConfig.S_ACTIVATOR, activator );
+
+        Properties headers = config.getProps( null, BldConfig.P_HEADER );
+
+        setHeader( headers, id, "CATEGORY", info.getCategory() );
+        setHeader( headers, id, Constants.BUNDLE_CONTACTADDRESS, info.getContactAddress() );
+        setHeader( headers, id, Constants.BUNDLE_COPYRIGHT, info.getCopyright() );
+        setHeader( headers, id, Constants.BUNDLE_DESCRIPTION, info.getDescription() );
+        setHeader( headers, id, Constants.BUNDLE_VENDOR, info.getVendor() );
+        setHeader( headers, id, Constants.BUNDLE_NAME, info.getName() );
+
+        if ( info.getDocURI() != null )
+            config.setProp( id, BldConfig.P_HEADER, Constants.BUNDLE_DOCURL, info.getDocURI().toString() );
+
+        if ( info.getLicenseURI() != null )
+            config.setProp( id, BldConfig.P_HEADER, Constants.BUNDLE_LICENSE, info.getLicenseURI().toString() );
+    }
+
+
+    private void setHeader( Properties headers, String id, String key, String value )
+    {
+        if ( value == null )
+            value = "";
+        if ( !value.equals( headers.getProperty( key, "" ) ) )
+            config.setProp( id, BldConfig.P_HEADER, key, value );
+    }
 }

Modified: felix/trunk/sigil/common/core/src/org/apache/felix/sigil/config/BldFactory.java
URL: http://svn.apache.org/viewvc/felix/trunk/sigil/common/core/src/org/apache/felix/sigil/config/BldFactory.java?rev=796467&r1=796466&r2=796467&view=diff
==============================================================================
--- felix/trunk/sigil/common/core/src/org/apache/felix/sigil/config/BldFactory.java (original)
+++ felix/trunk/sigil/common/core/src/org/apache/felix/sigil/config/BldFactory.java Tue Jul 21 18:51:33 2009
@@ -19,6 +19,7 @@
 
 package org.apache.felix.sigil.config;
 
+
 import java.io.File;
 import java.io.IOException;
 import java.net.URI;
@@ -26,55 +27,70 @@
 import java.util.Map;
 import java.util.Properties;
 
-public class BldFactory {
-	private static Map<URI, BldProject> projects = new HashMap<URI, BldProject>();
-	
-	public static IBldProject getProject(URI uri) throws IOException {
-		return getProject(uri, false);
-	}
-	
-	public static IBldProject getProject(URI uri, boolean ignoreCache) throws IOException {
-		return load(uri, ignoreCache);
-	}
-	
-	public static IRepositoryConfig getConfig(URI uri) throws IOException {
-		return load(uri, false);
-	}
-	
-	/**
-	 * creates a new project file, initialised with defaults.
-	 * @param uri where the file will be saved - used to resolve relative paths.
-	 * @param defaults relative path to defaults file - default ../sigil.properties.
-	 * @return
-	 * @throws IOException
-	 */
-	public static IBldProject newProject(URI uri, String defaults) throws IOException {
-		BldProject project = new BldProject(uri);
-		Properties p = new Properties();
-		if (defaults != null)
-    		p.setProperty(BldConfig.S_DEFAULTS, defaults);
-		project.loadDefaults(p);
-		return project;
-	}
-	
-	private static BldProject load(URI uri, boolean ignoreCache) throws IOException {
-		BldProject p = null; 
-		if (!ignoreCache) {
-			p = projects.get(uri);
-		}
-		
-		if (p == null) {
-			p = new BldProject(uri);
-			p.load();
-			projects.put(uri, p);
-    			
-    		if (Boolean.getBoolean("org.apache.felix.sigil.config.test")) {
-    			File path = new File(uri.getPath() + ".tmp");
-    			System.out.println("XXX: config.test writing: " + path);
-    			p.saveAs(path);
-    		}
-		}
-		return p;
-	}
-	
+
+public class BldFactory
+{
+    private static Map<URI, BldProject> projects = new HashMap<URI, BldProject>();
+
+
+    public static IBldProject getProject( URI uri ) throws IOException
+    {
+        return getProject( uri, false );
+    }
+
+
+    public static IBldProject getProject( URI uri, boolean ignoreCache ) throws IOException
+    {
+        return load( uri, ignoreCache );
+    }
+
+
+    public static IRepositoryConfig getConfig( URI uri ) throws IOException
+    {
+        return load( uri, false );
+    }
+
+
+    /**
+     * creates a new project file, initialised with defaults.
+     * @param uri where the file will be saved - used to resolve relative paths.
+     * @param defaults relative path to defaults file - default ../sigil.properties.
+     * @return
+     * @throws IOException
+     */
+    public static IBldProject newProject( URI uri, String defaults ) throws IOException
+    {
+        BldProject project = new BldProject( uri );
+        Properties p = new Properties();
+        if ( defaults != null )
+            p.setProperty( BldConfig.S_DEFAULTS, defaults );
+        project.loadDefaults( p );
+        return project;
+    }
+
+
+    private static BldProject load( URI uri, boolean ignoreCache ) throws IOException
+    {
+        BldProject p = null;
+        if ( !ignoreCache )
+        {
+            p = projects.get( uri );
+        }
+
+        if ( p == null )
+        {
+            p = new BldProject( uri );
+            p.load();
+            projects.put( uri, p );
+
+            if ( Boolean.getBoolean( "org.apache.felix.sigil.config.test" ) )
+            {
+                File path = new File( uri.getPath() + ".tmp" );
+                System.out.println( "XXX: config.test writing: " + path );
+                p.saveAs( path );
+            }
+        }
+        return p;
+    }
+
 }