001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.xsom;
027:
028: import org.relaxng.datatype.ValidationContext;
029:
030: /**
031: * String with in-scope namespace binding information.
032: *
033: * <p>
034: * In a general case, text (PCDATA/attributes) that appear in XML schema
035: * cannot be correctly interpreted unless you also have in-scope namespace
036: * binding (a case in point is QName.) Therefore, it's convenient to
037: * handle the lexical representation and the in-scope namespace binding
038: * in a pair.
039: *
040: * @author Kohsuke Kawaguchi
041: */
042: public final class XmlString {
043: /**
044: * Textual value. AKA lexical representation.
045: */
046: public final String value;
047:
048: /**
049: * Used to resole in-scope namespace bindings.
050: */
051: public final ValidationContext context;
052:
053: /**
054: * Creates a new {@link XmlString} from a lexical representation and in-scope namespaces.
055: */
056: public XmlString(String value, ValidationContext context) {
057: this .value = value;
058: this .context = context;
059: if (context == null)
060: throw new IllegalArgumentException();
061: }
062:
063: /**
064: * Creates a new {@link XmlString} with empty in-scope namespace bindings.
065: */
066: public XmlString(String value) {
067: this (value, NULL_CONTEXT);
068: }
069:
070: /**
071: * Resolves a namespace prefix to the corresponding namespace URI.
072: *
073: * This method is used for resolving prefixes in the {@link #value}
074: * (such as when {@link #value} represents a QName type.)
075: *
076: * <p>
077: * If the prefix is "" (empty string), the method
078: * returns the default namespace URI.
079: *
080: * <p>
081: * If the prefix is "xml", then the method returns
082: * "http://www.w3.org/XML/1998/namespace",
083: * as defined in the XML Namespaces Recommendation.
084: *
085: * @return
086: * namespace URI of this prefix.
087: * If the specified prefix is not declared,
088: * the implementation returns null.
089: */
090: public final String resolvePrefix(String prefix) {
091: return context.resolveNamespacePrefix(prefix);
092: }
093:
094: public String toString() {
095: return value;
096: }
097:
098: private static final ValidationContext NULL_CONTEXT = new ValidationContext() {
099: public String resolveNamespacePrefix(String s) {
100: if (s.length() == 0)
101: return "";
102: if (s.equals("xml"))
103: return "http://www.w3.org/XML/1998/namespace";
104: return null;
105: }
106:
107: public String getBaseUri() {
108: return null;
109: }
110:
111: public boolean isUnparsedEntity(String s) {
112: return false;
113: }
114:
115: public boolean isNotation(String s) {
116: return false;
117: }
118: };
119: }
|