Пример #1
0
  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();
    }
  }