summaryrefslogtreecommitdiff
path: root/src/com/memberwebs/ldapxml/map/LXBase.java
blob: 82b3d090d40bbf0c2b40926d6eb46e47620cdfa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
 * Copyright (c) 2004, Nate Nielsen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above
 *       copyright notice, this list of conditions and the
 *       following disclaimer.
 *     * Redistributions in binary form must reproduce the
 *       above copyright notice, this list of conditions and
 *       the following disclaimer in the documentation and/or
 *       other materials provided with the distribution.
 *     * The names of contributors to this software may not be
 *       used to endorse or promote products derived from this
 *       software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 *
 * CONTRIBUTORS
 *  Nate Nielsen <nielsen@memberwebs.com>
 *
 */

package com.memberwebs.ldapxml.map;

import java.util.Enumeration;
import java.util.Hashtable;

/**
 * A base class for all objects in an LX map tree.
 *
 * @author nielsen@memberwebs.com
 * @version 0.5
 */
public class LXBase
{
    /**
     * Constructs an LXBase object.
     *
     * @param parent The parent object in the LX map tree.
     */
    LXBase(LXBase parent)
    {
        m_name = null;
        m_xmlName = null;
        m_nameSpace = null;
        m_ns = null;
        m_parent = parent;
        m_isUseable = true;

        // If we have a parent and it doesn't have any children set
        // Then give it child capabilities.
        if(parent != null && parent.m_children == null)
            parent.m_children = new Hashtable();
    }

    /**
     * Get the full XML name for this object. This includes any namespace
     * prefixes prepended to the name.
     *
     * @return The XML name.
     */
    public String getXmlName()
    {
        String xmlName = m_xmlName;
        if(xmlName == null)
            xmlName = m_name;

        if(xmlName != null)
        {
            String prefix = getPrefix();
            if(prefix != null)
                xmlName =  prefix + ":" + xmlName;
        }

        return xmlName;
    }


    /**
     * Get the name of this object.
     *
     * @return The name.
     */
    public final String getName()
    {
        return m_name;
    }


    /**
     * Set the name of this object. Updates the parent (if any)
     * to reflect this change in tree structure.
     *
     * @param name The new name
     */
    void setName(String name)
    {
        if(m_name != null && m_parent != null)
            m_parent.m_children.remove(m_name);

        if(name != null)
        {
            if(m_parent != null)
                m_parent.m_children.put(name, this);

            m_name = name;
        }
    }

    /**
     * Get the namespace prefix for this object. If this object has
     * no namespaces info set, returns the parent objects namespace
     * prefix.
     *
     * @return The namespace prefix, or null if there is no prefix.
     */
    public String getPrefix()
    {
        // If we have a namespace then return
        // the ns value regardless
        if(m_nameSpace != null)
            return m_ns;

        if(m_ns != null)
            return m_ns;

        if(m_parent != null)
            return m_parent.getPrefix();

        return null;
    }

    /**
     * Get the full namespace URI for this object.
     *
     * @param here If set to true, then parent namespaces will
     *             be returned if none is set on this object.
     * @return The namespace.
     */
    public String getNamespace(boolean here)
    {
        if(m_nameSpace != null || here)
            return m_nameSpace;

        if(m_parent != null)
            return m_parent.getNamespace();

        return null;
    }

    /**
     * Get the full namespace URI for this node.
     * If this object has no namespace, it's parent will
     * be queried.
     *
     * @return The namespace.
     */
    public String getNamespace()
    {
        return getNamespace(false);
    }

    /**
     * Get this object's parent in the LX map tree.
     *
     * @return The parent, or null if this object has no parent.
     */
    public final LXBase getParent()
    {
        return m_parent;
    }

    /**
     * Get a child of this object in the LX map tree.
     *
     * @param name The child's name.
     * @return The child, or null if no such child is present.
     */
    public final LXBase getChild(String name)
    {
        if(m_children == null)
            return null;

        return (LXBase)m_children.get(name);
    }

    /**
     * Get the names of all the child objects.
     *
     * @return An enumeraton of the child attribute names.
     */
    public final Enumeration getChildNames()
    {
        if(m_children == null)
            return null;

        return m_children.keys();
    }

    /**
     * Check whether this object has been marked unusable.
     *
     * @return Usability status.
     */
    public final boolean isUseable()
    {
        return m_isUseable;
    }

    /**
     * Get the LX hook for this object. If this
     * object has no hook, then the parent will be queried.
     *
     * @return The class name of the LX hook.
     */
    public String getHook()
    {
        if(m_hook == null && m_parent != null)
            return m_parent.getHook();

        return m_hook;
    }

    // The name to be used when creating XML for this object.
    String m_xmlName;

    // Namespace information for this object
    String m_nameSpace;
    String m_ns;

    // The hook for this object and below
    String m_hook;

    // Is this object useable or not
    boolean m_isUseable;

    // This object's parent in the LX map
    private LXBase m_parent;

    // This object's name
    private String m_name;

    // Children in the LX map tree of this object.
    private Hashtable m_children;
};