001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.xml.wsdl.bindingsupport.template;
043:
044: import java.io.IOException;
045: import java.io.InputStream;
046: import java.util.Collection;
047: import java.util.HashMap;
048: import java.util.Map;
049:
050: import javax.xml.parsers.ParserConfigurationException;
051:
052: import org.netbeans.modules.xml.wsdl.bindingsupport.spi.ExtensibilityElementTemplateProvider;
053: import org.netbeans.modules.xml.wsdl.bindingsupport.template.localized.LocalizedTemplateGroup;
054: import org.openide.ErrorManager;
055: import org.openide.util.Lookup;
056: import org.xml.sax.SAXException;
057:
058: public class ExtensibilityElementTemplateFactory {
059:
060: private static Map<String, TemplateGroup> templateGroupMap;
061: private static Map<String, ExtensibilityElementTemplateProvider> providerMap;
062:
063: private static Map<TemplateGroup, LocalizedTemplateGroup> localizedTemplateGroupMap;
064:
065: public ExtensibilityElementTemplateFactory() {
066: initialise();
067: localizedTemplateGroupMap = new HashMap<TemplateGroup, LocalizedTemplateGroup>();
068:
069: }
070:
071: private void initialise() {
072: lookupProviders();
073: }
074:
075: public TemplateGroup getExtensibilityElementTemplateGroup(
076: String namespace) {
077: if (templateGroupMap == null || namespace == null)
078: return null;
079: return templateGroupMap.get(namespace);
080: }
081:
082: public Collection<TemplateGroup> getExtensibilityElementTemplateGroups() {
083: if (templateGroupMap == null)
084: return null;
085: return templateGroupMap.values();
086: }
087:
088: public String getLocalizedMessage(String namespace, String str,
089: Object[] objs) {
090: assert namespace != null : "namespace cannot be null";
091: assert str != null : "message key cannot be null";
092:
093: ExtensibilityElementTemplateProvider provider = providerMap
094: .get(namespace);
095: if (provider != null) {
096: return provider.getLocalizedMessage(str, objs);
097: }
098:
099: return null;
100: }
101:
102: public LocalizedTemplateGroup getLocalizedTemplateGroup(
103: TemplateGroup group) {
104: String namespace = group.getNamespace();
105: ExtensibilityElementTemplateProvider provider = providerMap
106: .get(namespace);
107:
108: assert provider != null : "ExtensibilityElementTemplateProvider cannot be null";
109: LocalizedTemplateGroup localTemplateGroup = localizedTemplateGroupMap
110: .get(group);
111: if (localTemplateGroup == null) {
112: localTemplateGroup = new LocalizedTemplateGroup(group,
113: provider);
114: localizedTemplateGroupMap.put(group, localTemplateGroup);
115: }
116:
117: return localTemplateGroup;
118:
119: }
120:
121: private synchronized void lookupProviders() {
122: if (templateGroupMap != null)
123: return;
124:
125: templateGroupMap = new HashMap<String, TemplateGroup>();
126: providerMap = new HashMap<String, ExtensibilityElementTemplateProvider>();
127:
128: Lookup.Result result = Lookup
129: .getDefault()
130: .lookup(
131: new Lookup.Template<ExtensibilityElementTemplateProvider>(
132: ExtensibilityElementTemplateProvider.class));
133:
134: for (Object obj : result.allInstances()) {
135: ExtensibilityElementTemplateProvider provider = (ExtensibilityElementTemplateProvider) obj;
136: InputStream stream = provider.getTemplateInputStream();
137: if (stream != null) {
138: TemplateGroup group;
139: try {
140: group = TemplateGroup.read(stream);
141: templateGroupMap.put(group.getNamespace(), group);
142: providerMap.put(group.getNamespace(), provider);
143: } catch (ParserConfigurationException e) {
144: ErrorManager.getDefault().notify(e);
145: } catch (SAXException e) {
146: ErrorManager.getDefault().notify(e);
147: } catch (IOException e) {
148: ErrorManager.getDefault().notify(e);
149: }
150: }
151: }
152:
153: }
154:
155: }
|