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