public String getQueryParameterFilter(MarrsQuery mq, String key) { String q = mq.getQueryText(); Pattern pat = Pattern.compile("\\$\\{" + key + "(\\|([\\w-]+))?\\}"); Matcher mat = pat.matcher(q); if (mat.find()) { return mat.group(2); } else return null; }
/** * Gets a set of query substitution parameters. The resulting map contains the parameters as keys, * and the (optional) filters as values */ public Map<String, String> getQueryParameters(MarrsQuery mq) { Map<String, String> result = new HashMap<String, String>(); String q = mq.getQueryText(); Pattern pat = Pattern.compile("\\$\\{(\\w+)(\\|([\\w-]+))?\\}"); Matcher mat = pat.matcher(q); while (mat.find()) { String key = mat.group(1); String filter = mat.group(3); result.put(key, filter); } return result; }
public void saveToFile(File dest) throws IOException { File temp = File.createTempFile(dest.getName(), ".tmp", dest.getParentFile()); try { Element root = new Element("MarrsProject"); root.setAttribute("title", title); root.setAttribute("schemaversion", "" + CURRENT_SCHEMAVERSION); for (MarrsQuery q : rows) { Element queryElt = new Element("Query"); CDATA data = new CDATA(q.getQueryText()); queryElt.addContent(data); queryElt.setAttribute("title", q.getTitle()); queryElt.setAttribute("type", "" + q.getQueryType()); if (q.getAskBefore() != null) { Element askElt = new Element("AskBefore"); askElt.setAttribute("key", q.getAskBefore()); queryElt.addContent(askElt); } for (Map.Entry<String, String> context : q.getContext().entrySet()) { Element contextElt = new Element("Context"); contextElt.setAttribute( "key", context.getKey()); // for now, we only use "type" to filter context, so this is // hardcoded. contextElt.setAttribute("value", context.getValue()); queryElt.addContent(contextElt); } for (String var : q.getPostProcessingVars()) { Element processElt = new Element("PostProcessing"); processElt.setAttribute( "var", var); // for now, we only use "type" to filter context, so this is hardcoded. processElt.setAttribute("operation", q.getPostProcessingOperation(var)); queryElt.addContent(processElt); } root.addContent(queryElt); } for (Map.Entry<String, String> param : parameters.entrySet()) { Element paramElt = new Element("Param"); paramElt.setAttribute("key", param.getKey()); paramElt.setAttribute("val", param.getValue()); root.addContent(paramElt); } for (NodeAttribute attr : nodeAttributes) { Element nodeAttr = new Element("NodeAttribute"); nodeAttr.setAttribute("key", attr.key); nodeAttr.setAttribute("value", attr.value); for (Map.Entry<String, String> e : attr.vizprops.entrySet()) { Element vizprop = new Element("Vizmap"); vizprop.setAttribute("prop", e.getKey()); vizprop.setAttribute("value", e.getValue()); nodeAttr.addContent(vizprop); } root.addContent(nodeAttr); } Document doc = new Document(root); XMLOutputter xout = new XMLOutputter(); Format format = Format.getPrettyFormat(); format.setIndent("\t"); format.setLineSeparator("\n"); format.setTextMode(TextMode.PRESERVE); xout.setFormat(format); xout.output(doc, new FileOutputStream(temp)); temp.renameTo(dest); dirty = false; } finally { temp.delete(); } }
public static MarrsProject createFromFile(InputSource in) throws JDOMException, IOException { List<String> warnings = new ArrayList<String>(); SAXBuilder builder = new SAXBuilder(false); // no validation when reading the xml file // try to read the file; if an error occurs, catch the exception and print feedback // build JDOM tree Document doc = builder.build(in); // Copy the pathway information to a VPathway Element root = doc.getRootElement(); if (!root.getName().equals("MarrsProject")) { throw new IllegalArgumentException("Not a MarrsProject file"); } MarrsProject project = new MarrsProject(); project.title = root.getAttributeValue("title"); Double version = StringUtils.safeParseDouble(root.getAttributeValue("schemaversion")); if (version == null || version > CURRENT_SCHEMAVERSION) { warnings.add( "WARNING: project file version is higher than this plugin version. You'll have to upgrade the plugin to make full use of any new features."); } // if missing, may be null. String values are allowed... project.pubVersion = root.getAttributeValue("pubversion"); for (Object o : root.getChildren("Query")) { Element eQuery = (Element) o; String title = eQuery.getAttributeValue("title"); String typeName = eQuery.getAttributeValue("type"); QueryType found = findQueryType(typeName); if (found == null) { warnings.add("WARNING: Parse error, query type " + typeName + " unknown. Skipping query"); continue; } String queryText = eQuery.getText(); MarrsQuery q = new MarrsQuery(title, queryText, found); for (Object context : eQuery.getChildren("Context")) { q.setContextQuery(true); Element eContext = (Element) context; String key = eContext.getAttributeValue("key"); String val = eContext.getAttributeValue("value"); if (val != null) q.putContext(key, val); } for (Object test : eQuery.getChildren("Test")) { Element eTest = (Element) test; String key = eTest.getAttributeValue("key"); String val = eTest.getAttributeValue("value"); if (val != null) { q.setTestParam(key, val); } } Object ask = eQuery.getChild("AskBefore"); if (ask != null) { String key = ((Element) ask).getAttributeValue("key"); q.setAskBefore(key); } for (Object pp : eQuery.getChildren("PostProcessing")) { String var = ((Element) pp).getAttributeValue("var"); String operation = ((Element) pp).getAttributeValue("operation"); q.setPostProcessing(var, operation); } project.addQuery(q); } for (Object o : root.getChildren("Param")) { Element eParam = (Element) o; String key = eParam.getAttributeValue("key"); String val = eParam.getAttributeValue("val"); project.setQueryParameter(key, val); } for (Object o : root.getChildren("NodeAttribute")) { NodeAttribute attr = new NodeAttribute(); Element eNodeAttribute = (Element) o; attr.key = eNodeAttribute.getAttributeValue("key"); attr.value = eNodeAttribute.getAttributeValue("value"); for (Object vp : eNodeAttribute.getChildren("Vizmap")) { Element eVizmap = (Element) vp; String prop = eVizmap.getAttributeValue("prop"); String propValue = eVizmap.getAttributeValue("value"); attr.vizprops.put(prop, propValue); } project.nodeAttributes.add(attr); } project.dirty = false; if (warnings.size() > 0) { // TODO: set dialog root JOptionPane.showMessageDialog( null, "<html><ul><li>" + StringUtils.join("<br/><li>", warnings) + "</ul></html>"); } return project; }
public String getSubstitutedQuery(MarrsQuery mq) throws MarrsException { String q = mq.getQueryText(); Map<String, String> queryData = getQueryParameters(mq); for (String key : queryData.keySet()) { if (!parameters.containsKey(key)) { throw new MarrsException("Missing substitution parameter: " + key); } String contents = parameters.get(key); String filter = queryData.get(key); if ("uri-list".equals(filter)) { Pattern pat = Pattern.compile("\\s*<[-~#_:/%.0-9a-zA-Z]+>(\\s*,\\s*<[-~#_:/%.0-9a-zA-Z]+>)*\\s*$"); Matcher mat = pat.matcher(contents); if (!mat.matches()) { throw new MarrsException( "'" + contents + "' does not match pattern for parameter ${" + key + "|" + filter + "}"); } } else if ("uri-bracketed" .equals( filter)) /* difference between uri and uri-bracketed is the surrounding < and > characters */ { Pattern pat = Pattern.compile("\\s*<[-~#_:/%.0-9a-zA-Z]+>\\s*"); Matcher mat = pat.matcher(contents); if (!mat.matches()) { throw new MarrsException( "'" + contents + "' does not match pattern for parameter ${" + key + "|" + filter + "}"); } } else if ("uri".equals(filter)) { Pattern pat = Pattern.compile("[-~#_:/%.0-9a-zA-Z]+"); Matcher mat = pat.matcher(contents); if (!mat.matches()) { throw new MarrsException( "'" + contents + "' does not match pattern for parameter ${" + key + "|" + filter + "}"); } } else if ("literal".equals(filter)) { Pattern pat = Pattern.compile("^[^\"\\n]*$"); Matcher mat = pat.matcher(contents); if (!mat.matches()) { throw new MarrsException( "'" + contents + "' does not match pattern for parameter ${" + key + "|" + filter + "}"); } } else { /* most restrictive base pattern */ Pattern pat = Pattern.compile("[-~#_:/%.0-9a-zA-Z]+"); Matcher mat = pat.matcher(contents); if (!mat.matches()) { throw new MarrsException( "'" + contents + "' does not match pattern for parameter ${" + key + "}"); } } // TODO: replace & appendReplace q = q.replaceAll("\\$\\{" + key + "(\\|([\\w-]+))?\\}", contents); } System.out.println(q); return q; }