001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: ElemDesc.java 468655 2006-10-28 07:12:06Z minchau $
020     */
021    package org.apache.xml.utils;
022    
023    import java.util.Hashtable;
024    
025    /**
026     * This class is in support of SerializerToHTML, and acts as a sort
027     * of element representative for HTML elements.
028     * @xsl.usage internal
029     */
030    class ElemDesc
031    {
032    
033      /** Table of attributes for the element */
034      Hashtable m_attrs = null;
035    
036      /** Element's flags, describing the role this element plays during
037       * formatting of the document. This is used as a bitvector; more than one flag
038       * may be set at a time, bitwise-ORed together. Mnemonic and bits
039       * have been assigned to the flag values. NOTE: Some bits are
040       * currently assigned multiple mnemonics; it is the caller's
041       * responsibility to disambiguate these if necessary. */
042      int m_flags;
043    
044      /** Defines mnemonic and bit-value for the EMPTY flag */
045      static final int EMPTY = (1 << 1);
046    
047      /** Defines mnemonic and bit-value for the FLOW flag  */
048      static final int FLOW = (1 << 2);
049    
050      /** Defines mnemonic and bit-value for the BLOCK flag          */
051      static final int BLOCK = (1 << 3);
052    
053      /** Defines mnemonic and bit-value for the BLOCKFORM  flag         */
054      static final int BLOCKFORM = (1 << 4);
055    
056      /** Defines mnemonic and bit-value for the BLOCKFORMFIELDSET flag          */
057      static final int BLOCKFORMFIELDSET = (1 << 5);
058    
059      /** Defines mnemonic and bit-value for the CDATA flag         */
060      static final int CDATA = (1 << 6);
061    
062      /** Defines mnemonic and bit-value for the PCDATA flag          */
063      static final int PCDATA = (1 << 7);
064    
065      /** Defines mnemonic and bit-value for the RAW flag         */
066      static final int RAW = (1 << 8);
067    
068      /** Defines mnemonic and bit-value for the INLINE flag          */
069      static final int INLINE = (1 << 9);
070    
071      /** Defines mnemonic and bit-value for the INLINEA flag          */
072      static final int INLINEA = (1 << 10);
073    
074      /** Defines mnemonic and bit-value for the INLINELABEL flag          */
075      static final int INLINELABEL = (1 << 11);
076    
077      /** Defines mnemonic and bit-value for the FONTSTYLE flag          */
078      static final int FONTSTYLE = (1 << 12);
079    
080      /** Defines mnemonic and bit-value for the PHRASE flag          */
081      static final int PHRASE = (1 << 13);
082    
083      /** Defines mnemonic and bit-value for the FORMCTRL flag         */
084      static final int FORMCTRL = (1 << 14);
085    
086      /** Defines mnemonic and bit-value for the SPECIAL flag         */
087      static final int SPECIAL = (1 << 15);
088    
089      /** Defines mnemonic and bit-value for the ASPECIAL flag         */
090      static final int ASPECIAL = (1 << 16);
091    
092      /** Defines mnemonic and bit-value for the HEADMISC flag         */
093      static final int HEADMISC = (1 << 17);
094    
095      /** Defines mnemonic and bit-value for the HEAD flag         */
096      static final int HEAD = (1 << 18);
097    
098      /** Defines mnemonic and bit-value for the LIST flag         */
099      static final int LIST = (1 << 19);
100    
101      /** Defines mnemonic and bit-value for the PREFORMATTED flag         */
102      static final int PREFORMATTED = (1 << 20);
103    
104      /** Defines mnemonic and bit-value for the WHITESPACESENSITIVE flag         */
105      static final int WHITESPACESENSITIVE = (1 << 21);
106    
107      /** Defines mnemonic and bit-value for the ATTRURL flag         */
108      static final int ATTRURL = (1 << 1);
109    
110      /** Defines mnemonic and bit-value for the ATTREMPTY flag         */
111      static final int ATTREMPTY = (1 << 2);
112    
113      /**
114       * Construct an ElementDescription with an initial set of flags.
115       *
116       * @param flags Element flags
117       * @see m_flags
118       */
119      ElemDesc(int flags)
120      {
121        m_flags = flags;
122      }
123    
124      /**
125       * "is (this element described by these flags)".
126       * 
127       * This might more properly be called areFlagsSet(). It accepts an
128       * integer (being used as a bitvector) and checks whether all the 
129       * corresponding bits are set in our internal flags. Note that this
130       * test is performed as a bitwise AND, not an equality test, so a
131       * 0 bit in the input means "don't test", not "must be set false".
132       *
133       * @param flags Vector of flags to compare against this element's flags
134       *
135       * @return true if the flags set in the parameter are also set in the
136       * element's stored flags.
137       * 
138       * @see m_flags
139       * @see isAttrFlagSet
140       */
141      boolean is(int flags)
142      {
143        // int which = (m_flags & flags);
144        return (m_flags & flags) != 0;
145      }
146    
147      /**
148       * Set a new attribute for this element 
149       *
150       *
151       * @param name Attribute name
152       * @param flags Attibute flags
153       */
154      void setAttr(String name, int flags)
155      {
156    
157        if (null == m_attrs)
158          m_attrs = new Hashtable();
159    
160        m_attrs.put(name, new Integer(flags));
161      }
162    
163      /**
164       * Find out if a flag is set in a given attribute of this element 
165       *
166       *
167       * @param name Attribute name
168       * @param flags Flag to check
169       *
170       * @return True if the flag is set in the attribute. Returns false
171       * if the attribute is not found 
172       * @see m_flags
173       */
174      boolean isAttrFlagSet(String name, int flags)
175      {
176    
177        if (null != m_attrs)
178        {
179          Integer _flags = (Integer) m_attrs.get(name);
180    
181          if (null != _flags)
182          {
183            return (_flags.intValue() & flags) != 0;
184          }
185        }
186    
187        return false;
188      }
189    }