001/*
002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
003 * 
004 * This software is open source. 
005 * See the bottom of this file for the licence.
006 * 
007 * $Id: FlyweightProcessingInstruction.java,v 1.1 2001/06/20 18:59:23 jstrachan Exp $
008 */
009
010package org.dom4j.tree;
011
012import java.util.Collections;
013import java.util.Map;
014
015import org.dom4j.Element;
016import org.dom4j.Node;
017import org.dom4j.ProcessingInstruction;
018
019/** <p><code>FlyweightProcessingInstruction</code> is a Flyweight pattern implementation
020  * of a singly linked, read-only XML Processing Instruction.</p>
021  *
022  * <p>This node could be shared across documents and elements though 
023  * it does not support the parent relationship.</p>
024  *
025  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
026  * @version $Revision: 1.1 $
027  */
028public class FlyweightProcessingInstruction extends AbstractProcessingInstruction {
029
030    /** The target of the PI */
031    protected String target;
032
033    /** The values for the PI as a String */
034    protected String text;
035
036    /** The values for the PI in name/value pairs */
037    protected Map values;
038
039    /** A default constructor for implementors to use.
040      */
041    public FlyweightProcessingInstruction() { 
042    }
043
044    /** <p>This will create a new PI with the given target and values</p>
045      *
046      * @param target is the name of the PI
047      * @param values is the <code>Map</code> of the values for the PI
048      */
049    public FlyweightProcessingInstruction(String target,Map values) {
050        this.target = target;
051        this.values = values;
052        this.text = toString(values);
053    }
054
055    /** <p>This will create a new PI with the given target and values</p>
056      *
057      * @param target is the name of the PI
058      * @param text is the values for the PI as text
059      */
060    public FlyweightProcessingInstruction(String target,String text) {
061        this.target = target;
062        this.text = text;
063        this.values = parseValues(text);
064    }
065
066    public String getTarget() {
067        return target;
068    }
069
070    public void setTarget(String target) {
071        throw new UnsupportedOperationException( "This PI is read-only and cannot be modified" );
072    }
073
074    public String getText() {
075        return text;
076    }
077    
078    public String getValue(String name) {
079        String answer = (String) values.get(name);
080        if (answer == null) {
081            return "";
082        }
083        return answer;
084    }
085    
086    public Map getValues() {
087        return Collections.unmodifiableMap( values );
088    }
089    
090    protected Node createXPathResult(Element parent) {
091        return new DefaultProcessingInstruction( parent, getTarget(), getText() );
092    }
093}
094
095
096
097
098
099
100/*
101 * Redistribution and use of this software and associated documentation
102 * ("Software"), with or without modification, are permitted provided
103 * that the following conditions are met:
104 *
105 * 1. Redistributions of source code must retain copyright
106 *    statements and notices.  Redistributions must also contain a
107 *    copy of this document.
108 *
109 * 2. Redistributions in binary form must reproduce the
110 *    above copyright notice, this list of conditions and the
111 *    following disclaimer in the documentation and/or other
112 *    materials provided with the distribution.
113 *
114 * 3. The name "DOM4J" must not be used to endorse or promote
115 *    products derived from this Software without prior written
116 *    permission of MetaStuff, Ltd.  For written permission,
117 *    please contact dom4j-info@metastuff.com.
118 *
119 * 4. Products derived from this Software may not be called "DOM4J"
120 *    nor may "DOM4J" appear in their names without prior written
121 *    permission of MetaStuff, Ltd. DOM4J is a registered
122 *    trademark of MetaStuff, Ltd.
123 *
124 * 5. Due credit should be given to the DOM4J Project
125 *    (http://dom4j.org/).
126 *
127 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
128 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
129 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
130 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
131 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
132 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
133 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
134 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
135 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
136 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
137 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
138 * OF THE POSSIBILITY OF SUCH DAMAGE.
139 *
140 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
141 *
142 * $Id: FlyweightProcessingInstruction.java,v 1.1 2001/06/20 18:59:23 jstrachan Exp $
143 */