<commit_before>/* -*- 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):
 *   <NAME>
 *   <NAME>
 *   <NAME>
 *   <NAME> <<EMAIL>>
 *
 * 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.*;
import org.mozilla.javascript.xml.*;
import java.util.ArrayList;

class XMLList extends XMLObjectImpl implements Function {
    static final long serialVersionUID = -4543618751670781135L;

    private XmlNode.InternalList _annos;
    private XMLObjectImpl targetObject = null;
    private XmlNode.QName targetProperty = null;

    XMLList(XMLLibImpl lib, Scriptable scope, XMLObject prototype) {
        super(lib, scope, prototype);
        _annos = new XmlNode.InternalList();
    }

    /* TODO Will probably end up unnecessary as we move things around */
    XmlNode.InternalList getNodeList() {
        return _annos;
    }

    //    TODO    Should be XMLObjectImpl, XMLName?
    void setTargets(XMLObjectImpl object, XmlNode.QName property) {
        targetObject = object;
        targetProperty = property;
    }

    /* TODO: original author marked this as deprecated */
    private XML getXmlFromAnnotation(int index) {
        return getXML(_annos, index);
    }

    @Override
    XML getXML() {
        if (length() == 1) return getXmlFromAnnotation(0);
        return null;
    }

    private void internalRemoveFromList(int index) {
        _annos.remove(index);
    }

    void replace(int index, XML xml) {
        if (index < length()) {
            XmlNode.InternalList newAnnoList = new XmlNode.InternalList();
            newAnnoList.add(_annos, 0, index);
            newAnnoList.add(xml);
            newAnnoList.add(_annos, index+1, length());
            _annos = newAnnoList;
        }
    }

    private void insert(int index, XML xml) {
        if (index < length()) {
            XmlNode.InternalList newAnnoList = new XmlNode.InternalList();
            newAnnoList.add(_annos, 0, index);
            newAnnoList.add(xml);
            newAnnoList.add(_annos, index, length());
            _annos = newAnnoList;
        }
    }

    //
    //
    //  methods overriding ScriptableObject
    //
    //

    @Override
    public String getClassName() {
        return "XMLList";
    }

    //
    //
    //  methods overriding IdScriptableObject
    //
    //

    @Override
    public Object get(int index, Scriptable start) {
        //Log("get index: " + index);

        if (index >= 0 && index < length()) {
            return getXmlFromAnnotation(index);
        } else {
            return Scriptable.NOT_FOUND;
        }
    }

    @Override
    boolean hasXMLProperty(XMLName xmlName) {
        boolean result = false;

        // Has now should return true if the property would have results > 0 or
        // if it's a method name
        String name = xmlName.localName();
        if ((getPropertyList(xmlName).length() > 0) ||
            (getMethod(name) != NOT_FOUND)) {
            result = true;
        }

        return result;
    }

    @Override
    public boolean has(int index, Scriptable start) {
        return 0 <= index && index < length();
    }

    @Override
    void putXMLProperty(XMLName xmlName, Object value) {
        //Log("put property: " + name);

        // Special-case checks for undefined and null
        if (value == null) {
            value = "null";
        } else if (value instanceof Undefined) {
            value = "undefined";
        }

        if (length() > 1) {
            throw ScriptRuntime.typeError(
               "Assignment to lists with more than one item is not supported");
        } else if (length() == 0) {
            // Secret sauce for super-expandos.
            // We set an element here, and then add ourselves to our target.
            if (targetObject != null && targetProperty != null &&
                targetProperty.getLocalName() != null)
            {
                // Add an empty element with our targetProperty name and 
                // then set it.
                XML xmlValue = newTextElementXML(null, targetProperty, null);
                addToList(xmlValue);

                if(xmlName.isAttributeName()) {<commit_msg>Fix  Bug 535977 -  Asignment problems are reported as DomExceptions "NAMESPACE_ERR" Patch from <NAME><commit_after>/* -*- 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):
 *   <NAME>
 *   <NAME>
 *   <NAME>
 *   <NAME> <<EMAIL>>
 *
 * 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.*;
import org.mozilla.javascript.xml.*;
import java.util.ArrayList;

class XMLList extends XMLObjectImpl implements Function {
    static final long serialVersionUID = -4543618751670781135L;

    private XmlNode.InternalList _annos;
    private XMLObjectImpl targetObject = null;
    private XmlNode.QName targetProperty = null;

    XMLList(XMLLibImpl lib, Scriptable scope, XMLObject prototype) {
        super(lib, scope, prototype);
        _annos = new XmlNode.InternalList();
    }

    /* TODO Will probably end up unnecessary as we move things around */
    XmlNode.InternalList getNodeList() {
        return _annos;
    }

    //    TODO    Should be XMLObjectImpl, XMLName?
    void setTargets(XMLObjectImpl object, XmlNode.QName property) {
        targetObject = object;
        targetProperty = property;
    }

    /* TODO: original author marked this as deprecated */
    private XML getXmlFromAnnotation(int index) {
        return getXML(_annos, index);
    }

    @Override
    XML getXML() {
        if (length() == 1) return getXmlFromAnnotation(0);
        return null;
    }

    private void internalRemoveFromList(int index) {
        _annos.remove(index);
    }

    void replace(int index, XML xml) {
        if (index < length()) {
            XmlNode.InternalList newAnnoList = new XmlNode.InternalList();
            newAnnoList.add(_annos, 0, index);
            newAnnoList.add(xml);
            newAnnoList.add(_annos, index+1, length());
            _annos = newAnnoList;
        }
    }

    private void insert(int index, XML xml) {
        if (index < length()) {
            XmlNode.InternalList newAnnoList = new XmlNode.InternalList();
            newAnnoList.add(_annos, 0, index);
            newAnnoList.add(xml);
            newAnnoList.add(_annos, index, length());
            _annos = newAnnoList;
        }
    }

    //
    //
    //  methods overriding ScriptableObject
    //
    //

    @Override
    public String getClassName() {
        return "XMLList";
    }

    //
    //
    //  methods overriding IdScriptableObject
    //
    //

    @Override
    public Object get(int index, Scriptable start) {
        //Log("get index: " + index);

        if (index >= 0 && index < length()) {
            return getXmlFromAnnotation(index);
        } else {
            return Scriptable.NOT_FOUND;
        }
    }

    @Override
    boolean hasXMLProperty(XMLName xmlName) {
        boolean result = false;

        // Has now should return true if the property would have results > 0 or
        // if it's a method name
        String name = xmlName.localName();
        if ((getPropertyList(xmlName).length() > 0) ||
            (getMethod(name) != NOT_FOUND)) {
            result = true;
        }

        return result;
    }

    @Override
    public boolean has(int index, Scriptable start) {
        return 0 <= index && index < length();
    }

    @Override
    void putXMLProperty(XMLName xmlName, Object value) {
        //Log("put property: " + name);

        // Special-case checks for undefined and null
        if (value == null) {
            value = "null";
        } else if (value instanceof Undefined) {
            value = "undefined";
        }

        if (length() > 1) {
            throw ScriptRuntime.typeError(
               "Assignment to lists with more than one item is not supported");
        } else if (length() == 0) {
            // Secret sauce for super-expandos.
            // We set an element here, and then add ourselves to our target.
            if (targetObject != null && targetProperty != null &&
                targetProperty.getLocalName() != null &&
                targetProperty.getLocalName().length() > 0)
            {
                // Add an empty element with our targetProperty name and 
                // then set it.
                XML xmlValue = newTextElementXML(null, targetProperty, null);
                addToList(xmlValue);

                if(xmlName.isAttributeName()) {
{'fraction_non_alphanumeric': 0.09439171699741156, 'fraction_numerical': 0.009577221742881795, 'mean_word_length': 2.3051040775591676, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 26}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6885088', 'n_tokens_mistral': 3270, 'n_tokens_neox': 3075, 'n_words': 1144}