001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.common;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.List;
025: import java.util.Map;
026:
027: import javax.xml.namespace.QName;
028:
029: import org.xml.sax.InputSource;
030:
031: import org.apache.cxf.tools.common.model.JavaModel;
032: import org.apache.cxf.tools.util.PropertyUtil;
033: import org.apache.cxf.tools.util.URIParserUtil;
034:
035: public class ToolContext {
036:
037: protected JavaModel javaModel;
038: private Map<String, Object> paramMap;
039: private String packageName;
040: private Map<String, String> namespacePackageMap = new HashMap<String, String>();
041: private Map<String, String> excludeNamespacePackageMap = new HashMap<String, String>();
042: private List<InputSource> jaxbBindingFiles = new ArrayList<InputSource>();
043: private List<String> excludePkgList = new ArrayList<String>();
044: private List<String> excludeFileList = new ArrayList<String>();
045:
046: public ToolContext() {
047: }
048:
049: public void loadDefaultNS2Pck(InputStream ins) {
050: try {
051: PropertyUtil properties = new PropertyUtil();
052: properties.load(ins);
053: namespacePackageMap.putAll(properties.getMaps());
054: } catch (IOException e) {
055: e.printStackTrace();
056: }
057: }
058:
059: public void loadDefaultExcludes(InputStream ins) {
060: try {
061: PropertyUtil properties = new PropertyUtil();
062: properties.load(ins);
063: namespacePackageMap.putAll(properties.getMaps());
064: excludeNamespacePackageMap.putAll(properties.getMaps());
065: } catch (IOException e) {
066: e.printStackTrace();
067: }
068: }
069:
070: public JavaModel getJavaModel() {
071: return javaModel;
072: }
073:
074: public void setJavaModel(JavaModel jModel) {
075: this .javaModel = jModel;
076: }
077:
078: public void addParameters(Map<String, Object> map) {
079: for (String key : map.keySet()) {
080: if (!optionSet(key)) {
081: put(key, map.get(key));
082: }
083: }
084: }
085:
086: public void setParameters(Map<String, Object> map) {
087: this .paramMap = map;
088: }
089:
090: public boolean containsKey(String key) {
091: return (paramMap == null) ? false : paramMap.containsKey(key);
092: }
093:
094: public Object get(String key) {
095: return (paramMap == null) ? null : paramMap.get(key);
096: }
097:
098: public Object get(String key, Object defaultValue) {
099: if (!optionSet(key)) {
100: return defaultValue;
101: } else {
102: return get(key);
103: }
104: }
105:
106: public <T> T get(Class<T> key) {
107: return key.cast(get(key.getName()));
108: }
109:
110: public <T> void put(Class<T> key, T value) {
111: put(key.getName(), value);
112: }
113:
114: public boolean getBooleanValue(String key, String defaultValue) {
115: return Boolean.valueOf((String) get(key, defaultValue))
116: .booleanValue();
117: }
118:
119: public void put(String key, Object value) {
120: if (paramMap == null) {
121: paramMap = new HashMap<String, Object>();
122: }
123: paramMap.put(key, value);
124: }
125:
126: public void remove(String key) {
127: if (paramMap == null) {
128: return;
129: }
130: paramMap.remove(key);
131: }
132:
133: public boolean optionSet(String key) {
134: return (get(key) == null) ? false : true;
135: }
136:
137: public boolean isVerbose() {
138: if (get(ToolConstants.CFG_VERBOSE) == null) {
139: return false;
140: } else {
141: return get(ToolConstants.CFG_VERBOSE) == ToolConstants.CFG_VERBOSE;
142: }
143: }
144:
145: // REVIST: Prefer using optionSet, to keep the context clean
146: public boolean validateWSDL() {
147: return get(ToolConstants.CFG_VALIDATE_WSDL) != null;
148:
149: }
150:
151: public void addNamespacePackageMap(String namespace, String pn) {
152: this .namespacePackageMap.put(namespace, pn);
153: }
154:
155: private String mapNamespaceToPackageName(String ns) {
156: return this .namespacePackageMap.get(ns);
157: }
158:
159: public boolean hasNamespace(String ns) {
160: return this .namespacePackageMap.containsKey(ns);
161: }
162:
163: public void addExcludeNamespacePackageMap(String namespace,
164: String pn) {
165: excludeNamespacePackageMap.put(namespace, pn);
166: excludePkgList.add(pn);
167: }
168:
169: public boolean hasExcludeNamespace(String ns) {
170: return excludeNamespacePackageMap.containsKey(ns);
171: }
172:
173: public String getExcludePackageName(String ns) {
174: return this .excludeNamespacePackageMap.get(ns);
175: }
176:
177: public void setPackageName(String pkgName) {
178: this .packageName = pkgName;
179: }
180:
181: public String getPackageName() {
182: return this .packageName;
183: }
184:
185: public String mapPackageName(String ns) {
186: if (hasNamespace(ns)) {
187: return mapNamespaceToPackageName(ns);
188: } else {
189: if (getPackageName() != null) {
190: return getPackageName();
191: }
192: return URIParserUtil.parsePackageName(ns, null);
193:
194: }
195: }
196:
197: public String getCustomizedNS(String ns) {
198: return URIParserUtil.getNamespace(mapPackageName(ns));
199: }
200:
201: public void setJaxbBindingFiles(List<InputSource> bindings) {
202: jaxbBindingFiles = bindings;
203: }
204:
205: public List<InputSource> getJaxbBindingFile() {
206: return this .jaxbBindingFiles;
207: }
208:
209: public boolean isExcludeNamespaceEnabled() {
210: return excludeNamespacePackageMap.size() > 0;
211: }
212:
213: public List<String> getExcludePkgList() {
214: return this .excludePkgList;
215: }
216:
217: public List<String> getExcludeFileList() {
218: return this .excludeFileList;
219: }
220:
221: public QName getQName(String key) {
222: return getQName(key, null);
223: }
224:
225: public QName getQName(String key, String defaultNamespace) {
226: if (optionSet(key)) {
227: String pns = (String) get(key);
228: int pos = pns.indexOf("=");
229: String localname = pns;
230: if (pos != -1) {
231: String ns = pns.substring(0, pos);
232: localname = pns.substring(pos + 1);
233: return new QName(ns, localname);
234: } else {
235: return new QName(defaultNamespace, localname);
236: }
237: }
238: return null;
239: }
240:
241: public Map<String, String> getNamespacePackageMap() {
242: return namespacePackageMap;
243: }
244: }
|