View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2013-8-10 上午06:12:04
4    */
5   package gboat2.base.core.web.md;
6   
7   import gboat2.base.core.util.ActionUtil;
8   import gboat2.base.core.util.JsonVelocityUtil;
9   import gboat2.base.core.web.BaseActionSupport;
10  
11  import java.net.URL;
12  
13  import net.sf.json.JSONObject;
14  
15  import org.osgi.framework.Bundle;
16  
17  /**
18   * 
19   * 单一文件模式,文件名称为"uriPrefix.md"
20   * @author lysming
21   * @since jdk1.6
22   * @date 2013-8-10
23   *  
24   */
25  
26  public class SingleFileModelHandler extends MetadataHandler implements IFileFilter {
27  	
28  	@Override
29  	public String getFile(String invokeMethod, BaseActionSupport action, Bundle bundle) {
30  		return ActionUtil.getPathPrefix(action.getClass()) + "/metadata/" + action.getUriPrefix() + ".md";
31  	}
32  	
33  	@Override
34  	public JSONObject parseMetadata(JSONObject object, URL url, Bundle bundle, MetadataHandlerChain chain) {
35  		if (object == null) {
36  			String content = JsonVelocityUtil.getMetadataFile(url);
37  			content = JsonVelocityUtil.fillJsonByVelocityfromMdStr(content, getActionParam(chain.getAction()));
38  			
39  			try {
40  				object = JSONObject.fromObject(content);
41  				if (object.containsKey(chain.getInvokeMethod())) {
42  					object = (JSONObject) object.get(chain.getInvokeMethod());
43  				}
44  			} catch (Exception e) {
45  				dealJSONException(e);
46  			}
47  		} else if (object.containsKey(chain.getInvokeMethod())) {
48  			object = (JSONObject) object.get(chain.getInvokeMethod());
49  		}
50  		return chain.doHandler(object, url, bundle);
51  	}
52  	
53  }