public static Element getAtomicSoundComponentElement( Document doc, AtomicSoundComponent atomicSoundComponent, String elementName, int componentCounter) { Element atomicSoundComponentElement = doc.createElement(elementName); atomicSoundComponentElement.setAttribute("Type", atomicSoundComponent.getType()); atomicSoundComponentElement.setAttribute("Name", atomicSoundComponent.getName()); atomicSoundComponentElement.setAttribute("Id", Integer.toString(componentCounter)); Element properties = doc.createElement("Properties"); // integer properties for (int i = 0; i < atomicSoundComponent.getIntegerProperties().size(); i++) { Element integerProperty = doc.createElement("IntProperty"); integerProperty.setAttribute( "Name", atomicSoundComponent.getIntegerProperties().get(i).getKey().toString()); integerProperty.setAttribute( "Value", atomicSoundComponent.getIntegerProperties().get(i).getValue().toString()); properties.appendChild(integerProperty); } // float properties for (int i = 0; i < atomicSoundComponent.getFloatProperties().size(); i++) { Element floatProperty = doc.createElement("FloatProperty"); floatProperty.setAttribute( "Name", atomicSoundComponent.getFloatProperties().get(i).getKey().toString()); floatProperty.setAttribute( "Value", atomicSoundComponent.getFloatProperties().get(i).getValue().toString()); properties.appendChild(floatProperty); } // boolean properties for (int i = 0; i < atomicSoundComponent.getBooleanProperties().size(); i++) { Element booleanProperty = doc.createElement("BoolProperty"); booleanProperty.setAttribute( "Name", atomicSoundComponent.getBooleanProperties().get(i).getKey().toString()); booleanProperty.setAttribute( "Value", atomicSoundComponent.getBooleanProperties().get(i).getValue().toString()); properties.appendChild(booleanProperty); } // user string properties for (int i = 0; i < atomicSoundComponent.getUserStringProperties().size(); i++) { Element userStringProperty = doc.createElement("UserStringProperty"); userStringProperty.setAttribute( "Name", atomicSoundComponent.getUserStringProperties().get(i).getKey().toString()); userStringProperty.setAttribute( "Value", atomicSoundComponent.getUserStringProperties().get(i).getValue().toString()); properties.appendChild(userStringProperty); } // string property - impl type for (int i = 0; i < atomicSoundComponent.getStringProperties().size(); i++) { if (atomicSoundComponent.getStringProperties().get(i).getKey().equals("implType")) { Element stringProperty = doc.createElement("StringProperty"); stringProperty.setAttribute( "Name", atomicSoundComponent.getStringProperties().get(i).getKey().toString()); stringProperty.setAttribute( "Value", atomicSoundComponent.getStringProperties().get(i).getValue().toString()); properties.appendChild(stringProperty); } } atomicSoundComponentElement.appendChild(properties); return atomicSoundComponentElement; }
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; }