Home » xmlbeans-2.5.0-src » org.apache » xmlbeans » [javadoc | source]

    1   /*   Copyright 2004 The Apache Software Foundation
    2    *
    3    *   Licensed under the Apache License, Version 2.0 (the "License");
    4    *   you may not use this file except in compliance with the License.
    5    *   You may obtain a copy of the License at
    6    *
    7    *       http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    *   Unless required by applicable law or agreed to in writing, software
   10    *   distributed under the License is distributed on an "AS IS" BASIS,
   11    *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    *   See the License for the specific language governing permissions and
   13    *  limitations under the License.
   14    */
   15   
   16   package org.apache.xmlbeans;
   17   
   18   import java.util.HashMap;
   19   
   20   /**
   21    * Corresponds to the Saver and XmlOptions.
   22    * <p>
   23    * This class is used to set up a map containing characters to be escaped.
   24    * Characters can be escaped as hex, decimal or as a predefined entity (this
   25    * latter option applies only to the 5 characters defined as predefined entities
   26    * in the XML Spec).
   27    * <p>
   28    * <ul>
   29    * For example:
   30    * <pre>
   31    *      XmlOptionCharEscapeMap escapes = new XmlOptionCharEscapeMap();
   32    *      escapes.addMapping('A', XmlOptionCharEscapeMap.HEXADECIMAL);
   33    *      escapes.addMapping('B', XmlOptionCharEscapeMap.DECIMAL);
   34    *      escapes.addMapping('>', XmlOptionCharEscapeMap.PREDEF_ENTITY);
   35    *
   36    *      XmlOptions opts = new XmlOptions();
   37    *      opts.setSaveSubstituteCharacters(escapes);
   38    *      System.out.println(myXml.xmlText(opts));
   39    *
   40    *      will result in:
   41    *      A being printed as &#x41;
   42    *      B being printed as &#66;
   43    *      > being printed as &gt;
   44    *
   45    * </pre>
   46    */
   47   public class XmlOptionCharEscapeMap
   48   {
   49       public static final int PREDEF_ENTITY = 0;
   50       public static final int DECIMAL       = 1;
   51       public static final int HEXADECIMAL   = 2;
   52   
   53       // map of Character to String which will represent it in the output document
   54       private HashMap _charMap;
   55   
   56       // internal HashMap just for predefined entities
   57       private static final HashMap _predefEntities = new HashMap();
   58       static {
   59           _predefEntities.put(new Character('<'), "&lt;");
   60           _predefEntities.put(new Character('>'), "&gt;");
   61           _predefEntities.put(new Character('&'), "&amp;");
   62           _predefEntities.put(new Character('\''), "&apos;");
   63           _predefEntities.put(new Character('"'), "&quot;");
   64       }
   65   
   66       /**
   67        * Construct a new XmlOptionCharEncoder.
   68        */
   69       public XmlOptionCharEscapeMap()
   70       {
   71           _charMap = new HashMap();
   72       }
   73   
   74       /**
   75        *  @return whether a character encoding exists for this character
   76        */
   77       public boolean containsChar(char ch)
   78       {
   79           return _charMap.containsKey(new Character(ch));
   80       }
   81   
   82       /**
   83        * set up this character to be escaped in output documents
   84        * according to the given mode
   85        */
   86       public void addMapping(char ch, int mode) throws XmlException
   87       {
   88           Character theChar = new Character(ch);
   89           switch(mode)
   90           {
   91               case PREDEF_ENTITY:
   92                   String replString = (String)_predefEntities.get(theChar);
   93                   if ( replString == null )
   94                   {
   95                       throw new XmlException("XmlOptionCharEscapeMap.addMapping(): " +
   96                           "the PREDEF_ENTITY mode can only be used for the following " +
   97                           "characters: <, >, &, \" and '");
   98                   }
   99                   _charMap.put(theChar, replString);
  100                   break;
  101   
  102               case DECIMAL:
  103                   _charMap.put(theChar, "&#" + (int)ch + ";");
  104                   break;
  105   
  106               case HEXADECIMAL:
  107                   String hexCharPoint = Integer.toHexString((int)ch);
  108                   _charMap.put(theChar, "&#x" + hexCharPoint + ";");
  109                   break;
  110   
  111               default:
  112                   throw new XmlException("XmlOptionCharEscapeMap.addMapping(): " +
  113                       "mode must be PREDEF_ENTITY, DECIMAL or HEXADECIMAL");
  114           }
  115       }
  116   
  117       /**
  118        * set up this contiguous set of characters to be escaped in
  119        * output documents according to the given mode
  120        */
  121       public void addMappings(char ch1, char ch2, int mode) throws XmlException
  122       {
  123           if (ch1 > ch2)
  124           {
  125               throw new XmlException("XmlOptionCharEscapeMap.addMappings(): " +
  126                   "ch1 must be <= ch2");
  127           }
  128   
  129           for (char c = ch1; c <= ch2; c++)
  130           {
  131               addMapping(c, mode);
  132           }
  133       }
  134   
  135       /**
  136        * returns the escaped String for the character
  137        */
  138       public String getEscapedString(char ch)
  139       {
  140           return (String)_charMap.get(new Character(ch));
  141       }
  142   }

Home » xmlbeans-2.5.0-src » org.apache » xmlbeans » [javadoc | source]