parser/html/javasrc/StackNode.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/parser/html/javasrc/StackNode.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,295 @@
     1.4 +/*
     1.5 + * Copyright (c) 2007 Henri Sivonen
     1.6 + * Copyright (c) 2007-2011 Mozilla Foundation
     1.7 + *
     1.8 + * Permission is hereby granted, free of charge, to any person obtaining a 
     1.9 + * copy of this software and associated documentation files (the "Software"), 
    1.10 + * to deal in the Software without restriction, including without limitation 
    1.11 + * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
    1.12 + * and/or sell copies of the Software, and to permit persons to whom the 
    1.13 + * Software is furnished to do so, subject to the following conditions:
    1.14 + *
    1.15 + * The above copyright notice and this permission notice shall be included in 
    1.16 + * all copies or substantial portions of the Software.
    1.17 + *
    1.18 + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
    1.19 + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
    1.20 + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
    1.21 + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
    1.22 + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
    1.23 + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
    1.24 + * DEALINGS IN THE SOFTWARE.
    1.25 + */
    1.26 +
    1.27 +package nu.validator.htmlparser.impl;
    1.28 +
    1.29 +import nu.validator.htmlparser.annotation.Inline;
    1.30 +import nu.validator.htmlparser.annotation.Local;
    1.31 +import nu.validator.htmlparser.annotation.NsUri;
    1.32 +
    1.33 +final class StackNode<T> {
    1.34 +    final int flags;
    1.35 +
    1.36 +    final @Local String name;
    1.37 +
    1.38 +    final @Local String popName;
    1.39 +
    1.40 +    final @NsUri String ns;
    1.41 +
    1.42 +    final T node;
    1.43 +
    1.44 +    // Only used on the list of formatting elements
    1.45 +    HtmlAttributes attributes;
    1.46 +
    1.47 +    private int refcount = 1;
    1.48 +
    1.49 +    // [NOCPP[
    1.50 +
    1.51 +    private final TaintableLocatorImpl locator;
    1.52 +    
    1.53 +    public TaintableLocatorImpl getLocator() {
    1.54 +        return locator;
    1.55 +    }
    1.56 +
    1.57 +    // ]NOCPP]
    1.58 +
    1.59 +    @Inline public int getFlags() {
    1.60 +        return flags;
    1.61 +    }
    1.62 +
    1.63 +    public int getGroup() {
    1.64 +        return flags & ElementName.GROUP_MASK;
    1.65 +    }
    1.66 +
    1.67 +    public boolean isScoping() {
    1.68 +        return (flags & ElementName.SCOPING) != 0;
    1.69 +    }
    1.70 +
    1.71 +    public boolean isSpecial() {
    1.72 +        return (flags & ElementName.SPECIAL) != 0;
    1.73 +    }
    1.74 +
    1.75 +    public boolean isFosterParenting() {
    1.76 +        return (flags & ElementName.FOSTER_PARENTING) != 0;
    1.77 +    }
    1.78 +
    1.79 +    public boolean isHtmlIntegrationPoint() {
    1.80 +        return (flags & ElementName.HTML_INTEGRATION_POINT) != 0;
    1.81 +    }
    1.82 +
    1.83 +    // [NOCPP[
    1.84 +    
    1.85 +    public boolean isOptionalEndTag() {
    1.86 +        return (flags & ElementName.OPTIONAL_END_TAG) != 0;
    1.87 +    }
    1.88 +    
    1.89 +    // ]NOCPP]
    1.90 +
    1.91 +    /**
    1.92 +     * Constructor for copying. This doesn't take another <code>StackNode</code>
    1.93 +     * because in C++ the caller is reponsible for reobtaining the local names
    1.94 +     * from another interner.
    1.95 +     * 
    1.96 +     * @param flags
    1.97 +     * @param ns
    1.98 +     * @param name
    1.99 +     * @param node
   1.100 +     * @param popName
   1.101 +     * @param attributes
   1.102 +     */
   1.103 +    StackNode(int flags, @NsUri String ns, @Local String name, T node,
   1.104 +            @Local String popName, HtmlAttributes attributes
   1.105 +            // [NOCPP[
   1.106 +            , TaintableLocatorImpl locator
   1.107 +    // ]NOCPP]
   1.108 +    ) {
   1.109 +        this.flags = flags;
   1.110 +        this.name = name;
   1.111 +        this.popName = popName;
   1.112 +        this.ns = ns;
   1.113 +        this.node = node;
   1.114 +        this.attributes = attributes;
   1.115 +        this.refcount = 1;
   1.116 +        // [NOCPP[
   1.117 +        this.locator = locator;
   1.118 +        // ]NOCPP]
   1.119 +    }
   1.120 +
   1.121 +    /**
   1.122 +     * Short hand for well-known HTML elements.
   1.123 +     * 
   1.124 +     * @param elementName
   1.125 +     * @param node
   1.126 +     */
   1.127 +    StackNode(ElementName elementName, T node
   1.128 +    // [NOCPP[
   1.129 +            , TaintableLocatorImpl locator
   1.130 +    // ]NOCPP]
   1.131 +    ) {
   1.132 +        this.flags = elementName.getFlags();
   1.133 +        this.name = elementName.name;
   1.134 +        this.popName = elementName.name;
   1.135 +        this.ns = "http://www.w3.org/1999/xhtml";
   1.136 +        this.node = node;
   1.137 +        this.attributes = null;
   1.138 +        this.refcount = 1;
   1.139 +        assert !elementName.isCustom() : "Don't use this constructor for custom elements.";
   1.140 +        // [NOCPP[
   1.141 +        this.locator = locator;
   1.142 +        // ]NOCPP]
   1.143 +    }
   1.144 +
   1.145 +    /**
   1.146 +     * Constructor for HTML formatting elements.
   1.147 +     * 
   1.148 +     * @param elementName
   1.149 +     * @param node
   1.150 +     * @param attributes
   1.151 +     */
   1.152 +    StackNode(ElementName elementName, T node, HtmlAttributes attributes
   1.153 +    // [NOCPP[
   1.154 +            , TaintableLocatorImpl locator
   1.155 +    // ]NOCPP]
   1.156 +    ) {
   1.157 +        this.flags = elementName.getFlags();
   1.158 +        this.name = elementName.name;
   1.159 +        this.popName = elementName.name;
   1.160 +        this.ns = "http://www.w3.org/1999/xhtml";
   1.161 +        this.node = node;
   1.162 +        this.attributes = attributes;
   1.163 +        this.refcount = 1;
   1.164 +        assert !elementName.isCustom() : "Don't use this constructor for custom elements.";
   1.165 +        // [NOCPP[
   1.166 +        this.locator = locator;
   1.167 +        // ]NOCPP]
   1.168 +    }
   1.169 +
   1.170 +    /**
   1.171 +     * The common-case HTML constructor.
   1.172 +     * 
   1.173 +     * @param elementName
   1.174 +     * @param node
   1.175 +     * @param popName
   1.176 +     */
   1.177 +    StackNode(ElementName elementName, T node, @Local String popName
   1.178 +    // [NOCPP[
   1.179 +            , TaintableLocatorImpl locator
   1.180 +    // ]NOCPP]
   1.181 +    ) {
   1.182 +        this.flags = elementName.getFlags();
   1.183 +        this.name = elementName.name;
   1.184 +        this.popName = popName;
   1.185 +        this.ns = "http://www.w3.org/1999/xhtml";
   1.186 +        this.node = node;
   1.187 +        this.attributes = null;
   1.188 +        this.refcount = 1;
   1.189 +        // [NOCPP[
   1.190 +        this.locator = locator;
   1.191 +        // ]NOCPP]
   1.192 +    }
   1.193 +
   1.194 +    /**
   1.195 +     * Constructor for SVG elements. Note that the order of the arguments is
   1.196 +     * what distinguishes this from the HTML constructor. This is ugly, but
   1.197 +     * AFAICT the least disruptive way to make this work with Java's generics
   1.198 +     * and without unnecessary branches. :-(
   1.199 +     * 
   1.200 +     * @param elementName
   1.201 +     * @param popName
   1.202 +     * @param node
   1.203 +     */
   1.204 +    StackNode(ElementName elementName, @Local String popName, T node
   1.205 +    // [NOCPP[
   1.206 +            , TaintableLocatorImpl locator
   1.207 +    // ]NOCPP]
   1.208 +    ) {
   1.209 +        this.flags = prepareSvgFlags(elementName.getFlags());
   1.210 +        this.name = elementName.name;
   1.211 +        this.popName = popName;
   1.212 +        this.ns = "http://www.w3.org/2000/svg";
   1.213 +        this.node = node;
   1.214 +        this.attributes = null;
   1.215 +        this.refcount = 1;
   1.216 +        // [NOCPP[
   1.217 +        this.locator = locator;
   1.218 +        // ]NOCPP]
   1.219 +    }
   1.220 +
   1.221 +    /**
   1.222 +     * Constructor for MathML.
   1.223 +     * 
   1.224 +     * @param elementName
   1.225 +     * @param node
   1.226 +     * @param popName
   1.227 +     * @param markAsIntegrationPoint
   1.228 +     */
   1.229 +    StackNode(ElementName elementName, T node, @Local String popName,
   1.230 +            boolean markAsIntegrationPoint
   1.231 +            // [NOCPP[
   1.232 +            , TaintableLocatorImpl locator
   1.233 +    // ]NOCPP]
   1.234 +    ) {
   1.235 +        this.flags = prepareMathFlags(elementName.getFlags(),
   1.236 +                markAsIntegrationPoint);
   1.237 +        this.name = elementName.name;
   1.238 +        this.popName = popName;
   1.239 +        this.ns = "http://www.w3.org/1998/Math/MathML";
   1.240 +        this.node = node;
   1.241 +        this.attributes = null;
   1.242 +        this.refcount = 1;
   1.243 +        // [NOCPP[
   1.244 +        this.locator = locator;
   1.245 +        // ]NOCPP]
   1.246 +    }
   1.247 +
   1.248 +    private static int prepareSvgFlags(int flags) {
   1.249 +        flags &= ~(ElementName.FOSTER_PARENTING | ElementName.SCOPING
   1.250 +                | ElementName.SPECIAL | ElementName.OPTIONAL_END_TAG);
   1.251 +        if ((flags & ElementName.SCOPING_AS_SVG) != 0) {
   1.252 +            flags |= (ElementName.SCOPING | ElementName.SPECIAL | ElementName.HTML_INTEGRATION_POINT);
   1.253 +        }
   1.254 +        return flags;
   1.255 +    }
   1.256 +
   1.257 +    private static int prepareMathFlags(int flags,
   1.258 +            boolean markAsIntegrationPoint) {
   1.259 +        flags &= ~(ElementName.FOSTER_PARENTING | ElementName.SCOPING
   1.260 +                | ElementName.SPECIAL | ElementName.OPTIONAL_END_TAG);
   1.261 +        if ((flags & ElementName.SCOPING_AS_MATHML) != 0) {
   1.262 +            flags |= (ElementName.SCOPING | ElementName.SPECIAL);
   1.263 +        }
   1.264 +        if (markAsIntegrationPoint) {
   1.265 +            flags |= ElementName.HTML_INTEGRATION_POINT;
   1.266 +        }
   1.267 +        return flags;
   1.268 +    }
   1.269 +
   1.270 +    @SuppressWarnings("unused") private void destructor() {
   1.271 +        Portability.delete(attributes);
   1.272 +    }
   1.273 +
   1.274 +    public void dropAttributes() {
   1.275 +        attributes = null;
   1.276 +    }
   1.277 +
   1.278 +    // [NOCPP[
   1.279 +    /**
   1.280 +     * @see java.lang.Object#toString()
   1.281 +     */
   1.282 +    @Override public @Local String toString() {
   1.283 +        return name;
   1.284 +    }
   1.285 +
   1.286 +    // ]NOCPP]
   1.287 +
   1.288 +    public void retain() {
   1.289 +        refcount++;
   1.290 +    }
   1.291 +
   1.292 +    public void release() {
   1.293 +        refcount--;
   1.294 +        if (refcount == 0) {
   1.295 +            Portability.delete(this);
   1.296 +        }
   1.297 +    }
   1.298 +}

mercurial