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-2006 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.xtest.xmlserializer;
043:
044: import java.io.IOException;
045: import java.io.InputStream;
046: import java.util.ArrayList;
047: import java.util.Enumeration;
048: import java.util.Iterator;
049: import java.util.Properties;
050:
051: import org.netbeans.xtest.xmlserializer.ClassMappingRegistry.ClassElementMapping;
052:
053: public class GlobalMappingRegistry {
054:
055: // private constructor - only static methods can be used from this class
056: private GlobalMappingRegistry() {
057: }
058:
059: // global registry collection
060: private static ArrayList globalRegistry = new ArrayList();
061:
062: // register element <-> class relationship
063: public static boolean registerClassForElementName(
064: String elementName, Class clazz) throws MappingException {
065:
066: if (!XMLSerializable.class.isAssignableFrom(clazz)) {
067: throw new MappingException(
068: "Cannot register class "
069: + clazz
070: + ", because does not implement XMLSerializable interface");
071: }
072:
073: ClassMappingRegistry.ClassElementMapping aRow = new ClassMappingRegistry.ClassElementMapping(
074: elementName, clazz);
075: // check if any of the mapping side is already registered
076: if (aRow.canBeAddedTo(globalRegistry)) {
077: globalRegistry.add(aRow);
078: return true;
079: } else {
080: return false;
081: }
082: }
083:
084: /*
085: public static boolean unregisterRootElement(String elementName) {
086: return false;
087: }
088: */
089:
090: public static String getElementNameForClass(Class clazz) {
091: Iterator i = globalRegistry.iterator();
092: while (i.hasNext()) {
093: ClassElementMapping aRow = (ClassElementMapping) i.next();
094: if (clazz.equals(aRow.getClazz())) {
095: return aRow.getElementName();
096: }
097: }
098: return null;
099: }
100:
101: public static Class getClassForElementName(String elementName) {
102: Iterator i = globalRegistry.iterator();
103: while (i.hasNext()) {
104: ClassMappingRegistry.ClassElementMapping aRow = (ClassMappingRegistry.ClassElementMapping) i
105: .next();
106: if (elementName.equals(aRow.getElementName())) {
107: return aRow.getClazz();
108: }
109: }
110: return null;
111: }
112:
113: public static void clearRegistry() {
114: globalRegistry = new ArrayList();
115: }
116:
117: public static void loadRegistry(String registryResource)
118: throws MappingException {
119: // try to load elements/classes registr
120: if (registryResource != null) {
121: try {
122: InputStream inStream = ClassMappingRegistry.class
123: .getResourceAsStream(registryResource);
124: Properties registry = new Properties();
125: registry.load(inStream);
126: // succesfully loaded - continue
127: Enumeration elements = registry.propertyNames();
128: while (elements.hasMoreElements()) {
129: String element = (String) elements.nextElement();
130: String className = registry.getProperty(element);
131: if (className != null) {
132: try {
133: Class clazz = Class.forName(className);
134: registerClassForElementName(element, clazz);
135: } catch (ClassNotFoundException cnfe) {
136: // class not found - not everything is registered correctly
137: throw new MappingException(
138: "Class not found when registering class "
139: + className
140: + " for element name "
141: + element);
142: }
143: }
144: }
145: } catch (IOException ioe) {
146: // file not found or cannot be opened - not everything is registered correctly
147: throw new MappingException(
148: "IOException caught when loading mapping properties",
149: ioe);
150: }
151: }
152: }
153:
154: public static void loadDefaultClassesForElementsRegistry()
155: throws MappingException {
156: String registryResource = System
157: .getProperty("xtest.xmlserializer.registry.resource");
158: if (registryResource == null) {
159: throw new MappingException(
160: "Cannot load default registry, system property 'xtest.xmlserializer.registry.resource' is not set");
161: }
162: loadRegistry(registryResource);
163: }
164:
165: }
|