public Namespace readNamespace(String path) { try { File fXmlFile = new File(path); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); String topTag = doc.getDocumentElement().getNodeName(); if (topTag.equals("Namespace")) { Namespace result = createNamespace(doc.getDocumentElement()); for (ForwardDeclaration decl : forwardDeclarationMap.keySet()) { decl.setDeclaration((Declaration) findIrObject(forwardDeclarationMap.get(decl))); } return result; } else { throw new RuntimeException("Invalid top tag in XML ir document"); } // As a last step, patch the forward declarations } catch (Exception e) { System.err.println("[IrXmlReader. Error reading '" + path + "' message = " + e.getMessage()); e.printStackTrace(); return null; } }
public AbstractActor readActor(String path) { try { File fXmlFile = new File(path); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); String topTag = doc.getDocumentElement().getNodeName(); AbstractActor result; if (topTag.equals("Actor")) { result = createActor(doc.getDocumentElement()); } else if (topTag.equals("Network")) { result = createNetwork(doc.getDocumentElement()); } else if (topTag.equals("ExternalActor")) { result = createExternalActor(doc.getDocumentElement()); } else { throw new RuntimeException("Invalid top tag in XML ir document"); } // As a last step, patch the forward declarations for (ForwardDeclaration decl : forwardDeclarationMap.keySet()) { decl.setDeclaration((Declaration) findIrObject(forwardDeclarationMap.get(decl))); } return result; } catch (Exception x) { System.err.println("[ActorDirectory]ÊError reading '" + path + "' x " + x.getMessage()); return null; } }
private Declaration createDeclaration(Element element) { String kind = element.getAttribute("kind"); if (kind.equals("Variable")) { Variable variable = IrFactory.eINSTANCE.createVariable(); variable.setName(element.getAttribute("name")); String id = element.getAttribute("id"); variable.setId(id); doAnnotations(variable, element); addIrObject(id, variable); variable.setScope((Scope) findIrObject(element.getAttribute("scope"))); if (element.getAttribute("constant").equals("true")) { variable.setConstant(true); } else { variable.setConstant(false); } if (element.getAttribute("parameter").equals("true")) { variable.setParameter(true); } else { variable.setParameter(false); } Element typeElement = getChild(element, "Type"); Type type = createType(typeElement); variable.setType(type); Element initialValueElement = getChild(element, "InitialValue"); if (initialValueElement != null) { Expression initalValue = createExpression(getChild(initialValueElement, "Expr")); variable.setInitValue(initalValue); } return variable; } else if (kind.equals("VariableExternal")) { VariableExternal variableExternal = IrFactory.eINSTANCE.createVariableExternal(); variableExternal.setName(element.getAttribute("name")); String id = element.getAttribute("id"); variableExternal.setId(id); doAnnotations(variableExternal, element); addIrObject(id, variableExternal); variableExternal.setScope((Scope) findIrObject(element.getAttribute("scope"))); Element typeElement = getChild(element, "Type"); Type type = createType(typeElement); variableExternal.setType(type); return variableExternal; } else if (kind.equals("VariableImport")) { VariableImport variableImport = IrFactory.eINSTANCE.createVariableImport(); variableImport.setName(element.getAttribute("name")); String id = element.getAttribute("id"); variableImport.setId(id); doAnnotations(variableImport, element); addIrObject(id, variableImport); for (String s : Util.unpackQualifiedName(element.getAttribute("namespace"))) { variableImport.getNamespace().add(s); } return variableImport; } else if (kind.equals("TypeImport")) { TypeDeclarationImport typeImport = IrFactory.eINSTANCE.createTypeDeclarationImport(); typeImport.setName(element.getAttribute("name")); String id = element.getAttribute("id"); typeImport.setId(id); doAnnotations(typeImport, element); addIrObject(id, typeImport); for (String s : Util.unpackQualifiedName(element.getAttribute("namespace"))) { typeImport.getNamespace().add(s); } return typeImport; } else if (kind.equals("Forward")) { ForwardDeclaration forwardDeclaration = IrFactory.eINSTANCE.createForwardDeclaration(); forwardDeclaration.setName(element.getAttribute("name")); String id = element.getAttribute("id"); forwardDeclaration.setId(id); doAnnotations(forwardDeclaration, element); addIrObject(id, forwardDeclaration); forwardDeclaration.setScope((Scope) findIrObject(element.getAttribute("scope"))); Element typeElement = getChild(element, "Type"); Type type = createType(typeElement); forwardDeclaration.setType(type); // Since the declaration that the ForwardDeclaration // is pointing at does not yet exists. That value must // be set in a second pass after the whole actor/namespace // is evaluted. The id of the actual declaration is stored // temprary map. forwardDeclarationMap.put(forwardDeclaration, element.getAttribute("forward-id")); return forwardDeclaration; } else if (kind.equals("Type")) { TypeDeclaration typeDeclaration = IrFactory.eINSTANCE.createTypeDeclaration(); String id = element.getAttribute("id"); typeDeclaration.setId(id); doAnnotations(typeDeclaration, element); addIrObject(id, typeDeclaration); typeDeclaration.setName(element.getAttribute("name")); typeDeclaration.setScope((Scope) findIrObject(element.getAttribute("scope"))); Element typeElement = getChild(element, "Type"); Type type = createType(typeElement); typeDeclaration.setType(type); Element ctorElement = getChild(element, "TypeConstructor"); TypeConstructor ctor = IrFactory.eINSTANCE.createTypeConstructor(); ctor.setName(ctorElement.getAttribute("name")); ctor.setId(ctorElement.getAttribute("id")); doAnnotations(ctor, ctorElement); ctor.setScope((Scope) findIrObject(ctorElement.getAttribute("scope"))); ctor.setTypedef(typeDeclaration); typeDeclaration.setConstructor(ctor); return typeDeclaration; } assert (false); return null; }