View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  package com.eviware.soapui.support.xml;
13  
14  import com.eviware.soapui.support.types.StringToStringMap;
15  
16  /***
17   * Separates a path component into its parts.
18   * 
19   * @author lars
20   */
21  public class XPathComponent
22  {
23     private String namespace;
24     private String prefix;
25     private String localNameWithoutBraces;
26     
27     // index and conditions, for example "[1]" or "[x > 3]"
28     private String braces;
29     
30     public XPathComponent(String c, StringToStringMap prefixMap)
31     {
32        String localName;
33        int ix = c.indexOf(':');
34        if(ix >= 0)
35        {
36           prefix = c.substring(0, ix);
37           localName = c.substring(ix+1);
38           namespace = prefixMap.get(prefix);
39        }
40        else
41        {
42           prefix = null;
43           localName = c;
44           namespace = null;
45        }
46        ix = localName.indexOf('[');
47        if(ix >= 0)
48        {
49           localNameWithoutBraces = localName.substring(0, ix);
50           braces = localName.substring(ix);
51        }
52        else
53        {
54           localNameWithoutBraces = localName;
55           braces = "";
56        }
57        assert localName.equals(localNameWithoutBraces + braces) :
58           localName + " != " + localNameWithoutBraces + " + " + braces;
59     }
60     
61     @Override
62     public String toString()
63     {
64        if(prefix != null)
65           return prefix + ":" + localNameWithoutBraces + braces;
66        else
67           return localNameWithoutBraces + braces;
68     }
69     
70     public String getNamespace()
71     {
72        return namespace;
73     }
74     
75     public boolean hasPrefix()
76     {
77        return prefix != null;
78     }
79     
80     public String getPrefix()
81     {
82        if( prefix == null )
83           return "";
84        else
85           return prefix;
86     }
87     
88     public String getLocalName()
89     {
90        return localNameWithoutBraces;
91     }
92     
93     public String getBraces()
94     {
95        return braces;
96     }
97     
98     public String getFullNameWithPrefix()
99     {
100       return getFullNameWithPrefix(localNameWithoutBraces);
101    }
102    
103    public String getFullNameWithPrefix(String aLocalName)
104    {
105       return (hasPrefix() ? getPrefix() + ":" : "") + aLocalName + getBraces();
106    }
107 }