View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package goldengate.common.xml;
22  
23  
24  /**
25   * XmlDecl to declare types, path and name for values from/to XML file/document
26   * 
27   * @author Frederic Bregier
28   * 
29   */
30  public class XmlDecl {
31      private final String name;
32  
33      private final XmlType type;
34  
35      private final String xmlPath;
36  
37      private final XmlDecl[] subXml;
38  
39      private final boolean isMultiple;
40  
41      public XmlDecl(String name, String xmlPath) {
42          this.name = name;
43          this.type = XmlType.EMPTY;
44          this.xmlPath = xmlPath;
45          this.isMultiple = false;
46          this.subXml = null;
47      }
48  
49      public XmlDecl(XmlType type, String xmlPath) {
50          this.name = xmlPath;
51          this.type = type;
52          this.xmlPath = xmlPath;
53          this.isMultiple = false;
54          this.subXml = null;
55      }
56  
57      public XmlDecl(String name, XmlType type, String xmlPath) {
58          this.name = name;
59          this.type = type;
60          this.xmlPath = xmlPath;
61          this.isMultiple = false;
62          this.subXml = null;
63      }
64  
65      public XmlDecl(String name, XmlType type, String xmlPath, boolean isMultiple) {
66          this.name = name;
67          this.type = type;
68          this.xmlPath = xmlPath;
69          this.isMultiple = isMultiple;
70          this.subXml = null;
71      }
72  
73      public XmlDecl(String name, XmlType type, String xmlPath, XmlDecl[] decls,
74              boolean isMultiple) {
75          this.name = name;
76          this.type = type;
77          this.xmlPath = xmlPath;
78          this.isMultiple = isMultiple;
79          this.subXml = decls;
80      }
81  
82      /**
83       * Get Java field name
84       * 
85       * @return the field name
86       */
87      public String getName() {
88          return name;
89      }
90  
91      /**
92       * @return the class type
93       */
94      public Class<?> getClassType() {
95          return type.getClassType();
96      }
97  
98      /**
99       * @return the internal type
100      */
101     public XmlType getType() {
102         return type;
103     }
104 
105     /**
106      * @return the xmlPath
107      */
108     public String getXmlPath() {
109         return xmlPath;
110     }
111 
112     /**
113      * 
114      * @return True if this Decl is a subXml
115      */
116     public boolean isSubXml() {
117         return this.subXml != null;
118     }
119 
120     /**
121      * @return the subXml
122      */
123     public XmlDecl[] getSubXml() {
124         return subXml;
125     }
126 
127     /**
128      * @return the subXml size
129      */
130     public int getSubXmlSize() {
131         if (subXml == null) return 0;
132         return subXml.length;
133     }
134 
135     /**
136      * @return the isMultiple
137      */
138     public boolean isMultiple() {
139         return isMultiple;
140     }
141 
142     /**
143      * Check if two XmlDecl are compatible
144      * 
145      * @param xmlDecl
146      * @return True if compatible
147      */
148     public boolean isCompatible(XmlDecl xmlDecl) {
149         if (((isMultiple && xmlDecl.isMultiple) || ((!isMultiple) && (!xmlDecl.isMultiple))) &&
150                 ((isSubXml() && xmlDecl.isSubXml()) || 
151                         ((!isSubXml()) && (!xmlDecl.isSubXml())))) {
152             if (!isSubXml()) {
153                 return type == xmlDecl.type;
154             }
155             if (subXml.length != xmlDecl.subXml.length) {
156                 return false;
157             }
158             for (int i = 0; i < subXml.length; i ++) {
159                 if (!subXml[i].isCompatible(xmlDecl.subXml[i])) {
160                     return false;
161                 }
162             }
163             return true;
164         }
165         return false;
166     }
167 
168     public String toString() {
169         return "Decl: " + name + " Type: " + type.name() + " XmlPath: " +
170                 xmlPath + " isMultiple: " + isMultiple + " isSubXml: " +
171                 isSubXml();
172     }
173 }