- Timestamp:
- 2020-03-21T12:00:55+01:00 (5 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/tools
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/XmlObjectParser.java
r16180 r16181 17 17 import java.util.Locale; 18 18 import java.util.Map; 19 import java.util.Optional; 19 20 import java.util.Stack; 20 21 … … 68 69 private final Stack<Object> current = new Stack<>(); 69 70 private StringBuilder characters = new StringBuilder(64); 70 71 71 private Locator locator; 72 private final StringParser primitiveParsers = new StringParser(StringParser.DEFAULT) 73 .registerParser(boolean.class, this::parseBoolean) 74 .registerParser(Boolean.class, this::parseBoolean); 72 75 73 76 @Override … … 123 126 } 124 127 125 private Object getValueForClass(Class<?> klass, String value) {126 if (boolean.class.equals(klass))127 return parseBoolean(value);128 else if (char.class.equals(klass))129 return value.charAt(0);130 else if (short.class.equals(klass) || Short.class.equals(klass))131 return Integer.valueOf(value);132 else if (Integer.class.equals(klass))133 return Integer.valueOf(value);134 else if (Long.class.equals(klass))135 return Long.valueOf(value);136 else if (Float.class.equals(klass))137 return Float.valueOf(value);138 else if (Double.class.equals(klass))139 return Double.valueOf(value);140 return value;141 }142 143 128 private void setValue(Entry entry, String fieldName, String value) throws SAXException { 144 129 if (value != null) { … … 157 142 f = entry.getField("locale_" + fieldName.substring(lang.length())); 158 143 } 159 if (f != null && Modifier.isPublic(f.getModifiers()) && ( 160 String.class.equals(f.getType()) || boolean.class.equals(f.getType()) || char.class.equals(f.getType()) || 161 Float.class.equals(f.getType()) || Double.class.equals(f.getType()) || 162 short.class.equals(f.getType()) || Short.class.equals(f.getType()) || 163 Long.class.equals(f.getType()) || Integer.class.equals(f.getType()))) { 164 f.set(c, getValueForClass(f.getType(), value)); 144 Optional<?> parsed; 145 if (f != null && Modifier.isPublic(f.getModifiers()) && (parsed = primitiveParsers.tryParse(f.getType(), value)).isPresent()) { 146 f.set(c, parsed.get()); 165 147 } else { 166 148 String setter; … … 173 155 Method m = entry.getMethod(setter); 174 156 if (m != null) { 175 m.invoke(c, getValueForClass(m.getParameterTypes()[0], value)); 157 parsed = primitiveParsers.tryParse(m.getParameterTypes()[0], value); 158 m.invoke(c, parsed.isPresent() ? parsed.get() : value); 176 159 } 177 160 }
Note:
See TracChangeset
for help on using the changeset viewer.