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 @ 47646

History | View | Annotate | Download (6.79 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
    private String srs;
37

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
230
    public void setSrs(String value) {
231
        this.srs = value;
232
    }
233

    
234
    public String getSrs() {
235
        return srs;
236
    }
237
    
238
}