FlyweightProcessingInstruction xref

View Javadoc

    1   /*
    2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    3    *
    4    * This software is open source.
    5    * See the bottom of this file for the licence.
    6    */
    7
    8   package org.dom4j.tree;
    9
    10  import java.util.Collections;
    11  import java.util.Map;
    12
    13  import org.dom4j.Element;
    14  import org.dom4j.Node;
    15
    16  /***
    17   * <p>
    18   * <code>FlyweightProcessingInstruction</code> is a Flyweight pattern
    19   * implementation of a singly linked, read-only XML Processing Instruction.
    20   * </p>
    21   *
    22   * <p>
    23   * This node could be shared across documents and elements though it does not
    24   * support the parent relationship.
    25   * </p>
    26   *
    27   * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
    28   * @version $Revision: 1.7 $
    29   */
    30  public class FlyweightProcessingInstruction extends
    31          AbstractProcessingInstruction {
    32      /*** The target of the PI */
    33      protected String target;
    34
    35      /*** The values for the PI as a String */
    36      protected String text;
    37
    38      /*** The values for the PI in name/value pairs */
    39      protected Map values;
    40
    41      /***
    42       * A default constructor for implementors to use.
    43       */
    44      public FlyweightProcessingInstruction() {
    45      }
    46
    47      /***
    48       * <p>
    49       * This will create a new PI with the given target and values
    50       * </p>
    51       *
    52       * @param target
    53       *            is the name of the PI
    54       * @param values
    55       *            is the <code>Map</code> of the values for the PI
    56       */
    57      public FlyweightProcessingInstruction(String target, Map values) {
    58          this.target = target;
    59          this.values = values;
    60          this.text = toString(values);
    61      }
    62
    63      /***
    64       * <p>
    65       * This will create a new PI with the given target and values
    66       * </p>
    67       *
    68       * @param target
    69       *            is the name of the PI
    70       * @param text
    71       *            is the values for the PI as text
    72       */
    73      public FlyweightProcessingInstruction(String target, String text) {
    74          this.target = target;
    75          this.text = text;
    76          this.values = parseValues(text);
    77      }
    78
    79      public String getTarget() {
    80          return target;
    81      }
    82
    83      public void setTarget(String target) {
    84          throw new UnsupportedOperationException("This PI is read-only and "
    85                  + "cannot be modified");
    86      }
    87
    88      public String getText() {
    89          return text;
    90      }
    91
    92      public String getValue(String name) {
    93          String answer = (String) values.get(name);
    94
    95          if (answer == null) {
    96              return "";
    97          }
    98
    99          return answer;
    100     }
    101
    102     public Map getValues() {
    103         return Collections.unmodifiableMap(values);
    104     }
    105
    106     protected Node createXPathResult(Element parent) {
    107         return new DefaultProcessingInstruction(parent, getTarget(), getText());
    108     }
    109 }
    110
    111 /*
    112  * Redistribution and use of this software and associated documentation
    113  * ("Software"), with or without modification, are permitted provided that the
    114  * following conditions are met:
    115  *
    116  * 1. Redistributions of source code must retain copyright statements and
    117  * notices. Redistributions must also contain a copy of this document.
    118  *
    119  * 2. Redistributions in binary form must reproduce the above copyright notice,
    120  * this list of conditions and the following disclaimer in the documentation
    121  * and/or other materials provided with the distribution.
    122  *
    123  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    124  * from this Software without prior written permission of MetaStuff, Ltd. For
    125  * written permission, please contact dom4j-info@metastuff.com.
    126  *
    127  * 4. Products derived from this Software may not be called "DOM4J" nor may
    128  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    129  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    130  *
    131  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    132  *
    133  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    134  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    135  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    136  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    137  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    138  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    139  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    140  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    141  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    142  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    143  * POSSIBILITY OF SUCH DAMAGE.
    144  *
    145  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    146  */