View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2013-8-10 上午11:52:36
4    */
5   package gboat2.base.core.web.md;
6   
7   import gboat2.base.bridge.debug.DefaultDebugHook;
8   import gboat2.base.core.util.JsonVelocityUtil;
9   import gboat2.base.core.web.MetadataException;
10  
11  import java.net.URL;
12  
13  import net.sf.json.JSONObject;
14  
15  import org.osgi.framework.Bundle;
16  
17  /**
18   * 
19   * 元数据文件扩展处理
20   * @author lysming
21   * @since jdk1.6
22   * @date 2013-8-10
23   *  
24   */
25  
26  public class ExtendMetadataHandler extends MetadataHandler {
27  	/** 
28  	  * {@inheritDoc}   
29  	  * @see gboat2.base.core.web.md.MetadataHandler#parseMetadata(net.sf.json.JSONObject, java.net.URL, gboat2.base.core.web.md.MetadataHandlerChain) 
30  	  */
31  	@Override
32  	public JSONObject parseMetadata(JSONObject object, URL url, Bundle bundle, MetadataHandlerChain chain) {
33  		if (object != null && object.containsKey("extends")) {
34  			String parentPath = object.getString("extends");
35  			URL parentUrl = DefaultDebugHook.getInstance().findResource(bundle, parentPath.startsWith("/")?parentPath:"/"+parentPath);
36  			if (parentUrl == null) {
37  				throw new MetadataException("can't find parent metadata file : " + parentPath);
38  			} else {
39  				JSONObject parentObject = MetadataSupport.parseMetadata(parentUrl, bundle, chain.clone(chain));
40  				if (parentObject != null) {
41  					object = JsonVelocityUtil.merge(parentObject, object);
42  				}
43  			}
44  		}
45  		return chain.doHandler(object, url, bundle);
46  	}
47  
48  }