All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mozilla.javascript.xmlimpl.XMLName Maven / Gradle / Ivy

The newest version!
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Rhino code, released
 * May 6, 1999.
 *
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 1997-2000
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *   Igor Bukanov
 *   Milen Nankov
 *   David P. Caldwell 
 *
 * Alternatively, the contents of this file may be used under the terms of
 * the GNU General Public License Version 2 or later (the "GPL"), in which
 * case the provisions of the GPL are applicable instead of those above. If
 * you wish to allow use of your version of this file only under the terms of
 * the GPL and not to allow others to use your version of this file under the
 * MPL, indicate your decision by deleting the provisions above and replacing
 * them with the notice and other provisions required by the GPL. If you do
 * not delete the provisions above, a recipient may use your version of this
 * file under either the MPL or the GPL.
 *
 * ***** END LICENSE BLOCK ***** */

package org.mozilla.javascript.xmlimpl;

import org.mozilla.javascript.*;

class XMLName extends Ref {
	static final long serialVersionUID = 3832176310755686977L;
	
	private static boolean isNCNameStartChar(int c) {
		if ((c & ~0x7F) == 0) {
			// Optimize for ASCII and use A..Z < _ < a..z
			if (c >= 'a') {
				return c <= 'z';
			} else if (c >= 'A') {
				if (c <= 'Z') {
					return true;
				}
				return c == '_';
			}
		} else if ((c & ~0x1FFF) == 0) {
			return (0xC0 <= c && c <= 0xD6)
			|| (0xD8 <= c && c <= 0xF6)
			|| (0xF8 <= c && c <= 0x2FF)
			|| (0x370 <= c && c <= 0x37D)
			|| 0x37F <= c;
		}
		return (0x200C <= c && c <= 0x200D)
		|| (0x2070 <= c && c <= 0x218F)
		|| (0x2C00 <= c && c <= 0x2FEF)
		|| (0x3001 <= c && c <= 0xD7FF)
		|| (0xF900 <= c && c <= 0xFDCF)
		|| (0xFDF0 <= c && c <= 0xFFFD)
		|| (0x10000 <= c && c <= 0xEFFFF);
	}
	
	private static boolean isNCNameChar(int c) {
		if ((c & ~0x7F) == 0) {
			// Optimize for ASCII and use - < . < 0..9 < A..Z < _ < a..z
			if (c >= 'a') {
				return c <= 'z';
			} else if (c >= 'A') {
				if (c <= 'Z') {
					return true;
				}
				return c == '_';
			} else if (c >= '0') {
				return c <= '9';
			} else {
				return c == '-' || c == '.';
			}
		} else if ((c & ~0x1FFF) == 0) {
			return isNCNameStartChar(c) || c == 0xB7
				|| (0x300 <= c && c <= 0x36F);
		}
		return isNCNameStartChar(c) || (0x203F <= c && c <= 0x2040);
	}
	
	//	This means "accept" in the parsing sense
	//	See ECMA357 13.1.2.1
	static boolean accept(Object nameObj) {
		String name;
		try {
			name = ScriptRuntime.toString(nameObj);
		} catch (EcmaError ee) {
			if ("TypeError".equals(ee.getName())) {
				return false;
			}
			throw ee;
		}
		
		// See http://w3.org/TR/xml-names11/#NT-NCName
		int length = name.length();
		if (length != 0) {
			if (isNCNameStartChar(name.charAt(0))) {
				for (int i = 1; i != length; ++i) {
					if (!isNCNameChar(name.charAt(i))) {
						return false;
					}
				}
				return true;
			}
		}
		
		return false;
	}
	
	private XmlNode.QName qname;
	private boolean isAttributeName;
	private boolean isDescendants;
	private XMLObjectImpl xmlObject;
	
	private XMLName() {
	}
	
	static XMLName formStar() {
		XMLName rv = new XMLName();
		rv.qname = XmlNode.QName.create(null, null);
		return rv;
	}
	
	/** @deprecated */
	static XMLName formProperty(XmlNode.Namespace namespace, String localName) {
		if (localName != null && localName.equals("*")) localName = null;
		XMLName rv = new XMLName();
		rv.qname = XmlNode.QName.create(namespace, localName);
		return rv;
	}
	
	/** @deprecated */
	static XMLName formProperty(String uri, String localName) {
		return formProperty(XmlNode.Namespace.create(uri), localName);
	}
	
	/** @deprecated */
	static XMLName create(String defaultNamespaceUri, String name) {
		if (name == null)
			throw new IllegalArgumentException();
		
		int l = name.length();
		if (l != 0) {
			char firstChar = name.charAt(0);
			if (firstChar == '*') {
				if (l == 1) {
					return XMLName.formStar();
				}
			} else if (firstChar == '@') {
				XMLName xmlName = XMLName.formProperty("", name.substring(1));
				xmlName.setAttributeName();
				return xmlName;
			}
		}
		
		return XMLName.formProperty(defaultNamespaceUri, name);
	}
	
	static XMLName create(XmlNode.QName qname, boolean attribute, boolean descendants) {
		XMLName rv = new XMLName();
		rv.qname = qname;
		rv.isAttributeName = attribute;
		rv.isDescendants = descendants;
		return rv;
	}
	
	/** @deprecated */
	static XMLName create(XmlNode.QName qname) {
		return create(qname, false, false);
	}
	
	void initXMLObject(XMLObjectImpl xmlObject) {
		if (xmlObject == null) throw new IllegalArgumentException();
		if (this.xmlObject != null) throw new IllegalStateException();
		this.xmlObject = xmlObject;
	}
	
	String uri() {
		if (qname.getNamespace() == null) return null;
		return qname.getNamespace().getUri();
	}
	
	String localName() {
		if (qname.getLocalName() == null) return "*";
		return qname.getLocalName();
	}
	
	private void addDescendantChildren(XMLList list, XML target) {
		XMLName xmlName = this;
		if (target.isElement()) {
			XML[] children = target.getChildren();
			for (int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy