@Override public VPackage parse() { logger.debug("Starting parsing package: " + xmlFile.getAbsolutePath()); long startParsing = System.currentTimeMillis(); try { Document document = getDocument(); Element root = document.getDocumentElement(); _package = new VPackage(xmlFile); Node name = root.getElementsByTagName(EL_NAME).item(0); _package.setName(name.getTextContent()); Node descr = root.getElementsByTagName(EL_DESCRIPTION).item(0); _package.setDescription(descr.getTextContent()); NodeList list = root.getElementsByTagName(EL_CLASS); boolean initPainters = false; for (int i = 0; i < list.getLength(); i++) { PackageClass pc = parseClass((Element) list.item(i)); if (pc.getPainterName() != null) { initPainters = true; } } if (initPainters) { _package.initPainters(); } logger.info( "Parsing the package '{}' finished in {}ms.\n", _package.getName(), (System.currentTimeMillis() - startParsing)); } catch (Exception e) { collector.collectDiagnostic(e.getMessage(), true); if (RuntimeProperties.isLogDebugEnabled()) { e.printStackTrace(); } } try { checkProblems("Error parsing package file " + xmlFile.getName()); } catch (Exception e) { return null; } return _package; }
@Override public String getName() { return packageClass.getCanonicalName(); }
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 void parsePort(PackageClass newClass, Element portNode) { String name = portNode.getAttribute(ATR_NAME); String type = portNode.getAttribute(ATR_TYPE); String x = portNode.getAttribute(ATR_X); String y = portNode.getAttribute(ATR_Y); String portConnection = portNode.getAttribute(ATR_PORT_CONNECTION); String strict = portNode.getAttribute(ATR_STRICT); String multi = portNode.getAttribute(ATR_MULTI); ClassField cf = newClass.getSpecField(name); if (newClass.getComponentType().hasSpec()) { if (name.indexOf(".") > -1) { // TODO - temporarily do not dig into hierarchy int idx = name.indexOf("."); String root = name.substring(0, idx); if (newClass.getSpecField(root) == null) { collector.collectDiagnostic( "Field " + root + " in class " + newClass.getName() + " is not declared in the specification, variable " + type + " " + name + " ignored "); return; } newClass.addSpecField(new ClassField(name, type)); } else if (!TypeUtil.TYPE_THIS.equalsIgnoreCase(name)) { if (cf == null) { collector.collectDiagnostic( "Port " + type + " " + name + " in class " + newClass.getName() + " does not have the corresponding field in the specification"); } else if (!cf.getType().equals(type) // type may be declared as "alias", however cf.getType() returns e.g. "double[]", ignore // it && !(cf.isAlias() && TypeUtil.TYPE_ALIAS.equals(type))) { collector.collectDiagnostic( "Port " + type + " " + name + " in class " + newClass.getName() + " does not match the field declared in the specification: " + cf.getType() + " " + cf.getName()); } } } Port newPort = new Port( name, type, Integer.parseInt(x), Integer.parseInt(y), portConnection, Boolean.parseBoolean(strict), Boolean.parseBoolean(multi)); if (portNode.hasAttribute(ATR_ID)) newPort.setId(portNode.getAttribute(ATR_ID)); Element gr; // open if ((gr = getElementByName(portNode, EL_OPEN)) != null && (gr = getElementByName(gr, EL_GRAPHICS)) != null) { newPort.setOpenGraphics(getGraphicsParser().parse(gr)); } // closed if ((gr = getElementByName(portNode, EL_CLOSED)) != null && (gr = getElementByName(gr, EL_GRAPHICS)) != null) { newPort.setClosedGraphics(getGraphicsParser().parse(gr)); } newClass.addPort(newPort); }
private void parseField(PackageClass newClass, Element fieldNode) { String name = fieldNode.getAttribute(ATR_NAME); String type = fieldNode.getAttribute(ATR_TYPE); ClassField newField; if (newClass.getComponentType().hasSpec()) { if (name.indexOf(".") > -1) { // TODO - temporarily do not dig into hierarchy int idx = name.indexOf("."); String root = name.substring(0, idx); if (newClass.getSpecField(root) == null) { collector.collectDiagnostic( "Field " + root + " in class " + newClass.getName() + " is not declared in the specification, variable " + type + " " + name + " ignored "); return; } newField = new ClassField(name, type); newClass.addSpecField(newField); } else { newField = newClass.getSpecField(name); if (newField == null) { collector.collectDiagnostic( "Field " + type + " " + name + " in class " + newClass.getName() + " is not declared in the specification"); return; } else if (!newField.getType().equals(type)) { collector.collectDiagnostic( "Field " + type + " " + name + " in class " + newClass.getName() + " does not match the field declared in the specification: " + newField.getType() + " " + newField.getName()); return; } } } else { newField = new ClassField(name, type); newClass.addSpecField(newField); } newField.setValue(fieldNode.hasAttribute(ATR_VALUE) ? fieldNode.getAttribute(ATR_VALUE) : null); newField.setDescription(fieldNode.getAttribute(ATR_DESCRIPTION)); newField.setHidden(Boolean.parseBoolean(fieldNode.getAttribute(ATR_HIDDEN))); String nature = fieldNode.getAttribute(ATR_NATURE); if ("input".equals(nature)) newField.setInput(true); else if ("goal".equals(nature)) newField.setGoal(true); newClass.addField(newField); Element gr; // known if ((gr = getElementByName(fieldNode, EL_KNOWN)) != null && (gr = getElementByName(gr, EL_GRAPHICS)) != null) { newField.setKnownGraphics(getGraphicsParser().parse(gr)); } // default if ((gr = getElementByName(fieldNode, EL_DEFAULT)) != null && (gr = getElementByName(gr, EL_GRAPHICS)) != null) { newField.setDefaultGraphics(getGraphicsParser().parse(gr)); } }
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; }