View Javadoc

1   /*
2    * Copyright 2004-2005 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.context.contexts;
17  
18  import net.sf.morph.Defaults;
19  import net.sf.morph.context.Context;
20  import net.sf.morph.context.ContextException;
21  import net.sf.morph.context.DecoratedContext;
22  import net.sf.morph.transform.Converter;
23  
24  /**
25   * Decorates any context so that it implements
26   * {@link net.sf.morph.lang.DecoratedLanguage}.
27   * 
28   * @author Matt Sgarlata
29   * @since Dec 5, 2004
30   */
31  public class ContextDecorator extends BaseContext implements Context, DecoratedContext {
32  	
33  	private Converter converter;
34  	private Context context;
35  	
36  	public ContextDecorator() {
37  		super();
38  		this.converter = Defaults.createConverter();
39  	}
40  	
41  	public ContextDecorator(Context context) {
42  		this();
43  		this.context = context;
44  	}
45  
46  	protected void checkInitialization() {
47  		
48  	}
49  	
50  	public String[] getPropertyNamesImpl() throws ContextException {
51  		checkInitialization();
52  		return context.getPropertyNames();
53  	}
54  
55  	public Object getImpl(String propertyName) throws Exception {
56  		return getLanguage().get(context, propertyName);
57  	}
58  
59  	public void setImpl(String propertyName, Object propertyValue)
60  		throws ContextException {
61  		getLanguage().set(context, propertyName, propertyValue);
62  	}
63  
64  	/**
65  	 * @return Returns the context.
66  	 */
67  	public Context getContext() {
68  		return context;
69  	}
70  	/**
71  	 * @param context The context to set.
72  	 */
73  	public void setContext(Context context) {
74  		this.context = context;
75  	}
76  	/**
77  	 * @return Returns the converter.
78  	 */
79  	public Converter getConverter() {
80  		if (converter == null) {
81  			setConverter(Defaults.createConverter());
82  		}
83  		return converter;
84  	}
85  	/**
86  	 * @param converter The converter to set.
87  	 */
88  	public void setConverter(Converter converter) {
89  		this.converter = converter;
90  	}
91  	
92  }