Home | History | Annotate | Download | only in dom
      1 /*
      2  *  Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "ExceptionCode.h"
     28 
     29 #include "EventException.h"
     30 #include "RangeException.h"
     31 #include "XMLHttpRequestException.h"
     32 
     33 #if ENABLE(SVG)
     34 #include "SVGException.h"
     35 #endif
     36 
     37 #if ENABLE(XPATH)
     38 #include "XPathException.h"
     39 #endif
     40 
     41 namespace WebCore {
     42 
     43 static const char* const exceptionNames[] = {
     44     "INDEX_SIZE_ERR",
     45     "DOMSTRING_SIZE_ERR",
     46     "HIERARCHY_REQUEST_ERR",
     47     "WRONG_DOCUMENT_ERR",
     48     "INVALID_CHARACTER_ERR",
     49     "NO_DATA_ALLOWED_ERR",
     50     "NO_MODIFICATION_ALLOWED_ERR",
     51     "NOT_FOUND_ERR",
     52     "NOT_SUPPORTED_ERR",
     53     "INUSE_ATTRIBUTE_ERR",
     54     "INVALID_STATE_ERR",
     55     "SYNTAX_ERR",
     56     "INVALID_MODIFICATION_ERR",
     57     "NAMESPACE_ERR",
     58     "INVALID_ACCESS_ERR",
     59     "VALIDATION_ERR",
     60     "TYPE_MISMATCH_ERR",
     61     "SECURITY_ERR",
     62     "NETWORK_ERR",
     63     "ABORT_ERR",
     64     "URL_MISMATCH_ERR",
     65     "QUOTA_EXCEEDED_ERR"
     66 };
     67 
     68 static const char* const exceptionDescriptions[] = {
     69     "Index or size was negative, or greater than the allowed value.",
     70     "The specified range of text did not fit into a DOMString.",
     71     "A Node was inserted somewhere it doesn't belong.",
     72     "A Node was used in a different document than the one that created it (that doesn't support it).",
     73     "An invalid or illegal character was specified, such as in an XML name.",
     74     "Data was specified for a Node which does not support data.",
     75     "An attempt was made to modify an object where modifications are not allowed.",
     76     "An attempt was made to reference a Node in a context where it does not exist.",
     77     "The implementation did not support the requested type of object or operation.",
     78     "An attempt was made to add an attribute that is already in use elsewhere.",
     79     "An attempt was made to use an object that is not, or is no longer, usable.",
     80     "An invalid or illegal string was specified.",
     81     "An attempt was made to modify the type of the underlying object.",
     82     "An attempt was made to create or change an object in a way which is incorrect with regard to namespaces.",
     83     "A parameter or an operation was not supported by the underlying object.",
     84     "A call to a method such as insertBefore or removeChild would make the Node invalid with respect to \"partial validity\", this exception would be raised and the operation would not be done.",
     85     "The type of an object was incompatible with the expected type of the parameter associated to the object.",
     86     "An attempt was made to break through the security policy of the user agent.",
     87     // FIXME: Couldn't find a description in the HTML/DOM specifications for NETWORK_ERR, ABORT_ERR, URL_MISMATCH_ERR, and QUOTA_EXCEEDED_ERR
     88     "A network error occured.",
     89     "The user aborted a request.",
     90     "A worker global scope represented an absolute URL that is not equal to the resulting absolute URL.",
     91     "An attempt was made to add something to storage that exceeded the quota."
     92 };
     93 
     94 static const char* const rangeExceptionNames[] = {
     95     "BAD_BOUNDARYPOINTS_ERR",
     96     "INVALID_NODE_TYPE_ERR"
     97 };
     98 
     99 static const char* const rangeExceptionDescriptions[] = {
    100     "The boundary-points of a Range did not meet specific requirements.",
    101     "The container of an boundary-point of a Range was being set to either a node of an invalid type or a node with an ancestor of an invalid type."
    102 };
    103 
    104 static const char* const eventExceptionNames[] = {
    105     "UNSPECIFIED_EVENT_TYPE_ERR"
    106 };
    107 
    108 static const char* const eventExceptionDescriptions[] = {
    109     "The Event's type was not specified by initializing the event before the method was called."
    110 };
    111 
    112 static const char* const xmlHttpRequestExceptionNames[] = {
    113     "NETWORK_ERR",
    114     "ABORT_ERR"
    115 };
    116 
    117 static const char* const xmlHttpRequestExceptionDescriptions[] = {
    118     "A network error occured in synchronous requests.",
    119     "The user aborted a request in synchronous requests."
    120 };
    121 
    122 #if ENABLE(XPATH)
    123 static const char* const xpathExceptionNames[] = {
    124     "INVALID_EXPRESSION_ERR",
    125     "TYPE_ERR"
    126 };
    127 
    128 static const char* const xpathExceptionDescriptions[] = {
    129     "The expression had a syntax error or otherwise is not a legal expression according to the rules of the specific XPathEvaluator.",
    130     "The expression could not be converted to return the specified type."
    131 };
    132 #endif
    133 
    134 #if ENABLE(SVG)
    135 static const char* const svgExceptionNames[] = {
    136     "SVG_WRONG_TYPE_ERR",
    137     "SVG_INVALID_VALUE_ERR",
    138     "SVG_MATRIX_NOT_INVERTABLE"
    139 };
    140 
    141 static const char* const svgExceptionDescriptions[] = {
    142     "An object of the wrong type was passed to an operation.",
    143     "An invalid value was passed to an operation or assigned to an attribute.",
    144     "An attempt was made to invert a matrix that is not invertible."
    145 };
    146 #endif
    147 
    148 void getExceptionCodeDescription(ExceptionCode ec, ExceptionCodeDescription& description)
    149 {
    150     ASSERT(ec);
    151 
    152     const char* typeName;
    153     int code = ec;
    154     const char* const* nameTable;
    155     const char* const* descriptionTable;
    156     int nameTableSize;
    157     int nameTableOffset;
    158     ExceptionType type;
    159 
    160     if (code >= RangeException::RangeExceptionOffset && code <= RangeException::RangeExceptionMax) {
    161         type = RangeExceptionType;
    162         typeName = "DOM Range";
    163         code -= RangeException::RangeExceptionOffset;
    164         nameTable = rangeExceptionNames;
    165         descriptionTable = rangeExceptionDescriptions;
    166         nameTableSize = sizeof(rangeExceptionNames) / sizeof(rangeExceptionNames[0]);
    167         nameTableOffset = RangeException::BAD_BOUNDARYPOINTS_ERR;
    168     } else if (code >= EventException::EventExceptionOffset && code <= EventException::EventExceptionMax) {
    169         type = EventExceptionType;
    170         typeName = "DOM Events";
    171         code -= EventException::EventExceptionOffset;
    172         nameTable = eventExceptionNames;
    173         descriptionTable = eventExceptionDescriptions;
    174         nameTableSize = sizeof(eventExceptionNames) / sizeof(eventExceptionNames[0]);
    175         nameTableOffset = EventException::UNSPECIFIED_EVENT_TYPE_ERR;
    176     } else if (code >= XMLHttpRequestException::XMLHttpRequestExceptionOffset && code <= XMLHttpRequestException::XMLHttpRequestExceptionMax) {
    177         type = XMLHttpRequestExceptionType;
    178         typeName = "XMLHttpRequest";
    179         code -= XMLHttpRequestException::XMLHttpRequestExceptionOffset;
    180         nameTable = xmlHttpRequestExceptionNames;
    181         descriptionTable = xmlHttpRequestExceptionDescriptions;
    182         nameTableSize = sizeof(xmlHttpRequestExceptionNames) / sizeof(xmlHttpRequestExceptionNames[0]);
    183         // XMLHttpRequest exception codes start with 101 and we don't want 100 empty elements in the name array
    184         nameTableOffset = XMLHttpRequestException::NETWORK_ERR;
    185 #if ENABLE(XPATH)
    186     } else if (code >= XPathException::XPathExceptionOffset && code <= XPathException::XPathExceptionMax) {
    187         type = XPathExceptionType;
    188         typeName = "DOM XPath";
    189         code -= XPathException::XPathExceptionOffset;
    190         nameTable = xpathExceptionNames;
    191         descriptionTable = xpathExceptionDescriptions;
    192         nameTableSize = sizeof(xpathExceptionNames) / sizeof(xpathExceptionNames[0]);
    193         // XPath exception codes start with 51 and we don't want 51 empty elements in the name array
    194         nameTableOffset = XPathException::INVALID_EXPRESSION_ERR;
    195 #endif
    196 #if ENABLE(SVG)
    197     } else if (code >= SVGException::SVGExceptionOffset && code <= SVGException::SVGExceptionMax) {
    198         type = SVGExceptionType;
    199         typeName = "DOM SVG";
    200         code -= SVGException::SVGExceptionOffset;
    201         nameTable = svgExceptionNames;
    202         descriptionTable = svgExceptionDescriptions;
    203         nameTableSize = sizeof(svgExceptionNames) / sizeof(svgExceptionNames[0]);
    204         nameTableOffset = SVGException::SVG_WRONG_TYPE_ERR;
    205 #endif
    206     } else {
    207         type = DOMExceptionType;
    208         typeName = "DOM";
    209         nameTable = exceptionNames;
    210         descriptionTable = exceptionDescriptions;
    211         nameTableSize = sizeof(exceptionNames) / sizeof(exceptionNames[0]);
    212         nameTableOffset = INDEX_SIZE_ERR;
    213     }
    214 
    215     description.typeName = typeName;
    216     description.name = (ec >= nameTableOffset && ec - nameTableOffset < nameTableSize) ? nameTable[ec - nameTableOffset] : 0;
    217     description.description = (ec >= nameTableOffset && ec - nameTableOffset < nameTableSize) ? descriptionTable[ec - nameTableOffset] : 0;
    218     description.code = code;
    219     description.type = type;
    220 
    221     // All exceptions used in the DOM code should have names.
    222     ASSERT(description.name);
    223     ASSERT(description.description);
    224 }
    225 
    226 } // namespace WebCore
    227