private void InitFromStrings(Type[] keys, string[] values) { if (keys.Length != values.Length) { throw new NotSupportedException("Argument array lengths differ: " + Arrays.ToString(keys) + " vs. " + Arrays.ToString(values)); } for (int i = 0; i < keys.Length; i++) { Type coreKeyClass = keys[i]; string value = values[i]; try { Type valueClass = AnnotationLookup.GetValueType(coreKeyClass); if (valueClass.Equals(typeof(string))) { this.Set(coreKeyClass, values[i]); } else { if (valueClass == typeof(int)) { this.Set(coreKeyClass, System.Convert.ToInt32(values[i])); } else { if (valueClass == typeof(double)) { this.Set(coreKeyClass, double.ParseDouble(values[i])); } else { if (valueClass == typeof(long)) { this.Set(coreKeyClass, long.Parse(values[i])); } else { throw new Exception("Can't handle " + valueClass); } } } } } catch (Exception e) { // unexpected value type throw new NotSupportedException("CORE: CoreLabel.initFromStrings: " + "Bad type for " + coreKeyClass.GetSimpleName() + ". Value was: " + value + "; expected " + AnnotationLookup.GetValueType(coreKeyClass), e); } } }
public static Type[] ParseStringKeys(string[] keys) { Type[] classes = new Type[keys.Length]; for (int i = 0; i < keys.Length; i++) { string key = keys[i]; classes[i] = AnnotationLookup.ToCoreKey(key); // now work with the key we got above if (classes[i] == null) { throw new NotSupportedException("Unknown key " + key); } } return(classes); }
//Unchecked is below because eclipse can't handle the level of type inference if we correctly parametrize GenericAnnotation with String private void InitFromStrings(string[] keys, string[] values) { if (keys.Length != values.Length) { throw new NotSupportedException("Argument array lengths differ: " + Arrays.ToString(keys) + " vs. " + Arrays.ToString(values)); } for (int i = 0; i < keys.Length; i++) { string key = keys[i]; string value = values[i]; Type coreKeyClass = AnnotationLookup.ToCoreKey(key); //now work with the key we got above if (coreKeyClass == null) { if (key != null) { throw new NotSupportedException("Unknown key " + key); } } else { // It used to be that the following code let you put unknown keys // in the CoreLabel. However, you can't create classes dynamically // at run time, which meant only one of these classes could ever // exist, which meant multiple unknown keys would clobber each // other and be very annoying. It's easier just to not allow // it at all. // If it becomes possible to create classes dynamically, // we could add this code back. //if(genericKeys.containsKey(key)) { // this.set(genericKeys.get(key), value); //} else { // GenericAnnotation<String> newKey = new GenericAnnotation<String>() { // public Class<String> getType() { return String.class;} }; // this.set(newKey.getClass(), values[i]); // genericKeys.put(keys[i], newKey.getClass()); // genericValues.put(newKey.getClass(), keys[i]); //} // unknown key; ignore //if (VERBOSE) { // log.info("CORE: CoreLabel.fromAbstractMapLabel: " + // "Unknown key "+key); //} try { Type valueClass = AnnotationLookup.GetValueType(coreKeyClass); if (valueClass.Equals(typeof(string))) { this.Set(coreKeyClass, values[i]); } else { if (valueClass == typeof(int)) { this.Set(coreKeyClass, System.Convert.ToInt32(values[i])); } else { if (valueClass == typeof(double)) { this.Set(coreKeyClass, double.ParseDouble(values[i])); } else { if (valueClass == typeof(long)) { this.Set(coreKeyClass, long.Parse(values[i])); } else { throw new Exception("Can't handle " + valueClass); } } } } } catch (Exception e) { // unexpected value type throw new NotSupportedException("CORE: CoreLabel.initFromStrings: " + "Bad type for " + key + ". Value was: " + value + "; expected " + AnnotationLookup.GetValueType(coreKeyClass), e); } } } }