public GNRemoteQueryDecoder(QueryModel qm, ApplicationContext ctx) { try { InternalModelRootNode rn = qm.toInternalQueryModel(ctx); QueryNodeVisitor qnv = new QueryNodeVisitor() { public void visit(AttrPlusTermNode aptn) { super.visit(aptn); Element node = new Element("term"); if (aptn.getAccessPoint() != null) { node.setAttribute( new Attribute("use", getAttrVal((AttrValue) aptn.getAccessPoint()))); } if (aptn.getRelation() != null) { node.setAttribute( new Attribute("relation", getAttrVal((AttrValue) aptn.getRelation()))); } if (aptn.getStructure() != null) { node.setAttribute( new Attribute("structure", getAttrVal((AttrValue) aptn.getStructure()))); } if (aptn.getTruncation() != null) { node.setAttribute( new Attribute("truncation", getAttrVal((AttrValue) aptn.getTruncation()))); } node.addContent(aptn.getTermAsString(false)); stack.push(node); } public void visit(ComplexNode cn) { super.visit(cn); Element rightChild = (Element) stack.pop(); Element leftChild = (Element) stack.pop(); Element node = new Element(getOpString(cn.getOp())); node.addContent(leftChild); node.addContent(rightChild); stack.push(node); } public void visit(InternalModelRootNode rn) { super.visit(rn); Element query = new Element("query"); // QueryNode node = rn.getChild(); TODO: I dont know if this is important // query.setAttribute("attrset", node.getAttrs().toString() ); query.addContent((Element) stack.pop()); stack.push(query); } @Override public void onAttrPlusTermNode(AttrPlusTermNode aptn) { if (Log.isDebugEnabled(Geonet.Z3950_SERVER)) Log.debug( Geonet.Z3950_SERVER, "doing nothing..." + aptn); // TODO: find out how this is supposed to be used } /** * @param val * @return extracts the last index of an attribute (e.G 1.4 becomes 4) */ private String getAttrVal(AttrValue val) { if (val == null || val.getValue() == null) return null; String value = val.getValue(); String ret = value; String[] temp = value.split("\\."); if (temp != null && temp.length > 1) { ret = temp[temp.length - 1]; } return ret; } }; qnv.visit(rn); } catch (InvalidQueryException iqe) { iqe.printStackTrace(); } }
/** * {@inheritDoc} * * @throws RepositoryException */ public Object accept(QueryNodeVisitor visitor, Object data) throws RepositoryException { return visitor.visit(this, data); }