001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */ 
017    
018    package org.apache.commons.betwixt.schema;
019    
020    
021    
022    /**
023     * Models a global definition of an <code>element</code>.
024     * @author <a href='http://commons.apache.org/'>Apache Commons Team</a>
025     * @version $Revision: 561314 $
026     */
027    public class GlobalElement implements Element {
028            //TODO: going to ignore the issue of namespacing for the moment
029            public static final String STRING_SIMPLE_TYPE="xsd:string";
030            
031            private String name;
032            private String type;
033    
034        private GlobalComplexType complexType;
035            
036            public GlobalElement() {}
037        
038        public GlobalElement(String name, String type) {
039            setName(name);
040            setType(type);
041        }
042        
043        public GlobalElement(String name, GlobalComplexType complexType) {
044            setName(name);
045            setComplexType(complexType);
046        }
047        
048    
049        
050    
051        /**
052         * Gets the element name
053         * @return element name, not null
054         */
055        public String getName() {
056            return name;
057        }
058    
059        /**
060         * Sets the element name
061         * @param string not null
062         */
063        public void setName(String string) {
064            name = string;
065        }
066    
067        /**
068         * Gets the element type
069         * @return the type of the element
070         */
071        public String getType() {
072            return type;
073        }
074    
075        /**
076         * Sets the element type
077         * @param string
078         */
079        public void setType(String string) {
080            type = string;
081        }
082    
083    
084        /**
085         * Gets the anonymous type definition for this element, if one exists.
086         * @return ComplexType, null if there is no associated anonymous type definition
087         */
088        public GlobalComplexType getComplexType() {
089            return complexType;
090        }
091    
092        /**
093         * Sets the anonymous type definition for this element
094         * @param type ComplexType to be set as the anonymous type definition, 
095         * null if the type is to be referenced
096         */
097        public void setComplexType(GlobalComplexType type) {
098            this.type = type.getName();
099            complexType = type;
100        }    
101    
102            public boolean equals(Object obj) {
103                    boolean result = false;
104                    if (obj instanceof GlobalElement) {
105                GlobalElement element = (GlobalElement) obj;
106                result = isEqual(type, element.type) &&
107                         isEqual(name, element.name);               
108                    }
109                    return result;
110            }
111        
112        public int hashCode() {
113            return 0;
114        }
115    
116            /**
117             * Null safe equals method
118             * @param one
119             * @param two
120             * @return
121             */
122            private boolean isEqual(String one, String two) {
123                    boolean result = false;
124                    if (one == null) {
125                            result = (two == null); 
126                    }
127                    else
128                    {
129                            result = one.equals(two);
130                    }
131                    
132                    return result;
133            }
134    
135        public String toString() {
136            StringBuffer buffer = new StringBuffer();
137            buffer.append("<xsd:element name='");
138            buffer.append(name);
139            buffer.append("' type='");
140            buffer.append(type);
141            buffer.append("'>");
142            
143            if (complexType != null) {
144                buffer.append(complexType);
145            }
146            buffer.append("</xsd:element>");
147            return buffer.toString();
148        }
149    
150    
151    }