Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.file / org.gvsig.fmap.dal.file.csv / src / main / java / org / gvsig / fmap / dal / store / gml / virtualrows / xmlinfo / XMLAttributeInfoImpl.java @ 47638

History | View | Annotate | Download (6.64 KB)

1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package org.gvsig.fmap.dal.store.gml.virtualrows.xmlinfo;
7

    
8
import java.util.HashMap;
9
import java.util.Locale;
10
import java.util.Map;
11
import org.apache.commons.io.FilenameUtils;
12
import org.apache.commons.lang3.mutable.MutableInt;
13
import org.gvsig.fmap.dal.DALLocator;
14
import org.gvsig.fmap.dal.DataTypes;
15
import org.gvsig.fmap.dal.feature.DataTypeDetector;
16
import org.gvsig.fmap.geom.type.GeometryType;
17
import org.gvsig.tools.ToolsLocator;
18
import org.gvsig.tools.dataTypes.DataType;
19
import org.gvsig.tools.dataTypes.DataTypesManager;
20

    
21
/**
22
 *
23
 * @author jjdelcerro
24
 */
25
public class XMLAttributeInfoImpl {
26
    
27
    private String path;
28
    private String name;
29
    private int type;
30
    private Map<String, MutableInt> childCounts;
31
    private Map<String, Integer> childCountsMax;
32
    private Map<String, String> childIds;
33
    private DataTypeDetector dataTypeDetector;
34
    private boolean isAttr;
35
    private String ns;
36

    
37
    protected XMLAttributeInfoImpl(Locale locale) {
38
        this.path = null;
39
        this.name = null;
40
        this.type = DataTypes.STRING;
41
        this.childCounts = new HashMap<>();
42
        this.childIds = new HashMap<>();
43
        this.childCountsMax = new HashMap<>();
44
        this.dataTypeDetector = DALLocator.getDataManager().createDataTypeDetector(locale);
45
        this.isAttr = false;
46
    }
47

    
48
    public XMLAttributeInfoImpl(Locale locale, String path, String ns) {
49
        this(locale);
50
        this.path = path;
51
        this.name = FilenameUtils.getBaseName(path);
52
        this.ns = ns;
53
        //            if( StringUtils.contains(this.path, "/ALCOHOL/") ) {
54
        //                System.out.println("### TAG: "+this.path);
55
        //            }
56
    } //            if( StringUtils.contains(this.path, "/ALCOHOL/") ) {
57
    //                System.out.println("### TAG: "+this.path);
58
    //            }
59

    
60
    public XMLAttributeInfoImpl(Locale locale, String name, int type) {
61
        this(locale);
62
        this.name = name;
63
        this.setType(type);
64
    }
65

    
66
    public String getName() {
67
        return name;
68
    }
69

    
70
    public String getPath() {
71
        return path;
72
    }
73

    
74
    public void setType(int type) {
75
        this.type = type;
76
        this.dataTypeDetector = null;
77
    }
78

    
79
    public int getType() {
80
        if (this.dataTypeDetector == null) {
81
            return this.type;
82
        }
83
        int x = this.dataTypeDetector.getDataType().getType();
84
        if (x == DataTypes.UNKNOWN) {
85
            return DataTypes.STRING;
86
        }
87
        return x;
88
    }
89

    
90
    public int getPrecision() {
91
        if (this.dataTypeDetector == null) {
92
            return DataType.PRECISION_NONE;
93
        }
94
        return this.dataTypeDetector.getDataType().getPrecision();
95
    }
96

    
97
    public int getScale() {
98
        if (this.dataTypeDetector == null) {
99
            return DataType.SCALE_NONE;
100
        }
101
        return this.dataTypeDetector.getDataType().getScale();
102
    }
103

    
104
    public int getSize() {
105
        if (this.dataTypeDetector == null) {
106
            return 0;
107
        }
108
        if (this.getType() != DataTypes.STRING) {
109
            return 0;
110
        }
111
        int size = this.dataTypeDetector.getDataType().getDisplaySize();
112
        if (size > 0) {
113
            size = ((size + 10) / 10) * 10;
114
        }
115
        return size;
116
    }
117

    
118
    public GeometryType getGeometryType() {
119
        if (this.dataTypeDetector == null) {
120
            return null;
121
        }
122
        return this.dataTypeDetector.getDataType().getGeometryType();
123
    }
124

    
125
    public String getNs() {
126
        return ns;
127
    }
128
    
129
    
130

    
131
    @Override
132
    public String toString() {
133
        DataTypesManager dataTypeManager = ToolsLocator.getDataTypesManager();
134
        StringBuilder builder = new StringBuilder();
135
        builder.append(name);
136
        builder.append(" ");
137
        builder.append(dataTypeManager.getTypeName(this.getType()));
138
        switch (this.getType()) {
139
            case DataTypes.STRING:
140
                builder.append("(");
141
                builder.append(this.getSize());
142
                builder.append(")");
143
                break;
144
            case DataTypes.DECIMAL:
145
                builder.append("(");
146
                builder.append(this.getPrecision());
147
                builder.append(",");
148
                builder.append(this.getScale());
149
                builder.append(")");
150
                break;
151
            case DataTypes.GEOMETRY:
152
                builder.append("(");
153
                builder.append(this.getGeometryType().getFullName());
154
                builder.append(")");
155
                break;
156
        }
157
        return builder.toString();
158
    }
159

    
160
    public void incrChildCount(String childName) {
161
        MutableInt count = this.childCounts.get(childName);
162
        if (count == null) {
163
            count = new MutableInt(0);
164
            this.childCounts.put(childName, count);
165
        }
166
        count.increment();
167
    }
168

    
169
    public void setLastChildID(String childName, String idvalue) {
170
        this.childIds.put(childName, idvalue);
171
    }
172

    
173
    public String getLastChildID(String childName) {
174
        return this.childIds.get(childName);
175
    }
176

    
177
    public void consolidateChildCounters() {
178
        for (Map.Entry<String, MutableInt> entry : childCounts.entrySet()) {
179
            String childName = entry.getKey();
180
            MutableInt count = entry.getValue();
181
            int countmax = this.childCountsMax.getOrDefault(childName, -1);
182
            if (countmax < count.getValue() || countmax < 0) {
183
                this.childCountsMax.put(childName, count.getValue());
184
            }
185
            childCounts.get(childName).setValue(0);
186
        }
187
    }
188

    
189
    public int getMaxChildCount(String childName) {
190
        int countmax = this.childCountsMax.getOrDefault(childName, 0);
191
        return countmax;
192
    }
193

    
194
    public void setSize(int size) {
195
        if (this.dataTypeDetector == null) {
196
            return;
197
        }
198
        DataTypeDetector.DataTypeDetected detectedType = this.dataTypeDetector.getDataType();
199
        detectedType.setDisplaySize(size);
200
    }
201

    
202
    public void addValue(String value) {
203
        if (this.dataTypeDetector == null) {
204
            return;
205
        }
206
        this.dataTypeDetector.addValue(value);
207
    }
208

    
209
    public boolean hasChilds() {
210
        return  !this.childCounts.isEmpty();
211
    }
212

    
213
    public int getChildsCount(String name) {
214
        MutableInt count = this.childCounts.get(name);
215
        if(count == null){
216
            return 0;
217
        }
218
        return count.getValue();
219
    }
220

    
221
    public void setIsAttr(boolean isAttr) {
222
        this.isAttr = isAttr;
223
    }
224
    
225
    public boolean isAttr() {
226
        return this.isAttr;
227
    }
228
    
229
}