1/*
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.internal.xjc.reader.dtd.bindinfo;
27
28import java.util.ArrayList;
29
30import com.sun.tools.internal.xjc.generator.bean.field.FieldRenderer;
31import com.sun.tools.internal.xjc.generator.bean.field.FieldRendererFactory;
32
33import org.w3c.dom.Attr;
34import org.w3c.dom.Element;
35
36/** {@code <attribute>} declaration in the binding file. */
37public class BIAttribute
38{
39    /**
40     * Wraps a given {@code <attribute>} element.
41     * <p>
42     * Should be created only from {@link BIElement}.
43     */
44    BIAttribute( BIElement _parent, Element _e ) {
45        this.parent = _parent;
46        this.element = _e;
47    }
48
49    private final BIElement parent;
50    private final Element element;
51
52    /** Gets the name of this attribute-property declaration. */
53    public final String name() {
54        return element.getAttribute("name");
55    }
56
57
58    /**
59     * Gets the conversion method for this attribute, if any.
60     *
61     * @return
62     *        If the convert attribute is not specified, this
63     *        method returns null.
64     */
65    public BIConversion getConversion() {
66        if (element.getAttributeNode("convert") == null)
67            return null;
68
69        String cnv = element.getAttribute("convert");
70        return parent.conversion(cnv);
71    }
72
73    /**
74     * Gets the realization of this particle, if any.
75     *
76     * @return
77     *      null if the "collection" attribute was not specified.
78     */
79    public final FieldRenderer getRealization() {
80        Attr a = element.getAttributeNode("collection");
81        if(a==null)     return null;
82
83        String v = element.getAttribute("collection").trim();
84
85        FieldRendererFactory frf = parent.parent.model.options.getFieldRendererFactory();
86        if(v.equals("array"))   return frf.getArray();
87        if(v.equals("list"))
88            return frf.getList(
89                parent.parent.codeModel.ref(ArrayList.class));
90
91        // the correctness of the attribute value must be
92        // checked by the validator.
93        throw new InternalError("unexpected collection value: "+v);
94    }
95
96    /**
97     * Gets the property name for this attribute.
98     *
99     * @return
100     *      always a non-null, valid string.
101     */
102    public final String getPropertyName() {
103        String r = DOMUtil.getAttribute(element,"property");
104        if(r!=null)     return r;
105        else            return name();
106    }
107}
108