View Javadoc

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