public Object parse() throws RuntimeException { NBTCompound result = new NBTCompound(); for (TypeParser parser : this.parsers) { String key = parser.name; if (key.startsWith("\"") && key.endsWith("\"")) { key = StringParser.parse(key.substring(1, key.length() - 1)); } result.put(key, (nbtUtils.getValue(parser.parse()))); } return result.getHandle(); }
public <T> T get(String msg, StringParser<T> handler) { T value = null; while (value == null) { String input = this.getString(msg); try { value = handler.parse(input); } catch (IllegalArgumentException e) { out.println(e.getMessage()); } } return value; }
/** * Get a Map with where the key is the converted value (according to keyType) of the attribute * named key and the value is given in the same way. * * @param doc the document to read * @param key the attribute name where its value will be converted and used as a key in the map * @param keyType the Class of the key * @param value the attribute name where its value will be converted and used as a value in the * map * @param valueType the Class of the value * @return the corresponding map. */ public static final <T, U> Map<T, U> get( final Document doc, final String key, final Class<T> keyType, final String value, final Class<U> valueType, final String path) { XPath xp = xpathFactory.newXPath(); Map<T, U> map = new HashMap<T, U>(); NodeList nodes; try { nodes = (NodeList) xp.compile(path).evaluate(doc, XPathConstants.NODESET); } catch (XPathExpressionException e) { System.err.println(e); return map; } int len = nodes.getLength(); for (int i = 0; i < len; i++) { NamedNodeMap nmap = nodes.item(i).getAttributes(); Node k = nmap.getNamedItem(key); Node v = nmap.getNamedItem(value); if (k == null || v == null) { return map; } String kk = k.getNodeValue(); String vv = v.getNodeValue(); StringParser convK = conv.get(keyType); StringParser convV = conv.get(valueType); if (convK == null || convV == null) { return map; } map.put((T) convK.parse(kk), (U) convV.parse(vv)); } return map; }
@Override protected Object parse(Class<?> valueType, String stringValue) { try { stringValue = stringParser.parse(String.class, stringValue); Method valueOf = valueType.getMethod("valueOf", String.class); return valueType.cast(valueOf.invoke(null, stringValue)); } catch (Exception e) { throw new IllegalStateException( "Failed to parse value using a valueOf() method in class '" + valueType.getName() + "': " + stringValue, e); } }
/** * Getter for annoted class (with @XConfAttribute) * * @param type the class type * @param nodes the nodes to read * @return an array of instances of type, if the class is annoted * with @XConfAttribute(isStatic=true), the returned array is empty. */ private static final <T> T[] get(final Class<T> type, NodeList nodes) { Method[] meths = type.getDeclaredMethods(); List<String[]> attrs = new ArrayList<String[]>(); List<Method> methods = new ArrayList<Method>(); Map<String[], Method> mapMethods = new HashMap<String[], Method>(); for (Method m : meths) { String name = m.getName(); Annotation ann = m.getAnnotation(XConfAttribute.class); if (ann != null) { String[] attributes = ((XConfAttribute) ann).attributes(); if (attributes.length == m.getParameterTypes().length) { m.setAccessible(true); attrs.add(attributes); methods.add(m); } else { return null; } } } Annotation ann = type.getAnnotation(XConfAttribute.class); boolean mustInstantiate = !((XConfAttribute) ann).isStatic(); T[] values = null; int len = nodes.getLength(); if (mustInstantiate) { values = (T[]) Array.newInstance(type, len); } for (int i = 0; i < len; i++) { NamedNodeMap map = nodes.item(i).getAttributes(); String nodeName = nodes.item(i).getNodeName(); if (mustInstantiate) { try { Constructor<T> constructor = type.getDeclaredConstructor(new Class[] {}); constructor.setAccessible(true); values[i] = constructor.newInstance(); } catch (InstantiationException e) { System.err.println(e); break; } catch (IllegalAccessException e) { System.err.println(e); break; } catch (NoSuchMethodException e) { System.err.println(e); break; } catch (InvocationTargetException e) { System.err.println(e.getTargetException()); } } for (int j = 0; j < methods.size(); j++) { Method method = methods.get(j); ann = method.getAnnotation(XConfAttribute.class); String tag = ((XConfAttribute) ann).tag(); if (tag.isEmpty() || tag.equals(nodeName)) { String[] attributes = attrs.get(j); Object[] params = new Object[attributes.length]; Class[] paramsClass = method.getParameterTypes(); for (int k = 0; k < attributes.length; k++) { String p = ""; Node node = null; if (map != null) { node = map.getNamedItem(attributes[k]); } if (node != null) { p = node.getNodeValue(); } StringParser parser = conv.get(paramsClass[k]); if (parser != null) { params[k] = parser.parse(p); } } try { if (mustInstantiate) { method.invoke(values[i], params); } else { method.invoke(null, params); } } catch (IllegalAccessException e) { System.err.println(e); } catch (IllegalArgumentException e) { System.err.println(e); } catch (InvocationTargetException e) { System.err.println(e.getTargetException()); } } } } if (mustInstantiate) { return values; } else { return (T[]) Array.newInstance(type, 0); } }
/** * Get all the nodes with the given path. All the get nodes are serialized into an object (generic * paramater) which must have a constructor without argument and with methods named * set<Attribute Name> with one argument and no returned value. For example a node <foo * aaa="1" bbb="true" ccc-ddd="#001122"/> could be retrieved with something like * XConfiguration.get(MyObject.class, doc, "//path/to/node") where MyObject should be something * like <code> * public class MyObject { * * public MyObject() { * // ... * } * * public void setAaa(int a) { * // ... * } * * public void setBbb(boolean b) { * // ... * } * * public void setCccDdd(Color c) { * // ... * } * } * </code> If an attribute must not be retrieved, just remove the setter. * * <p>It is possible to use the annotation @XConfAttribute to make easier the retrievement. For * example <code> * @XConfAttribute * public class MyObject { * * public MyObject() { * // ... * } * * @XConfAttribute(attributes={"aaa", "bbb", "ccc-ddd"}) * // the contents of aaa will be converted into int and passed as first argument * // the contents of bbb will be converted into boolean and passed as second argument * // the contents of ccc-ddd will be converted into Color and passed as third argument * public void set(int n, boolean b, Color c) { * // ... * } * } * </code> * * @param type the Class type to retrieve * @param doc the document to explore * @param path the xpath query to retrieve the corresponding nodeset. * @return an array of instance of class type. */ public static final <T> T[] get(final Class<T> type, final Document doc, final String path) { XPath xp = xpathFactory.newXPath(); NodeList nodes; try { nodes = (NodeList) xp.compile(path).evaluate(doc, XPathConstants.NODESET); } catch (XPathExpressionException e) { System.err.println(e); return (T[]) Array.newInstance(type, 0); } if (type.getAnnotation(XConfAttribute.class) != null) { T[] arr = get(type, nodes); if (arr != null) { return arr; } } Method[] meths = type.getDeclaredMethods(); Map<String, Method> mapMethods = new HashMap<String, Method>(); for (Method m : meths) { String name = m.getName(); if (name.startsWith("set") && m.getParameterTypes().length == 1 && m.getReturnType().equals(Void.TYPE)) { mapMethods.put(m.getName(), m); m.setAccessible(true); } } Map<String, String> names = new HashMap<String, String>(); T[] values = (T[]) Array.newInstance(type, nodes.getLength()); for (int i = 0; i < values.length; i++) { NamedNodeMap map = nodes.item(i).getAttributes(); try { Constructor<T> constructor = type.getDeclaredConstructor(new Class[] {}); constructor.setAccessible(true); values[i] = constructor.newInstance(); } catch (InstantiationException e) { System.err.println(e); break; } catch (IllegalAccessException e) { System.err.println(e); break; } catch (NoSuchMethodException e) { System.err.println(e); break; } catch (InvocationTargetException e) { System.err.println(e.getTargetException()); } for (int j = 0; j < map.getLength(); j++) { Node n = map.item(j); String name = n.getNodeName(); String methName = names.get(name); if (methName == null) { StringBuilder buffer = new StringBuilder("set"); String[] parts = name.split("-"); for (String part : parts) { if (part != null && part.length() > 0) { buffer.append(part.substring(0, 1).toUpperCase()); buffer.append(part.substring(1).toLowerCase()); } } methName = buffer.toString(); names.put(name, methName); } String value = n.getNodeValue(); Method method = mapMethods.get(methName); if (method != null) { Class[] paramsClass = method.getParameterTypes(); StringParser parser = conv.get(paramsClass[0]); if (parser != null) { Object[] params = new Object[] {parser.parse(value)}; try { method.invoke(values[i], params); } catch (IllegalAccessException e) { System.err.println(e); } catch (IllegalArgumentException e) { System.err.println(e); } catch (InvocationTargetException e) { System.err.println(e.getTargetException()); } } } } } return values; }
/** * parse archive name * * @param archiveName archive name string */ public ArchiveNameParts(final String archiveName) { type = ""; loginName = ""; loginPassword = ""; hostName = ""; hostPort = 0; deviceName = ""; fileName = ""; if (archiveName.startsWith("ftp://")) { // ftp type = "ftp"; String specifier = archiveName.substring(6); Object[] data = new Object[2]; int index = 0; if (StringParser.parse( specifier.substring(index), "%s:%s@", data, StringParser.QUOTE_CHARS)) { loginName = (String) data[0]; loginPassword = (String) data[1]; index = specifier.indexOf('@') + 1; } else if (StringParser.parse( specifier.substring(index), "%s@", data, StringParser.QUOTE_CHARS)) { loginName = (String) data[0]; index = specifier.indexOf('@') + 1; } if (StringParser.parse(specifier.substring(index), "%s/%s", data, StringParser.QUOTE_CHARS)) { hostName = (String) data[0]; fileName = (String) data[1]; } else { fileName = specifier; } } else if (archiveName.startsWith("scp://")) { // scp type = "scp"; String specifier = archiveName.substring(6); Object[] data = new Object[3]; int index = 0; if (StringParser.parse(specifier.substring(index), "%s@", data, StringParser.QUOTE_CHARS)) { loginName = (String) data[0]; index = specifier.indexOf('@') + 1; } if (StringParser.parse( specifier.substring(index), "%s:%d/%s", data, StringParser.QUOTE_CHARS)) { hostName = (String) data[0]; hostPort = (Integer) data[1]; fileName = (String) data[2]; } else if (StringParser.parse( specifier.substring(index), "%s/%s", data, StringParser.QUOTE_CHARS)) { hostName = (String) data[0]; fileName = (String) data[1]; } else { fileName = specifier; } } else if (archiveName.startsWith("sftp://")) { // sftp type = "sftp"; String specifier = archiveName.substring(7); Object[] data = new Object[3]; int index = 0; if (StringParser.parse(specifier.substring(index), "%s@", data, StringParser.QUOTE_CHARS)) { loginName = (String) data[0]; index = specifier.indexOf('@') + 1; } if (StringParser.parse( specifier.substring(index), "%s:%d/%s", data, StringParser.QUOTE_CHARS)) { hostName = (String) data[0]; hostPort = (Integer) data[1]; fileName = (String) data[2]; } else if (StringParser.parse( specifier.substring(index), "%s/%s", data, StringParser.QUOTE_CHARS)) { hostName = (String) data[0]; fileName = (String) data[1]; } else { fileName = specifier; } } else if (archiveName.startsWith("dvd://")) { // dvd type = "dvd"; String specifier = archiveName.substring(6); Object[] data = new Object[2]; if (StringParser.parse(specifier, "%S:%S", data, StringParser.QUOTE_CHARS)) { deviceName = (String) data[0]; fileName = (String) data[1]; } else { fileName = specifier; } } else if (archiveName.startsWith("device://")) { // dvd type = "device"; String specifier = archiveName.substring(9); Object[] data = new Object[2]; if (StringParser.parse(specifier, "%S:%S", data, StringParser.QUOTE_CHARS)) { deviceName = (String) data[0]; fileName = (String) data[1]; } else { fileName = specifier; } } else if (archiveName.startsWith("file://")) { // file type = "filesystem"; String specifier = archiveName.substring(7); fileName = specifier.substring(2); } else { // file type = "filesystem"; fileName = archiveName; } }
public Object parse() throws RuntimeException { if (patDouble.matcher(value).matches()) { return nbtUtils.createTagDouble(parseDouble(value.substring(0, value.length() - 1))); } else if (patFloat.matcher(value).matches()) { return nbtUtils.createTagFloat(parseFloat(value.substring(0, value.length() - 1))); } else if (patByte.matcher(value).matches()) { return nbtUtils.createTagByte(parseByte(value.substring(0, value.length() - 1))); } else if (patLong.matcher(value).matches()) { return nbtUtils.createTagLong(parseLong(value.substring(0, value.length() - 1))); } else if (patShort.matcher(value).matches()) { return nbtUtils.createTagShort(parseShort(value.substring(0, value.length() - 1))); } else if (patInt.matcher(value).matches()) { return nbtUtils.createTagInt(parseInt(value.substring(0, value.length() - 1))); } else if (patIntDef.matcher(value).matches()) { return nbtUtils.createTagInt(parseInt(value)); } else if (patDoubleDef.matcher(value).matches()) { return nbtUtils.createTagDouble(parseDouble(value)); } else if (value.equalsIgnoreCase("true")) { return nbtUtils.createTagByte((byte) 1); } else if (value.equalsIgnoreCase("false")) { return nbtUtils.createTagByte((byte) 0); } else if (this.value.startsWith("[") && this.value.endsWith("]i")) { String token = value.substring(1, this.value.length() - 2); List<Integer> tempResult = new ArrayList<Integer>(); for (String s : splitter.split(token)) { tempResult.add(parseInt(s.trim())); } int[] result = new int[tempResult.size()]; for (int i = 0; i < result.length; i++) result[i] = tempResult.get(i); return nbtUtils.createTagIntArray(result); } else if (this.value.startsWith("[") && this.value.endsWith("]b")) { String token = value.substring(1, this.value.length() - 2); List<Byte> tempResult = new ArrayList<Byte>(); for (String s : splitter.split(token)) { tempResult.add(parseByte(s.trim())); } byte[] result = new byte[tempResult.size()]; for (int i = 0; i < result.length; i++) result[i] = tempResult.get(i); return nbtUtils.createTagByteArray(result); } else if (this.value.startsWith("[") && this.value.endsWith("]")) { String token = this.value.substring(1, this.value.length() - 1); String[] tokens = Iterables.toArray(splitter.split(token), String.class); int[] result = new int[tokens.length]; for (int i = 0; i < tokens.length; ++i) { result[i] = parseInt(tokens[i].trim()); } return nbtUtils.createTagIntArray(result); } else { if (value.startsWith("\"") && value.endsWith("\"")) { String parseValue = value.substring(1, value.length() - 1); return nbtUtils.createTagString(StringParser.parse(parseValue)); } value = value.replaceAll("\\\\\"", "\""); StringBuilder builder = new StringBuilder(); for (int i = 0; i < value.length(); ++i) { if (i < value.length() - 1 && value.charAt(i) == 92 && value.charAt(i + 1) == 92) { builder.append('\\'); ++i; } else { builder.append(value.charAt(i)); } } return nbtUtils.createTagString(builder); } }