1/* 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.xml.internal.xsom.util; 27 28import java.util.Locale; 29import java.util.ResourceBundle; 30 31import com.sun.xml.internal.xsom.XSAnnotation; 32import com.sun.xml.internal.xsom.XSAttGroupDecl; 33import com.sun.xml.internal.xsom.XSAttributeDecl; 34import com.sun.xml.internal.xsom.XSAttributeUse; 35import com.sun.xml.internal.xsom.XSComplexType; 36import com.sun.xml.internal.xsom.XSComponent; 37import com.sun.xml.internal.xsom.XSContentType; 38import com.sun.xml.internal.xsom.XSElementDecl; 39import com.sun.xml.internal.xsom.XSFacet; 40import com.sun.xml.internal.xsom.XSModelGroup; 41import com.sun.xml.internal.xsom.XSModelGroupDecl; 42import com.sun.xml.internal.xsom.XSNotation; 43import com.sun.xml.internal.xsom.XSParticle; 44import com.sun.xml.internal.xsom.XSSchema; 45import com.sun.xml.internal.xsom.XSSimpleType; 46import com.sun.xml.internal.xsom.XSWildcard; 47import com.sun.xml.internal.xsom.XSIdentityConstraint; 48import com.sun.xml.internal.xsom.XSXPath; 49import com.sun.xml.internal.xsom.visitor.XSFunction; 50 51/** 52 * Gets the human-readable name of a schema component. 53 * 54 * <p> 55 * This is a function object that returns {@link String}. 56 * 57 * @author 58 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 59 */ 60public class NameGetter implements XSFunction<String> { 61 /** 62 * Initializes a NameGetter so that it will return 63 * messages in the specified locale. 64 */ 65 public NameGetter( Locale _locale ) { 66 this.locale = _locale; 67 } 68 69 private final Locale locale; 70 71 /** 72 * An instance that gets names in the default locale. 73 * This instance is provided just for convenience. 74 */ 75 public final static XSFunction theInstance = new NameGetter(null); 76 77 /** 78 * Gets the name of the specified component in the default locale. 79 * This method is just a wrapper. 80 */ 81 public static String get( XSComponent comp ) { 82 return (String)comp.apply(theInstance); 83 } 84 85 86 public String annotation(XSAnnotation ann) { 87 return localize("annotation"); 88 } 89 90 public String attGroupDecl(XSAttGroupDecl decl) { 91 return localize("attGroupDecl"); 92 } 93 94 public String attributeUse(XSAttributeUse use) { 95 return localize("attributeUse"); 96 } 97 98 public String attributeDecl(XSAttributeDecl decl) { 99 return localize("attributeDecl"); 100 } 101 102 public String complexType(XSComplexType type) { 103 return localize("complexType"); 104 } 105 106 public String schema(XSSchema schema) { 107 return localize("schema"); 108 } 109 110 public String facet(XSFacet facet) { 111 return localize("facet"); 112 } 113 114 public String simpleType(XSSimpleType simpleType) { 115 return localize("simpleType"); 116 } 117 118 public String particle(XSParticle particle) { 119 return localize("particle"); 120 } 121 122 public String empty(XSContentType empty) { 123 return localize("empty"); 124 } 125 126 public String wildcard(XSWildcard wc) { 127 return localize("wildcard"); 128 } 129 130 public String modelGroupDecl(XSModelGroupDecl decl) { 131 return localize("modelGroupDecl"); 132 } 133 134 public String modelGroup(XSModelGroup group) { 135 return localize("modelGroup"); 136 } 137 138 public String elementDecl(XSElementDecl decl) { 139 return localize("elementDecl"); 140 } 141 142 public String notation( XSNotation n ) { 143 return localize("notation"); 144 } 145 146 public String identityConstraint(XSIdentityConstraint decl) { 147 return localize("idConstraint"); 148 } 149 150 public String xpath(XSXPath xpath) { 151 return localize("xpath"); 152 } 153 154 private String localize( String key ) { 155 ResourceBundle rb; 156 157 if(locale==null) 158 rb = ResourceBundle.getBundle(NameGetter.class.getName()); 159 else 160 rb = ResourceBundle.getBundle(NameGetter.class.getName(),locale); 161 162 return rb.getString(key); 163 } 164} 165