?? mmquerydecomposer.java
字號(hào):
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 dli2fe.DAV;public class MMQueryDecomposer { XMLObject metaQuery; String image; String pfeedback; String nfeedback; boolean isAnd; public MMQueryDecomposer(XMLObject query) throws DLI2FEException { metaQuery = query; image=null; pfeedback=null; nfeedback=null; isAnd = false; Element where = DOMUtil.getChild(query.getElement(), DAV.where); if (where == null) { throw new DLI2FEException(DLI2FEException.BAD_QUERY_EXC, "No where clause specified: " + query.getString()); } Element firstChildOfWhere = DOMUtil.getChild(where, "*"); // 如果Where子句的直接兒子是邏輯操作符 if (firstChildOfWhere != null && (DAV.and.equals(firstChildOfWhere.getLocalName()) || DAV.or.equals(firstChildOfWhere.getLocalName()))) { isAnd = DAV.and.equals(firstChildOfWhere.getLocalName()); Element elMM; // 用來存儲(chǔ)多媒體屬性的Element Element condImage=null, condPFeedback=null, condNFeedback=null; // 用來存儲(chǔ)多媒體條件的Elements if ((elMM = DOMUtil.getDescendant((Element)firstChildOfWhere, MultimediaMeta.Namespace, MultimediaMeta.image))!=null) { condImage = (Element)elMM.getParentNode().getParentNode(); image = DOMUtil.getDescendantText(condImage, DAV.literal); } if ((elMM = DOMUtil.getDescendant((Element)firstChildOfWhere, MultimediaMeta.Namespace, MultimediaMeta.pfeedback))!=null) { condPFeedback = (Element)elMM.getParentNode().getParentNode(); pfeedback = DOMUtil.getDescendantText(condPFeedback, DAV.literal); } if ((elMM = DOMUtil.getDescendant((Element)firstChildOfWhere, MultimediaMeta.Namespace, MultimediaMeta.nfeedback))!=null) { condNFeedback = (Element)elMM.getParentNode().getParentNode(); nfeedback = DOMUtil.getDescendantText(condNFeedback, DAV.literal); } // 如果根本沒有多媒體查詢條件,直接返回 if (condImage == null) return; // 如果各個(gè)多媒體條件的父節(jié)點(diǎn)不同,則查詢語句有語法錯(cuò) if (condPFeedback != null && condImage.getParentNode() != condPFeedback.getParentNode() || condNFeedback != null && condImage.getParentNode() != condNFeedback.getParentNode()) throw new DLI2FEException(DLI2FEException.BAD_QUERY_EXC, "Unparelleled multimedia conditions: " + query.getString()); // 刪除多媒體查詢條件 Element parentCond = (Element)condImage.getParentNode(); parentCond.removeChild(condImage); if (condPFeedback != null) parentCond.removeChild(condPFeedback); if (condNFeedback != null) parentCond.removeChild(condNFeedback); // 向上遍歷直到刪除所有因刪除多媒體條件導(dǎo)致的空節(jié)點(diǎn) while (DOMUtil.getChild(parentCond, "*") == null && !(DAV.where.equals(parentCond.getLocalName()))) { Element oldParent = parentCond; parentCond = (Element)oldParent.getParentNode(); parentCond.removeChild(oldParent); } // 否則如果Where子句只包含image屬性的唯一條件 } else if (firstChildOfWhere != null && DOMUtil.getDescendant(firstChildOfWhere, MultimediaMeta.Namespace, MultimediaMeta.image)!=null) { image = DOMUtil.getDescendantText(firstChildOfWhere, DAV.literal); metaQuery = null; // 如果Where子句沒有條件,產(chǎn)生異常 } else if (firstChildOfWhere == null) throw new DLI2FEException(DLI2FEException.BAD_QUERY_EXC, "Null query: " + query.getString()); } public XMLObject getMetaQuery() { return metaQuery; } public String getQueryImage() { return image; } public String getQueryPFeedback() { return pfeedback; } public String getQueryNFeedback() { return nfeedback; } public boolean isAnd() { return isAnd; } public static void main(String[] args) { try { //XMLObject xq = new XMLObject("<?xml version=\"1.0\" encoding=\"GB2312\" ?><basicsearch xmlns=\"http://www.cs.fudan.edu.cn/DLI2FE/1.0#\" xmlns:dc=\"http://purl.org/metadata/dublin_core#\" xmlns:mm=\"http://www.cs.fudan.edu.cn/multimedia/#\"> <where> <like> <prop><mm:image/></prop> <literal>file://D:\\Temp\\bookinput.jpg</literal> </like> </where> </basicsearch>"); //XMLObject xq = new XMLObject("<?xml version=\"1.0\" encoding=\"GB2312\" ?><basicsearch xmlns=\"http://www.cs.fudan.edu.cn/DLI2FE/1.0#\" xmlns:dc=\"http://purl.org/metadata/dublin_core#\" xmlns:mm=\"http://www.cs.fudan.edu.cn/multimedia/#\"> <where> <and> <like> <prop><mm:image/></prop> <literal>file://D:\\Temp\\bookinput.jpg</literal> </like> <eq><prop><mm:pfeedback/></prop><literal>4+5+</literal> </eq><eq><prop><mm:nfeedback/></prop><literal>3+2+</literal> </eq></and></where> </basicsearch>"); //XMLObject xq = new XMLObject("<?xml version=\"1.0\" encoding=\"GB2312\" ?><basicsearch xmlns=\"http://www.cs.fudan.edu.cn/DLI2FE/1.0#\" xmlns:dc=\"http://purl.org/metadata/dublin_core#\" xmlns:mm=\"http://www.cs.fudan.edu.cn/multimedia/#\"> <where> <and> <eq> <prop><dc:Contributor><dc:Translator/></dc:Contributor></prop> <literal>施伯樂</literal> </eq> <and> <like> <prop><mm:image/></prop> <literal>file://D:\\Temp\\bookinput.jpg</literal> </like> <eq><prop><mm:pfeedback/></prop><literal>4+5+</literal> </eq><eq><prop><mm:nfeedback/></prop><literal>3+2+</literal> </eq></and></and> </where> </basicsearch>"); //XMLObject xq = new XMLObject("<?xml version=\"1.0\" encoding=\"GB2312\" ?><basicsearch xmlns=\"http://www.cs.fudan.edu.cn/DLI2FE/1.0#\" xmlns:dc=\"http://purl.org/metadata/dublin_core#\" xmlns:mm=\"http://www.cs.fudan.edu.cn/multimedia/#\"> <where> <like> <prop><mm:image/></prop> <literal>file://D:\\Temp\\bookinput.jpg</literal> </like> </where> </basicsearch>"); XMLObject xq = new XMLObject("<?xml version='1.0' encoding='GB2312' ?><basicsearch xmlns='http://www.cs.fudan.edu.cn/DLI2FE/1.0#' xmlns:mm='http://www.cs.fudan.edu.cn/multimedia/#' xmlns:ts='http://cs.fudan.edu.cn/travel/#'><where><like><prop><mm:image/></prop><literal>http://localhost:8080/multimediasearch/tempsample/first.jpg</literal></like></where></basicsearch>"); System.out.println(xq.getString()); MMQueryDecomposer qd = new MMQueryDecomposer(xq); if (qd.getMetaQuery() != null) System.out.println(qd.getMetaQuery().getString()); else System.out.println(qd.getMetaQuery()); System.out.println(qd.getQueryImage()); System.out.println(qd.getQueryPFeedback()); System.out.println(qd.getQueryNFeedback()); } catch (DLI2FEException ex) { System.out.println(ex); } }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -