public final void ReadFromBytes(byte[] bytes) { MyBuffer buff = new MyBuffer(bytes); setParametersCount(buff.get()); setParameters(new java.util.ArrayList<ParameterItem>( getParametersCount())); int pos = 1; while (buff.hasRemain()) { ParameterItem item = new ParameterItem(); item.setParameterId(buff.getLong()); pos += 4; item.setParameterLength(buff.get()); pos += 1; String fieldType = GetFieldType((int) item.getParameterId()); // C# TO JAVA CONVERTER NOTE: The following 'switch' operated on a // string member and was converted to Java 'if-else' logic: // switch (fieldType) // ORIGINAL LINE: case "BYTE": if (fieldType.equals("BYTE")) // 参数值为byte类型 { item.setParameterValue(buff.get()); } // ORIGINAL LINE: case "WORD": else if (fieldType.equals("WORD")) // 参数值为ushort类型 { item.setParameterValue(buff.getShort()); } // ORIGINAL LINE: case "DWORD": else if (fieldType.equals("DWORD")) // 参数值为uint类型 { item.setParameterValue(buff.getLong()); } else { byte[] strBytes = buff.gets(item.getParameterLength()); String strValue = BitConverter.getString(strBytes, 0, strBytes.length); item.setParameterValue(strValue); } getParameters().add(item); pos += item.getParameterLength(); } }
private static void parseConfiguration(String stringUrl, Product product) { try { URL url = new URL(stringUrl); URLConnection connection = url.openConnection(); int fileLength = connection.getContentLength(); if (fileLength == -1) { System.out.println("Invalide URL or file."); return; } CommandClass currentCC = null; Parameter currentParameter = null; Association currentAssociation = null; XMLInputFactory inputFactory = XMLInputFactory.newInstance(); InputStream input = connection.getInputStream(); final XMLEventReader eventReader = inputFactory.createXMLEventReader(input); while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isStartElement()) { StartElement startElt = event.asStartElement(); if (startElt.getName().getLocalPart().equals("CommandClass")) { CommandClass cc = product.view().createCommandClass(); Iterator<Attribute> attributes = startElt.getAttributes(); while (attributes.hasNext()) { Attribute next = attributes.next(); String attrName = next.getName().toString(); if (attrName.equals("id")) { cc.setId(Integer.parseInt(next.getValue(), 16)); } } product.addCommandClasses(cc); currentCC = cc; } else if (startElt.getName().getLocalPart().equals("Value")) { Parameter param = product.view().createParameter(); Iterator<Attribute> attributes = startElt.getAttributes(); while (attributes.hasNext()) { Attribute next = attributes.next(); String attrName = next.getName().toString(); if (!next.getValue().equals("")) { if (attrName.equals("type")) { param.setType(ParameterType.valueOf(next.getValue().toUpperCase())); } else if (attrName.equals("genre")) { param.setGenre(next.getValue()); } else if (attrName.equals("instance")) { param.setInstance(Integer.valueOf(next.getValue())); } else if (attrName.equals("index")) { param.setIndex(Integer.valueOf(next.getValue())); } else if (attrName.equals("label")) { param.setLabel(next.getValue()); } else if (attrName.equals("value")) { param.setValue(next.getValue()); } else if (attrName.equals("min")) { param.setMin(Integer.valueOf(next.getValue())); } else if (attrName.equals("max")) { param.setMax(Integer.valueOf(next.getValue())); } else if (attrName.equals("size")) { param.setSize(Integer.valueOf(next.getValue())); } } } currentCC.addParameters(param); currentParameter = param; } else if (startElt.getName().getLocalPart().equals("Help")) { // help } else if (startElt.getName().getLocalPart().equals("Item")) { ParameterItem item = product.view().createParameterItem(); Iterator<Attribute> attributes = startElt.getAttributes(); while (attributes.hasNext()) { Attribute next = attributes.next(); String attrName = next.getName().toString(); if (attrName.equals("label")) { item.setLabel(next.getValue()); } else if (attrName.equals("value")) { item.setValue(Integer.valueOf(next.getValue())); } } currentParameter.addItems(item); } else if (startElt.getName().getLocalPart().equals("Associations")) { Association assoc = product.view().createAssociation(); Iterator<Attribute> attributes = startElt.getAttributes(); while (attributes.hasNext()) { Attribute next = attributes.next(); String attrName = next.getName().toString(); if (attrName.equals("num_groups")) { assoc.setNumGroups(Integer.valueOf(next.getValue())); } } currentCC.addAssociations(assoc); currentAssociation = assoc; } else if (startElt.getName().getLocalPart().equals("Group")) { AssociationGroup group = product.view().createAssociationGroup(); Iterator<Attribute> attributes = startElt.getAttributes(); while (attributes.hasNext()) { Attribute next = attributes.next(); String attrName = next.getName().toString(); if (attrName.equals("label")) { group.setLabel(next.getValue()); } else if (attrName.equals("index")) { group.setIndex(Integer.valueOf(next.getValue())); } else if (attrName.equals("max_associations")) { group.setMaxAssociations(Integer.valueOf(next.getValue())); } else if (attrName.equals("auto")) { group.setAuto(Boolean.valueOf(next.getValue())); } currentAssociation.addGroups(group); } } } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (XMLStreamException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
public final byte[] WriteToBytes() { MyBuffer buff = new MyBuffer(); buff.put(getParametersCount()); for (ParameterItem item : getParameters()) { buff.put(item.getParameterId()); // buff.put(item.ParameterLength); String fieldType = GetFieldType((int) item.getParameterId()); String strParamValue = ""+item.getParameterValue(); if (fieldType.equals("BYTE") && StringUtil.isNullOrEmpty(strParamValue) == false) // 参数值类型为byte { item.setParameterLength((byte)1); buff.put(item.getParameterLength()); buff.put(Byte.parseByte(strParamValue)); } // ORIGINAL LINE: case "WORD": else if (fieldType.equals("WORD") && StringUtil.isNullOrEmpty(strParamValue) == false) // 参数值类型为16位无符号整形数值 { item.setParameterLength((byte)2); buff.put(item.getParameterLength()); buff.put(Short.parseShort(strParamValue)); } // ORIGINAL LINE: case "DWORD": else if (fieldType.equals("DWORD") && StringUtil.isNullOrEmpty(strParamValue) == false) // 参数值类型为32位无符号整形数值 { item.setParameterLength((byte)4); buff.put(item.getParameterLength()); int paramValue1 = Integer.parseInt(strParamValue); buff.put(paramValue1); //buff.put(((Integer) item.getParameterValue()).intValue()); } else // 参数值类型为字符串 { byte[] strBytes =BitConverter.getBytes(item .getParameterValue().toString()); item.setParameterLength((byte) (strBytes.length)); buff.put(item.getParameterLength()); buff.put(strBytes); //buff.put((byte) 0x00); } } return buff.array(); }