KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > basic > NameGenerator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.ui.basic;
21
22 import org.netbeans.modules.xml.schema.model.SchemaComponent;
23 import org.netbeans.modules.xml.schema.model.SchemaModel;
24 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
25
26 /**
27  * Utility class for generating names for elements within a schema model.
28  *
29  * @author Nathan Fiedler
30  */

31 public class NameGenerator {
32     /** Default starting value for the uniqueness counter (e.g. 0 or 1). */
33 // private static final int COUNTER_START = 1;
34
/** Prefix for the namespace prefix values (e.g. "ns"). */
35     private static final String JavaDoc PREFIX_PREFIX = "ns"; // NOI18N
36
/** The singleton instance of this class. */
37     private static NameGenerator theInstance;
38
39     /**
40      * Creates a new instance of NameGenerator.
41      */

42     private NameGenerator() {
43     }
44
45     /**
46      * Return the singleton instance of this class.
47      *
48      * @return instance of this class.
49      */

50     public static synchronized NameGenerator getInstance() {
51         if (theInstance == null) {
52             theInstance = new NameGenerator();
53         }
54         return theInstance;
55     }
56
57     /**
58      * Generate a unique namespace prefix for the given model. This is
59      * the same as generateNamespacePrefix(String, SchemaModel, int) with
60      * a value of zero for the counter parameter.
61      *
62      * @param prefix the desired prefix for the namespace prefix;
63      * if null, a default of "ns" will be used.
64      * @param model model in which to find unique prefix.
65      * @return the unique namespace prefix (e.g. "ns0").
66      */

67     public String JavaDoc generateNamespacePrefix(String JavaDoc prefix,
68             SchemaModel model) {
69         // WSDL uses zero for the namespace prefix, so let's do the same.
70
return generateNamespacePrefix(prefix, model, 0);
71     }
72
73     /**
74      * Generate a unique namespace prefix for the given model.
75      *
76      * @param prefix the desired prefix for the namespace prefix;
77      * if null, a default of "ns" will be used.
78      * @param model model in which to find unique prefix.
79      * @param counter minimum number to use as suffix (results in a
80      * prefix such as "ns" plus the value of counter).
81      * @return the unique namespace prefix (e.g. "ns0").
82      */

83     public String JavaDoc generateNamespacePrefix(String JavaDoc prefix,
84             SchemaModel model, int counter) {
85         String JavaDoc prefixStr = prefix == null ? PREFIX_PREFIX : prefix;
86         String JavaDoc generated = prefixStr + counter;
87         while (isPrefixExist(generated, model)) {
88             counter++;
89             generated = prefixStr + counter;
90         }
91         return generated;
92     }
93
94     /**
95      * Determine if the given namespace prefix is used in the model.
96      *
97      * @param prefix namespace prefix to look up.
98      * @param model the model in which to look.
99      * @return true if exists, false otherwise.
100      */

101     public static boolean isPrefixExist(String JavaDoc prefix, SchemaModel model) {
102         return getNamespaceURI(prefix, model) != null ? true : false;
103     }
104
105     /**
106      * Get the prefix for the given namespace, for this given element.
107      *
108      * @param namespace the namespace to lookup.
109      * @param element the element to look at.
110      * @return the prefix, or null if none.
111      */

112     public static String JavaDoc getNamespacePrefix(String JavaDoc namespace,
113             SchemaComponent element) {
114         if (element != null && namespace != null) {
115             return ((AbstractDocumentComponent) element).lookupPrefix(namespace);
116         }
117         return null;
118     }
119
120     /**
121      * Retrieve the namespace for the given prefix, if any.
122      *
123      * @param prefix namespace prefix to look up.
124      * @param model the model in which to look.
125      * @return the namespace for the prefix, or null if none.
126      */

127     public static String JavaDoc getNamespaceURI(String JavaDoc prefix, SchemaModel model) {
128         if (model != null && prefix != null) {
129             return ((AbstractDocumentComponent) model.getSchema()).
130                     lookupNamespaceURI(prefix, true);
131         }
132         return null;
133     }
134 }
135
Popular Tags