private PackageClass parseClass(Element classNode) { PackageClass newClass = new PackageClass(); _package.getClasses().add(newClass); newClass.setComponentType(PackageClass.ComponentType.getType(classNode.getAttribute(ATR_TYPE))); newClass.setStatic(Boolean.parseBoolean(classNode.getAttribute(ATR_STATIC))); newClass.setName(getElementByName(classNode, EL_NAME).getTextContent()); final String source = getElementStringByName(classNode, EL_FILE); if (source != null) { newClass.setSource(source); } newClass.setTarget(getElementStringByName(classNode, EL_EXTENDS)); newClass.setDescription(getElementByName(classNode, EL_DESCRIPTION).getTextContent()); newClass.setIcon(getElementByName(classNode, EL_ICON).getTextContent()); // parse all variables declared in the corresponding specification if (newClass.getComponentType().hasSpec()) { final String newClassName = newClass.getName(); try { switch (RuntimeProperties.getSpecParserKind()) { case REGEXP: { ClassList classList = new ClassList(); SpecParser.parseSpecClass(newClassName, getWorkingDir(), classList); newClass.setSpecFields(classList.getType(newClassName).getFields()); break; } case ANTLR: { if (specificationLoader == null) { specificationLoader = new SpecificationLoader(new PackageSpecSourceProvider(_package), null); } final AnnotatedClass annotatedClass = specificationLoader.getSpecification(newClassName); newClass.setSpecFields(annotatedClass.getFields()); break; } default: throw new IllegalStateException("Undefined specification language parser"); } } catch (SpecParseException e) { final String msg = "Unable to parse the specification of class " + newClassName; logger.error(msg, e); collector.collectDiagnostic(msg + "\nReason: " + e.getMessage() + "\nLine: " + e.getLine()); } } // Graphics Element grNode = getElementByName(classNode, EL_GRAPHICS); newClass.addGraphics( getGraphicsParser().parse(grNode/*, newClass.getComponentType() == ComponentType.REL*/ )); Element painter; if ((painter = getElementByName(grNode, EL_PAINTER)) != null) { newClass.setPainterName(painter.getTextContent()); } // Ports NodeList ports = classNode.getElementsByTagName(EL_PORT); for (int i = 0; i < ports.getLength(); i++) { parsePort(newClass, (Element) ports.item(i)); } // Fields NodeList fields = classNode.getElementsByTagName(EL_FIELD); for (int i = 0; i < fields.getLength(); i++) { parseField(newClass, (Element) fields.item(i)); } return newClass; }
public void addPackageClass(PackageClass pClass) { Document doc; try { doc = getDocument(); } catch (Exception e) { e.printStackTrace(); return; } String name = pClass.getName(); // check if such class exists and remove duplicates Element rootEl = doc.getDocumentElement(); NodeList classEls = rootEl.getElementsByTagName(EL_CLASS); for (int i = 0; i < classEls.getLength(); i++) { Element nameEl = getElementByName((Element) classEls.item(i), EL_NAME); if (name.equals(nameEl.getTextContent())) { rootEl.removeChild(classEls.item(i)); } } Element classNode = doc.createElement(EL_CLASS); doc.getDocumentElement().appendChild(classNode); classNode.setAttribute(ATR_TYPE, PackageClass.ComponentType.SCHEME.getXmlName()); classNode.setAttribute(ATR_STATIC, "false"); Element className = doc.createElement(EL_NAME); className.setTextContent(name); classNode.appendChild(className); Element desrc = doc.createElement(EL_DESCRIPTION); desrc.setTextContent(pClass.getDescription()); classNode.appendChild(desrc); Element icon = doc.createElement(EL_ICON); icon.setTextContent(pClass.getIcon()); classNode.appendChild(icon); // graphics classNode.appendChild(generateGraphicsNode(doc, pClass.getGraphics())); // ports List<Port> ports = pClass.getPorts(); if (!ports.isEmpty()) { Element portsEl = doc.createElement(EL_PORTS); classNode.appendChild(portsEl); for (Port port : ports) { portsEl.appendChild(generatePortNode(doc, port)); } } // fields Collection<ClassField> fields = pClass.getFields(); if (!fields.isEmpty()) { Element fieldsEl = doc.createElement(EL_FIELDS); classNode.appendChild(fieldsEl); for (ClassField cf : fields) { fieldsEl.appendChild(generateFieldNode(doc, cf)); } } // write try { writeDocument(doc, new FileOutputStream(xmlFile)); } catch (FileNotFoundException e) { e.printStackTrace(); } }
private String getElementStringByName(Element root, String name) { Element element; return (element = getElementByName(root, name)) != null ? element.getTextContent() : null; }