📄 metaquerytranslator.java
字号:
package dli2fe.helpers;/** * Title: Digial Library Interoperable Interface Fudan Edition * Description: This project contains all the classes required for DLI2FE interface. Developers use these classes to implement the wrapper and client side codes. The basic functions of DLI2FE is as follows: * Search: Search a digital library source site. * Metadata: Fetch metadata for a site. * ResultAccess: Get results for a given query. * DLI2FE uses Dublin Core as the basic attribute model, DAV/DASL as the general XML-based query language and CORBA as distributed object transportation mechanism. * Copyright: Copyright (c) 2001 * Company: Fudan University * @author Carl Tao * @version 1.0 */import org.w3c.dom.*;import dli2fe.xml.XMLObject;import dli2fe.DLI2FEException;import dli2fe.xml.DOMUtil;import dli2fe.sample.MultimediaMeta;import java.util.Hashtable;import dli2fe.DAV;public class MetaQueryTranslator { String sfwQuery; // select/from/where query String select; boolean returnImage; String where; Hashtable attrTrans; public MetaQueryTranslator(XMLObject metaQuery, Hashtable transTable, XMLObject propList, String fromClause, String dummyAttr) throws DLI2FEException { Element elWhere; returnImage = false; select = ""; where = ""; sfwQuery = ""; attrTrans = transTable; // make translation table globally accessable to other routines parsePropList(propList); // parse select clause with translation if (metaQuery == null || (elWhere = DOMUtil.getChild(metaQuery.getElement(), DAV.where))==null) { if ("".equals(select)) sfwQuery = "select " + dummyAttr +" from " + fromClause; else sfwQuery = "select " + select +" from " + fromClause; return; } parseQueryTree(elWhere); // parse where clause with translation if ("".equals(select)) sfwQuery = "select " + dummyAttr + " from " + fromClause + (("".equals(where)) ? "" : " where " + where); else sfwQuery = "select " + select + " from " + fromClause + (("".equals(where)) ? "" : " where " + where); } public String getMetaQuery() { return sfwQuery; } public String getSelectClause() { return select; } public boolean returnImage() { return returnImage; } public String getWhereClause() { return where; } void parseQueryTree(Element root) throws DLI2FEException { if (root == null) return; if (DAV.where.equals(root.getLocalName())) parseQueryTree(DOMUtil.getChild(root, "*")); if (DAV.and.equals(root.getLocalName()) || DAV.or.equals(root.getLocalName())) { boolean hasBrackets = true; if (DAV.where.equals(root.getParentNode().getLocalName())) hasBrackets = false; Node child = DOMUtil.getChild(root, "*"); if (child == null) return; if (hasBrackets) where += "("; parseQueryTree((Element)child); while ((child = child.getNextSibling())!=null) { if (child.getNodeType()!=Node.ELEMENT_NODE) continue; where += " " + root.getLocalName() + " "; parseQueryTree((Element)child); } if (hasBrackets) where += ")"; } if (DAV.not.equals(root.getLocalName())) { Element child = DOMUtil.getChild(root, "*"); if (child == null) return; where += "(" + DAV.not + " "; parseQueryTree(child); where += ")"; } if (DAV.eq.equals(root.getLocalName()) || DAV.like.equals(root.getLocalName())) { Element prop = DOMUtil.getDescendant(root, DAV.prop); Element literal = DOMUtil.getDescendant(root, DAV.literal); if (prop == null || literal == null) throw new DLI2FEException(DLI2FEException.BAD_QUERY_EXC, "No property or value specified in the meta query."); String propName = getAttrTranslation(DOMUtil.getDescendant(prop, "*").getLocalName()); String literalValue = DOMUtil.getText(literal); literalValue = literalValue == null ? "" : literalValue; where += propName + "='" + literalValue +"'"; } } void parsePropList(XMLObject propList) { NodeList nodes = propList.getElement().getElementsByTagName("*"); for (int i = 0; i < nodes.getLength(); i++) { Node propNode = nodes.item(i); if ("prop".equals(nodes.item(i).getLocalName())) propNode = nodes.item(i).getFirstChild(); if (!MultimediaMeta.Namespace.equals(propNode.getNamespaceURI())) { select += getAttrTranslation(propNode.getLocalName()) + ", "; } else returnImage = true; } if (select.endsWith(", ")) select = select.substring(0, select.length()-2); } String getAttrTranslation(String inputAttr) { String outputAttr = (String)attrTrans.get(inputAttr); if (outputAttr == null) return inputAttr; return outputAttr; } public static void main(String[] args) { try { XMLObject query = new XMLObject("<?xml version='1.0' encoding='GB2312' ?> <basicsearch xmlns='http://www.cs.fudan.edu.cn/DLI2FE/1.0#' xmlns:ts='http://cs.fudan.edu.cn/travel/#' xmlns:mm='http://www.cs.fudan.edu.cn/multimedia/#'> <where> <and> <or> <eq> <prop><ts:Location.Province/></prop> <literal>河北</literal> </eq> <eq> <prop><ts:Location.Province/></prop> <literal>山西</literal> </eq> </or> <eq> <prop><Scene.Culture.Figures.Figure/></prop> <literal>廉颇</literal> </eq> </and> </where> </basicsearch>");// XMLObject query = new XMLObject("<?xml version='1.0' encoding='GB2312' ?> <basicsearch xmlns='http://www.cs.fudan.edu.cn/DLI2FE/1.0#' xmlns:ts='http://cs.fudan.edu.cn/travel/#' xmlns:mm='http://www.cs.fudan.edu.cn/multimedia/#'> <where> <like><prop><mm:image/></prop><literal>http://aaa/</literal> </like> </where> </basicsearch>"); Hashtable attrTransTable = new Hashtable(); attrTransTable.put("Location.Province", "Scene.Location.Province"); XMLObject propList = new XMLObject("<?xml version='1.0' encoding='GB2312' ?> <proplist xmlns='http://www.cs.fudan.edu.cn/DLI2FE/1.0#' xmlns:ts='http://cs.fudan.edu.cn/travel/#' xmlns:mm='http://www.cs.fudan.edu.cn/multimedia/#'> <ts:Scene.Name/><ts:Scene.SubSights.SubSight/> </proplist> ");//<ts:Scene.Name/> <ts:Location.Province/> MetaQueryTranslator mqtran = new MetaQueryTranslator(query, attrTransTable, propList, "Sight", "Scene.Name"); System.out.println(mqtran.getMetaQuery()); System.out.print("Return image? " + mqtran.returnImage()); } catch (DLI2FEException ex) { System.out.println(ex); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -