001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: package com.sun.xml.xsom.util;
021:
022: import java.util.Locale;
023: import java.util.ResourceBundle;
024:
025: import com.sun.xml.xsom.XSAnnotation;
026: import com.sun.xml.xsom.XSAttGroupDecl;
027: import com.sun.xml.xsom.XSAttributeDecl;
028: import com.sun.xml.xsom.XSAttributeUse;
029: import com.sun.xml.xsom.XSComplexType;
030: import com.sun.xml.xsom.XSComponent;
031: import com.sun.xml.xsom.XSContentType;
032: import com.sun.xml.xsom.XSElementDecl;
033: import com.sun.xml.xsom.XSFacet;
034: import com.sun.xml.xsom.XSModelGroup;
035: import com.sun.xml.xsom.XSModelGroupDecl;
036: import com.sun.xml.xsom.XSNotation;
037: import com.sun.xml.xsom.XSParticle;
038: import com.sun.xml.xsom.XSSchema;
039: import com.sun.xml.xsom.XSSimpleType;
040: import com.sun.xml.xsom.XSWildcard;
041: import com.sun.xml.xsom.XSIdentityConstraint;
042: import com.sun.xml.xsom.XSXPath;
043: import com.sun.xml.xsom.visitor.XSFunction;
044:
045: /**
046: * Gets the human-readable name of a schema component.
047: *
048: * <p>
049: * This is a function object that returns {@link String}.
050: *
051: * @author
052: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
053: */
054: public class NameGetter implements XSFunction<String> {
055: /**
056: * Initializes a NameGetter so that it will return
057: * messages in the specified locale.
058: */
059: public NameGetter(Locale _locale) {
060: this .locale = _locale;
061: }
062:
063: private final Locale locale;
064:
065: /**
066: * An instance that gets names in the default locale.
067: * This instance is provided just for convenience.
068: */
069: public final static XSFunction theInstance = new NameGetter(null);
070:
071: /**
072: * Gets the name of the specified component in the default locale.
073: * This method is just a wrapper.
074: */
075: public static String get(XSComponent comp) {
076: return (String) comp.apply(theInstance);
077: }
078:
079: public String annotation(XSAnnotation ann) {
080: return localize("annotation");
081: }
082:
083: public String attGroupDecl(XSAttGroupDecl decl) {
084: return localize("attGroupDecl");
085: }
086:
087: public String attributeUse(XSAttributeUse use) {
088: return localize("attributeUse");
089: }
090:
091: public String attributeDecl(XSAttributeDecl decl) {
092: return localize("attributeDecl");
093: }
094:
095: public String complexType(XSComplexType type) {
096: return localize("complexType");
097: }
098:
099: public String schema(XSSchema schema) {
100: return localize("schema");
101: }
102:
103: public String facet(XSFacet facet) {
104: return localize("facet");
105: }
106:
107: public String simpleType(XSSimpleType simpleType) {
108: return localize("simpleType");
109: }
110:
111: public String particle(XSParticle particle) {
112: return localize("particle");
113: }
114:
115: public String empty(XSContentType empty) {
116: return localize("empty");
117: }
118:
119: public String wildcard(XSWildcard wc) {
120: return localize("wildcard");
121: }
122:
123: public String modelGroupDecl(XSModelGroupDecl decl) {
124: return localize("modelGroupDecl");
125: }
126:
127: public String modelGroup(XSModelGroup group) {
128: return localize("modelGroup");
129: }
130:
131: public String elementDecl(XSElementDecl decl) {
132: return localize("elementDecl");
133: }
134:
135: public String notation(XSNotation n) {
136: return localize("notation");
137: }
138:
139: public String identityConstraint(XSIdentityConstraint decl) {
140: return localize("idConstraint");
141: }
142:
143: public String xpath(XSXPath xpath) {
144: return localize("xpath");
145: }
146:
147: private String localize(String key) {
148: ResourceBundle rb;
149:
150: if (locale == null)
151: rb = ResourceBundle.getBundle(NameGetter.class.getName());
152: else
153: rb = ResourceBundle.getBundle(NameGetter.class.getName(),
154: locale);
155:
156: return rb.getString(key);
157: }
158: }
|