Ejemplo n.º 1
0
  public static void createPatchFromXML(String newFilePath, String patchFile, String newFileName)
      throws IOException {
    Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
    Map<String, Object> m = reg.getExtensionToFactoryMap();
    m.put(".sgd", new SoundgatesFactoryImpl());

    Patch patch = getPatchXML(patchFile);

    if (patch != null) {
      ResourceSet resSet = new ResourceSetImpl();
      Resource resource = resSet.createResource(URI.createFileURI(newFilePath));
      resource.getContents().add(patch);

      Diagram diag =
          ViewService.createDiagram(
              patch,
              PatchEditPart.MODEL_ID,
              SoundgatesDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);

      resource.getContents().add(diag);

      resource.save(Collections.EMPTY_MAP);

      MessageDialogs.patchtWasImported(newFileName);
    }
  }
Ejemplo n.º 2
0
  public static Patch getPatchXML(String filePath) {
    try {

      File file = new File(filePath);

      DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document doc = dBuilder.parse(file);

      Element patchElement = (Element) doc.getElementsByTagName("Patch").item(0);
      Patch patch = SoundgatesFactory.eINSTANCE.createPatch();

      Element elements = ((Element) patchElement.getElementsByTagName("Elements").item(0));

      NodeList atomicSoundComponents = elements.getElementsByTagName("AtomicSoundComponent");
      NodeList compositeSoundComponents = elements.getElementsByTagName("CompositeSoundComponent");
      NodeList links = elements.getElementsByTagName("Link");

      HashMap<Integer, SoundComponent> soundComponentsMapping =
          new HashMap<Integer, SoundComponent>();

      for (int i = 0; i < atomicSoundComponents.getLength(); i++) {
        Element atomicElement = (Element) atomicSoundComponents.item(i);

        AtomicSoundComponent atomicSoundComponent = null;
        try {
          atomicSoundComponent =
              AtomicSoundComponentLibrary.getInstance()
                  .createAtomicSoundComponentInstance(atomicElement.getAttribute("Type"));
        } catch (Exception e) {
          MessageDialogs.atomicComponentMissing(atomicElement.getAttribute("Type"));
          return null;
        }

        soundComponentsMapping.put(
            Integer.parseInt(atomicElement.getAttribute("Id")), atomicSoundComponent);

        atomicSoundComponent.setName(atomicElement.getAttribute("Name"));

        // walkaround
        atomicSoundComponent.getStringProperties().put("Type", atomicElement.getAttribute("Type"));

        Element propertiesNode = (Element) atomicElement.getElementsByTagName("Properties").item(0);
        XMLHandler.addPropertiesToSoundComponentFromPropertiesElement(
            atomicSoundComponent, propertiesNode, "Value");

        patch.getElements().add(atomicSoundComponent);
      }

      for (int i = 0; i < compositeSoundComponents.getLength(); i++) {
        Element compositeElement = (Element) compositeSoundComponents.item(i);

        CompositeSoundComponent compositeSoundComponent = null;
        try {
          compositeSoundComponent =
              CompositeSoundComponentLibrary.getInstance()
                  .createCompositeSoundComponentInstance(compositeElement.getAttribute("Name"));
        } catch (Exception e) {
          MessageDialogs.compositeComponentMissing(compositeElement.getAttribute("Name"));
          return null;
        }

        soundComponentsMapping.put(
            Integer.parseInt(compositeElement.getAttribute("Id")), compositeSoundComponent);

        compositeSoundComponent.setName(compositeElement.getAttribute("Name"));

        patch.getElements().add(compositeSoundComponent);
      }

      for (int i = 0; i < links.getLength(); i++) {
        Element linkElement = (Element) links.item(i);

        Link link = SoundgatesFactory.eINSTANCE.createLink();

        SoundComponent sourceComponent =
            soundComponentsMapping.get(
                Integer.parseInt(linkElement.getAttribute("SourceComponent")));
        SoundComponent targetComponent =
            soundComponentsMapping.get(
                Integer.parseInt(linkElement.getAttribute("TargetComponent")));

        Port sourcePort =
            findPort(sourceComponent.getPorts(), linkElement.getAttribute("SourcePort"));
        Port targetPort =
            findPort(targetComponent.getPorts(), linkElement.getAttribute("TargetPort"));
        link.setSource(sourcePort);
        link.setTarget(targetPort);

        patch.getElements().add(link);
      }
      return patch;
    } catch (NullPointerException e) {
      // TODO Differenziertere Exceptions
      System.out.println("Malformed XML Document!");
      e.printStackTrace();
    } catch (ParserConfigurationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SAXException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return null;
  }