protected Document generateAntTask() { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); Document doc = factory.newDocumentBuilder().newDocument(); Element root = doc.createElement("project"); // $NON-NLS-1$ root.setAttribute("name", "build"); // $NON-NLS-1$ //$NON-NLS-2$ root.setAttribute("default", "feature_export"); // $NON-NLS-1$ //$NON-NLS-2$ doc.appendChild(root); Element target = doc.createElement("target"); // $NON-NLS-1$ target.setAttribute("name", "feature_export"); // $NON-NLS-1$ //$NON-NLS-2$ root.appendChild(target); Element export = doc.createElement("pde.exportFeatures"); // $NON-NLS-1$ export.setAttribute("features", getFeatureIDs()); // $NON-NLS-1$ export.setAttribute("destination", fPage.getDestination()); // $NON-NLS-1$ String filename = fPage.getFileName(); if (filename != null) export.setAttribute("filename", filename); // $NON-NLS-1$ export.setAttribute("exportType", getExportOperation()); // $NON-NLS-1$ export.setAttribute("useJARFormat", Boolean.toString(fPage.useJARFormat())); // $NON-NLS-1$ export.setAttribute("exportSource", Boolean.toString(fPage.doExportSource())); // $NON-NLS-1$ if (fPage.doExportSource()) { export.setAttribute( "exportSourceBundle", Boolean.toString(fPage.doExportSourceBundles())); // $NON-NLS-1$ } String qualifier = fPage.getQualifier(); if (qualifier != null) export.setAttribute("qualifier", qualifier); // $NON-NLS-1$ target.appendChild(export); return doc; } catch (DOMException e) { } catch (FactoryConfigurationError e) { } catch (ParserConfigurationException e) { } return null; }
private Element generatePortNode(Document doc, Port port) { Element portEl = doc.createElement(EL_PORT); portEl.setAttribute(ATR_NAME, port.getName()); portEl.setAttribute(ATR_TYPE, port.getType()); portEl.setAttribute(ATR_X, Integer.toString(port.getX())); portEl.setAttribute(ATR_Y, Integer.toString(port.getY())); portEl.setAttribute(ATR_PORT_CONNECTION, port.isArea() ? "area" : ""); portEl.setAttribute(ATR_STRICT, Boolean.toString(port.isStrict())); portEl.setAttribute(ATR_MULTI, Boolean.toString(port.isMulti())); return portEl; }
private Element generateGraphicsNode(Document doc, ClassGraphics gr) { Element graphicsEl = doc.createElement(EL_GRAPHICS); Element bounds = doc.createElement(EL_BOUNDS); graphicsEl.appendChild(bounds); bounds.setAttribute(ATR_X, Integer.toString(gr.getBoundX())); bounds.setAttribute(ATR_Y, Integer.toString(gr.getBoundY())); bounds.setAttribute(ATR_WIDTH, Integer.toString(gr.getBoundWidth())); bounds.setAttribute(ATR_HEIGHT, Integer.toString(gr.getBoundHeight())); for (Shape shape : gr.getShapes()) { if (shape instanceof Rect) { Rect rect = (Rect) shape; Element rectEl = doc.createElement(EL_RECT); graphicsEl.appendChild(rectEl); rectEl.setAttribute(ATR_X, Integer.toString(rect.getX())); rectEl.setAttribute(ATR_Y, Integer.toString(rect.getY())); rectEl.setAttribute(ATR_WIDTH, Integer.toString(rect.getWidth())); rectEl.setAttribute(ATR_HEIGHT, Integer.toString(rect.getHeight())); rectEl.setAttribute(ATR_COLOUR, Integer.toString(rect.getColor().getRGB())); rectEl.setAttribute(ATR_FILLED, Boolean.toString(rect.isFilled())); rectEl.setAttribute(ATR_FIXED, Boolean.toString(rect.isFixed())); rectEl.setAttribute(ATR_STROKE, Float.toString(rect.getStroke().getLineWidth())); rectEl.setAttribute(ATR_LINETYPE, Float.toString(rect.getLineType())); rectEl.setAttribute(ATR_TRANSPARENCY, Integer.toString(rect.getTransparency())); } else if (shape instanceof Text) { Text text = (Text) shape; Element textEl = doc.createElement(EL_TEXT); textEl.setAttribute(ATR_STRING, text.getText()); textEl.setAttribute(ATR_X, Integer.toString(text.getX())); textEl.setAttribute(ATR_Y, Integer.toString(text.getY())); textEl.setAttribute(ATR_FONTNAME, text.getFont().getName()); textEl.setAttribute(ATR_FONTSIZE, Integer.toString(text.getFont().getSize())); textEl.setAttribute(ATR_FONTSTYLE, Integer.toString(text.getFont().getStyle())); textEl.setAttribute(ATR_TRANSPARENCY, Integer.toString(text.getTransparency())); textEl.setAttribute(ATR_COLOUR, Integer.toString(text.getColor().getRGB())); graphicsEl.appendChild(textEl); } // TODO handle the rest of shapes } return graphicsEl; }
public void setFeature(String name, boolean value) throws ParserConfigurationException { // fact.setFeature(name, value); try { invoke("setFeature", argsSetFeature, new Object[] {name, Boolean.valueOf(value)}); } catch (InvocationTargetException e) { if (e.getCause() instanceof ParserConfigurationException) { throw (ParserConfigurationException) e.getCause(); } if (e.getCause() instanceof RuntimeException) { throw (RuntimeException) e.getCause(); } throw new RuntimeException(e); } }
// read enemy launchers and their missiles form XML protected void readEnemyLaunchers() { NodeList launchers = root.getElementsByTagName("launcher"); for (int i = 0; i < launchers.getLength(); i++) { Element tempLauncher = (Element) launchers.item(i); String idLauncher = tempLauncher.getAttribute("id"); boolean isHidden = Boolean.parseBoolean(tempLauncher.getAttribute("isHidden")); // add to the war war.addEnemyLauncher(idLauncher, isHidden); IdGenerator.updateEnemyLauncherId(idLauncher); NodeList missiles = tempLauncher.getElementsByTagName("missile"); // read all missiles readMissilesForGivenLauncher(missiles, idLauncher); } // update the id's in the war IdGenerator.updateFinalEnemyMissileId(); IdGenerator.updateFinalEnemyLauncherId(); }
// реализуем метод парсинга @Override public List<Root.News> parse(File fileToParse) { try { // создаем фабрику документов, в наш документ парсим файл DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); // парсим файл в документ dom = db.parse(fileToParse); } catch (SAXException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } catch (Exception e) { System.out.println(e.getMessage()); } // root элемент Element root = dom.getDocumentElement(); // NodeList новостей NodeList elementsNodeList = root.getElementsByTagName("news").item(0).getChildNodes(); // перебираем все элементы for (int i = 0; i < elementsNodeList.getLength(); i++) { Node item = elementsNodeList.item(i); if (item.getNodeType() != Node.ELEMENT_NODE) { // если вернуло что-то не то continue; } Element element = (Element) item; Root.News myNews = new Root.News(); // дата String tempDate = element.getElementsByTagName("date").item(0).getTextContent(); SimpleDateFormat format = new SimpleDateFormat(Constants.DATE_FORMAT_IN); Date date = null; try { date = format.parse(tempDate); } catch (ParseException e) { e.printStackTrace(); } // описание String description = element.getElementsByTagName("description").item(0).getTextContent(); // id int id = Integer.parseInt(element.getElementsByTagName("id").item(0).getTextContent()); // ключевые слова List<String> keywordsList = new ArrayList<String>(); // перебираем ключевые слова NodeList keywords = element.getElementsByTagName("keywords").item(0).getChildNodes(); for (int j = 0; j < keywords.getLength(); j++) { Node itemKey = keywords.item(j); if (itemKey.getNodeType() != Node.ELEMENT_NODE) { // если вернуло что-то не то continue; } keywordsList.add(itemKey.getFirstChild().getTextContent()); } // заголовок String title = element.getElementsByTagName("title").item(0).getTextContent(); // буленовская переменная boolean isVisible = Boolean.parseBoolean(element.getElementsByTagName("visible").item(0).getTextContent()); // записываем данные в экземпляр новости myNews.setDescription(description); myNews.setDate(date); myNews.setTitle(title); myNews.setId(id); myNews.setKeywords(keywordsList); myNews.setVisible(isVisible); // добавляем новость в список news.add(myNews); } System.out.println("---> Парсинг файла " + "\"" + fileToParse + "\" завершен!"); return news; }
private ClassGraphics parse(Element grNode /*, boolean isRelation*/) { ClassGraphics newGraphics = new ClassGraphics(); newGraphics.setShowFields(Boolean.parseBoolean(grNode.getAttribute(ATR_SHOW_FIELDS))); // newGraphics.setRelation( isRelation ); NodeList list = grNode.getChildNodes(); for (int k = 0; k < list.getLength(); k++) { if (list.item(k).getNodeType() != Node.ELEMENT_NODE) continue; Element node = (Element) list.item(k); String nodeName = node.getNodeName(); Shape shape = null; if (EL_BOUNDS.equals(nodeName)) { Dim dim = getDim(node); newGraphics.setBounds(dim.x, dim.y, dim.width, dim.height); continue; } else if (EL_LINE.equals(nodeName)) { shape = makeLine(node, newGraphics); } else if (EL_RECT.equals(nodeName)) { Dim dim = getDim(node); Lineprops lp = getLineProps(node); shape = new Rect( dim.x, dim.y, dim.width, dim.height, getColor(node), isShapeFilled(node), lp.strokeWidth, lp.lineType); } else if (EL_OVAL.equals(nodeName)) { Dim dim = getDim(node); Lineprops lp = getLineProps(node); shape = new Oval( dim.x, dim.y, dim.width, dim.height, getColor(node), isShapeFilled(node), lp.strokeWidth, lp.lineType); } else if (EL_ARC.equals(nodeName)) { Dim dim = getDim(node); Lineprops lp = getLineProps(node); int startAngle = Integer.parseInt(node.getAttribute(ATR_START_ANGLE)); int arcAngle = Integer.parseInt(node.getAttribute(ATR_ARC_ANGLE)); shape = new Arc( dim.x, dim.y, dim.width, dim.height, startAngle, arcAngle, getColor(node), isShapeFilled(node), lp.strokeWidth, lp.lineType); } else if (EL_POLYGON.equals(nodeName)) { Lineprops lp = getLineProps(node); Polygon polygon = new Polygon(getColor(node), isShapeFilled(node), lp.strokeWidth, lp.lineType); // points NodeList points = node.getElementsByTagName(EL_POINT); int pointCount = points.getLength(); // arrays of polygon points int[] xs = new int[pointCount]; int[] ys = new int[pointCount]; // arrays of FIXED information about polygon points int[] fxs = new int[pointCount]; int[] fys = new int[pointCount]; int width = newGraphics.getBoundWidth(); int height = newGraphics.getBoundHeight(); for (int j = 0; j < pointCount; j++) { FixedCoords fc = getFixedCoords((Element) points.item(j), width, height, null); xs[j] = fc.x; fxs[j] = fc.fx; ys[j] = fc.y; fys[j] = fc.fy; } polygon.setPoints(xs, ys, fxs, fys); shape = polygon; } else if (EL_IMAGE.equals(nodeName)) { Dim dim = getDim(node); // image path should be relative to the package xml String imgPath = node.getAttribute(ATR_PATH); String fullPath = FileFuncs.preparePathOS(getWorkingDir() + imgPath); shape = new Image(dim.x, dim.y, fullPath, imgPath, isShapeFixed(node)); } else if (EL_TEXT.equals(nodeName)) { shape = makeText(node, newGraphics); /* * if (str.equals("*self")) newText.name = "self"; else if * (str.equals("*selfWithName")) newText.name = "selfName"; */ } if (shape != null) newGraphics.addShape(shape); } return newGraphics; }
private boolean isShapeFixed(Element shape) { return Boolean.parseBoolean(shape.getAttribute(ATR_FIXED)); }
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; }
public abstract class FileSystemOpBase extends ListOpBase { protected String getOpType() { String type = getClass().getName(); type = type.substring(type.lastIndexOf('.') + 1); return "disk-" + type; } protected String getConnectorPath() { return Properties.get(Names.ENV_DIR) + Names.PATH_SEPARATOR + "bin" + Names.PATH_SEPARATOR + "connector"; } protected String getEXEPath() { return getConnectorPath() + Names.PATH_SEPARATOR + getEXEName(); } protected abstract String getEXEName(); protected void initExecution() throws Exception {} protected void finishExecution() throws Exception {} protected void createInstructionFile(String filename) {} public FileSystemResultHandler getXMLResultHandler() { return null; } public void execute() throws Exception { getNewCount(); File executable = new File(getEXEPath()); if (!executable.exists()) throw new FileNotFoundException("executable file does not exist! " + executable); workingDir = getWorkingDirectory(executable); initExecution(); instruction = getInstructionFileName(workingDir); output = getOutputFileName(workingDir); createInstructionFile(instruction); // Profiler pofiler = new Profiler(); // profiler.start("exec-ilink-search " + xmlFileName, "exec-ilink-search " + xmlFileName); ExecShell shell = new ExecShell(); shell.setExecutable(executable.getAbsolutePath()); shell.setWorkingDirectory(workingDir); setArguments(shell); shell.addCommandLineArgument(executable.getParent()); shell.execute(); // profiler.start("exec-ilink-search " + xmlFileName, "shell-wait-for"); exitCode = shell.timeout(timeout); // profiler.stop("exec-ilink-search " + xmlFileName, "shell-wait-for"); // instructionFile.delete(); handleResponse(output); finishExecution(); if (zws.Server.debugMode()) return; // cleanup File[] contents = workingDir.listFiles(); for (int idx = 0; idx < contents.length; contents[idx++].delete()) ; workingDir.delete(); } protected void setArguments(ExecShell shell) { shell.addCommandLineArgument(instruction); shell.addCommandLineArgument(output); } private File getWorkingDirectory(File f) { try { File workingDir = new File( Properties.get(Names.WORKING_DIR) + Names.PATH_SEPARATOR + getOpType() + Names.PATH_SEPARATOR + "work" + getNewCount()); if (!workingDir.exists()) workingDir.mkdirs(); FileUtil.deleteContents(workingDir); return workingDir; } catch (Exception ignore) { // ++ notify of error - maybe out of disk space ignore.printStackTrace(); return null; } } private static int count = 0; private static synchronized int getNewCount() { return ++count; } protected String getInstructionFileName(File f) { return f.getAbsolutePath() + Names.PATH_SEPARATOR + "instruction.xml"; } protected String getOutputFileName(File f) { return f.getAbsolutePath() + Names.PATH_SEPARATOR + "response.xml"; } public int getExitCode() { return exitCode; } public FileSystemSource getDatasource() { return datasource; } public void setDatasource(FileSystemSource s) { datasource = s; } public void setAuthentication(Authentication a) { authentication = a; } public Authentication getAuthentication() { return authentication; } public String getUsername() { if (null != authentication) return authentication.getUsername(); return datasource.getDefaultUsername(); } public String getPassword() { if (null != authentication) return authentication.getPassword(); return datasource.getDefaultPassword(); } public void setDeleteOutput(boolean b) { deleteOutput = b; } public boolean getDeleteOutput() { return deleteOutput; } public void setStorable(Storable s) { storable = s; } protected void handleResponse(String outputFilename) throws Exception { FileSystemResultHandler handler = getXMLResultHandler(); // if (null==handler) throw new Exception("XML Result Handler not specified for " + // getClass().getName()); if (null == handler) { { } // Logwriter.printOnConsole("+++todo - XML Result Handler not specified for " + // getClass().getName()); return; } /* UTF8Tidy tidy = new UTF8Tidy(); tidy.setFilename(outputFilename); tidy.execute(); */ XMLReader xr = getParser(false).getXMLReader(); if (null == storable) storable = this; storable.throwOnFailure(false); handler.setStorable(storable); handler.setDatasource(getDatasource()); xr.setContentHandler(handler); xr.setErrorHandler(handler); File xml = new File(outputFilename); FileReader r = new FileReader(xml); xr.parse(new InputSource(r)); resetStorage(); initializeStorage(handler.getResults()); } protected static SAXParser getParser(boolean validate) throws SAXException, ParserConfigurationException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(validate); return factory.newSAXParser(); } private Storable storable = null; private Authentication authentication = null; private FileSystemSource datasource; private int exitCode = -999; private boolean deleteOutput = !Boolean.valueOf(Properties.get(Names.DEBUG_MODE)).booleanValue(); private String instruction = null; private String output = null; private long timeout = 600000; // 10 minutes protected File workingDir = null; }