|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.builtins.modules; |
| 42 | + |
| 43 | +import java.util.HashMap; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Map; |
| 46 | + |
| 47 | +import com.oracle.graal.python.builtins.Builtin; |
| 48 | +import com.oracle.graal.python.builtins.CoreFunctions; |
| 49 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
| 50 | +import com.oracle.graal.python.builtins.PythonBuiltins; |
| 51 | +import com.oracle.graal.python.builtins.objects.module.PythonModule; |
| 52 | +import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode; |
| 53 | +import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode; |
| 54 | +import com.oracle.graal.python.runtime.PythonCore; |
| 55 | +import com.oracle.truffle.api.dsl.GenerateNodeFactory; |
| 56 | +import com.oracle.truffle.api.dsl.NodeFactory; |
| 57 | +import com.oracle.truffle.api.dsl.Specialization; |
| 58 | + |
| 59 | +@CoreFunctions(defineModule = "pyexpat") |
| 60 | +public class PyExpatModuleBuiltins extends PythonBuiltins { |
| 61 | + @Override |
| 62 | + protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFactories() { |
| 63 | + return PyExpatModuleBuiltinsFactory.getFactories(); |
| 64 | + } |
| 65 | + |
| 66 | + private static enum ContentModelConstant { |
| 67 | + XML_CQUANT_NONE(0), |
| 68 | + XML_CQUANT_OPT(1), |
| 69 | + XML_CQUANT_PLUS(3), |
| 70 | + XML_CQUANT_REP(2), |
| 71 | + XML_CTYPE_ANY(2), |
| 72 | + XML_CTYPE_CHOICE(5), |
| 73 | + XML_CTYPE_EMPTY(1), |
| 74 | + XML_CTYPE_MIXED(3), |
| 75 | + XML_CTYPE_NAME(4), |
| 76 | + XML_CTYPE_SEQ(6); |
| 77 | + |
| 78 | + private final int number; |
| 79 | + |
| 80 | + private ContentModelConstant(int number) { |
| 81 | + this.number = number; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static enum ErrorConstant { |
| 86 | + XML_ERROR_NO_MEMORY("out of memory"), |
| 87 | + XML_ERROR_SYNTAX("syntax error"), |
| 88 | + XML_ERROR_NO_ELEMENTS("no element found"), |
| 89 | + XML_ERROR_INVALID_TOKEN("not well-formed (invalid token)"), |
| 90 | + XML_ERROR_UNCLOSED_TOKEN("unclosed token"), |
| 91 | + XML_ERROR_PARTIAL_CHAR("partial character"), |
| 92 | + XML_ERROR_TAG_MISMATCH("mismatched tag"), |
| 93 | + XML_ERROR_DUPLICATE_ATTRIBUTE("duplicate attribute"), |
| 94 | + XML_ERROR_JUNK_AFTER_DOC_ELEMENT("junk after document element"), |
| 95 | + XML_ERROR_PARAM_ENTITY_REF("illegal parameter entity reference"), |
| 96 | + XML_ERROR_UNDEFINED_ENTITY("undefined entity"), |
| 97 | + XML_ERROR_RECURSIVE_ENTITY_REF("recursive entity reference"), |
| 98 | + XML_ERROR_ASYNC_ENTITY("asynchronous entity"), |
| 99 | + XML_ERROR_BAD_CHAR_REF("reference to invalid character number"), |
| 100 | + XML_ERROR_BINARY_ENTITY_REF("reference to binary entity"), |
| 101 | + XML_ERROR_ATTRIBUTE_EXTERNAL_ENTITY_REF("reference to external entity in attribute"), |
| 102 | + XML_ERROR_MISPLACED_XML_PI("XML or text declaration not at start of entity"), |
| 103 | + XML_ERROR_UNKNOWN_ENCODING("unknown encoding"), |
| 104 | + XML_ERROR_INCORRECT_ENCODING("encoding specified in XML declaration is incorrect"), |
| 105 | + XML_ERROR_UNCLOSED_CDATA_SECTION("unclosed CDATA section"), |
| 106 | + XML_ERROR_EXTERNAL_ENTITY_HANDLING("error in processing external entity reference"), |
| 107 | + XML_ERROR_NOT_STANDALONE("document is not standalone"), |
| 108 | + XML_ERROR_UNEXPECTED_STATE("unexpected parser state - please send a bug report"), |
| 109 | + XML_ERROR_ENTITY_DECLARED_IN_PE("entity declared in parameter entity"), |
| 110 | + XML_ERROR_FEATURE_REQUIRES_XML_DTD("requested feature requires XML_DTD support in Expat"), |
| 111 | + XML_ERROR_CANT_CHANGE_FEATURE_ONCE_PARSING("cannot change setting once parsing has begun"), |
| 112 | + XML_ERROR_UNBOUND_PREFIX("unbound prefix"), |
| 113 | + XML_ERROR_UNDECLARING_PREFIX("must not undeclare prefix"), |
| 114 | + XML_ERROR_INCOMPLETE_PE("incomplete markup in parameter entity"), |
| 115 | + XML_ERROR_XML_DECL("XML declaration not well-formed"), |
| 116 | + XML_ERROR_TEXT_DECL("text declaration not well-formed"), |
| 117 | + XML_ERROR_PUBLICID("illegal character(s) in public id"), |
| 118 | + XML_ERROR_SUSPENDED("parser suspended"), |
| 119 | + XML_ERROR_NOT_SUSPENDED("parser not suspended"), |
| 120 | + XML_ERROR_ABORTED("parsing aborted"), |
| 121 | + XML_ERROR_FINISHED("parsing finished"), |
| 122 | + XML_ERROR_SUSPEND_PE("cannot suspend in external parameter entity"); |
| 123 | + |
| 124 | + private final String message; |
| 125 | + |
| 126 | + private ErrorConstant(String message) { |
| 127 | + this.message = message; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void initialize(PythonCore core) { |
| 133 | + super.initialize(core); |
| 134 | + PythonModule model = core.factory().createPythonModule("pyexpat.model"); |
| 135 | + for (ContentModelConstant v : ContentModelConstant.values()) { |
| 136 | + model.setAttribute(v.name(), v.number); |
| 137 | + } |
| 138 | + builtinConstants.put("model", model); |
| 139 | + |
| 140 | + PythonModule errors = core.factory().createPythonModule("pyexpat.errors"); |
| 141 | + Map<String, Integer> codes = new HashMap<>(); |
| 142 | + Map<Integer, String> messages = new HashMap<>(); |
| 143 | + for (ErrorConstant c : ErrorConstant.values()) { |
| 144 | + errors.setAttribute(c.name(), c.message); |
| 145 | + codes.put(c.message, c.ordinal() + 1); |
| 146 | + messages.put(c.ordinal() + 1, c.message); |
| 147 | + } |
| 148 | + errors.setAttribute("messages", core.factory().createDict(messages)); |
| 149 | + errors.setAttribute("codes", core.factory().createDict(codes)); |
| 150 | + builtinConstants.put("errors", errors); |
| 151 | + } |
| 152 | + |
| 153 | + @Builtin(name = "ParserCreate", keywordArguments = {"encoding", "namespace_separator", "intern"}, doc = "Return a new XML parser object.") |
| 154 | + @GenerateNodeFactory |
| 155 | + static abstract class ParserCreateNode extends PythonTernaryBuiltinNode { |
| 156 | + @SuppressWarnings("unused") |
| 157 | + @Specialization |
| 158 | + Object fail(Object encoding, Object namespace_separator, Object intern) { |
| 159 | + throw raise(PythonBuiltinClassType.NotImplementedError, "XML pyexpat parser is not implemented"); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments