001: /* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
002: *
003: * ***** BEGIN LICENSE BLOCK *****
004: * Version: MPL 1.1/GPL 2.0
005: *
006: * The contents of this file are subject to the Mozilla Public License Version
007: * 1.1 (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: * http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the
014: * License.
015: *
016: * The Original Code is Rhino code, released
017: * May 6, 1999.
018: *
019: * The Initial Developer of the Original Code is
020: * Netscape Communications Corporation.
021: * Portions created by the Initial Developer are Copyright (C) 1997-2000
022: * the Initial Developer. All Rights Reserved.
023: *
024: * Contributor(s):
025: * Igor Bukanov
026: * Milen Nankov
027: *
028: * Alternatively, the contents of this file may be used under the terms of
029: * the GNU General Public License Version 2 or later (the "GPL"), in which
030: * case the provisions of the GPL are applicable instead of those above. If
031: * you wish to allow use of your version of this file only under the terms of
032: * the GPL and not to allow others to use your version of this file under the
033: * MPL, indicate your decision by deleting the provisions above and replacing
034: * them with the notice and other provisions required by the GPL. If you do
035: * not delete the provisions above, a recipient may use your version of this
036: * file under either the MPL or the GPL.
037: *
038: * ***** END LICENSE BLOCK ***** */
039:
040: package org.mozilla.javascript.xml.impl.xmlbeans;
041:
042: import org.mozilla.javascript.Context;
043: import org.mozilla.javascript.Kit;
044: import org.mozilla.javascript.Ref;
045: import org.mozilla.javascript.ScriptRuntime;
046: import org.mozilla.javascript.Undefined;
047:
048: class XMLName extends Ref {
049: static final long serialVersionUID = 3832176310755686977L;
050:
051: private String uri;
052: private String localName;
053: private boolean isAttributeName;
054: private boolean isDescendants;
055: private XMLObjectImpl xmlObject;
056:
057: private XMLName(String uri, String localName) {
058: this .uri = uri;
059: this .localName = localName;
060: }
061:
062: static XMLName formStar() {
063: return new XMLName(null, "*");
064: }
065:
066: static XMLName formProperty(String uri, String localName) {
067: return new XMLName(uri, localName);
068: }
069:
070: void initXMLObject(XMLObjectImpl xmlObject) {
071: if (xmlObject == null)
072: throw new IllegalArgumentException();
073: if (this .xmlObject != null)
074: throw new IllegalStateException();
075: this .xmlObject = xmlObject;
076: }
077:
078: String uri() {
079: return uri;
080: }
081:
082: String localName() {
083: return localName;
084: }
085:
086: boolean isAttributeName() {
087: return isAttributeName;
088: }
089:
090: void setAttributeName() {
091: if (isAttributeName)
092: throw new IllegalStateException();
093: isAttributeName = true;
094: }
095:
096: boolean isDescendants() {
097: return isDescendants;
098: }
099:
100: void setIsDescendants() {
101: if (isDescendants)
102: throw new IllegalStateException();
103: isDescendants = true;
104: }
105:
106: public boolean has(Context cx) {
107: if (xmlObject == null) {
108: return false;
109: }
110: return xmlObject.hasXMLProperty(this );
111: }
112:
113: public Object get(Context cx) {
114: if (xmlObject == null) {
115: throw ScriptRuntime.undefReadError(Undefined.instance,
116: toString());
117: }
118: return xmlObject.getXMLProperty(this );
119: }
120:
121: public Object set(Context cx, Object value) {
122: if (xmlObject == null) {
123: throw ScriptRuntime.undefWriteError(Undefined.instance,
124: toString(), value);
125: }
126: // Assignment to descendants causes parse error on bad reference
127: // and this should not be called
128: if (isDescendants)
129: throw Kit.codeBug();
130: xmlObject.putXMLProperty(this , value);
131: return value;
132: }
133:
134: public boolean delete(Context cx) {
135: if (xmlObject == null) {
136: return true;
137: }
138: xmlObject.deleteXMLProperty(this );
139: return !xmlObject.hasXMLProperty(this );
140: }
141:
142: public String toString() {
143: //return qname.localName();
144: StringBuffer buff = new StringBuffer();
145: if (isDescendants)
146: buff.append("..");
147: if (isAttributeName)
148: buff.append('@');
149: if (uri == null) {
150: buff.append('*');
151: if (localName().equals("*")) {
152: return buff.toString();
153: }
154: } else {
155: buff.append('"').append(uri()).append('"');
156: }
157: buff.append(':').append(localName());
158: return buff.toString();
159: }
160:
161: }
|