⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resultlistentry.java

📁 基于MPEG 7 标准,符合未来语义网架构,很值得参考
💻 JAVA
字号:
/*
 * This file is part of Caliph & Emir.
 *
 * Caliph & Emir is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Caliph & Emir is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Caliph & Emir; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Copyright statement:
 * --------------------
 * (c) 2002-2005 by Mathias Lux (mathias@juggle.at)
 * http://www.juggle.at, http://caliph-emir.sourceforge.net
 */
package at.lux.fotoretrieval;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.transform.JDOMResult;
import org.jdom.transform.JDOMSource;

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * ResultListEntry
 *
 * @author Mathias Lux, mathias@juggle.at
 */
public class ResultListEntry implements Comparable {
    private double relevance;
    private Element documentRoot;
    private String filePath = null;
    private String thumbPath = null;
    private String descriptionPath = null;
    private String HTMLSummary = null;
    private static DecimalFormat df = ((DecimalFormat) DecimalFormat.getInstance());
    private int quality = -1;

    private static Transformer myTransformer = null;
    private String semanticDescriptionString;
    private String creatorName;
    private String freeTextDescription;
    private String creationTime;
    private String imageFilePath;
    private String imageSize;

    public ResultListEntry(double relevance, Element documentRoot, String descriptionPath) {
        this.relevance = relevance;
        this.documentRoot = documentRoot;
        this.descriptionPath = descriptionPath;

        df.setMaximumFractionDigits(2);

        List results = RetrievalToolkit.xpathQuery(documentRoot, "//MediaProfile[@master='true']/MediaInstance/MediaLocator/MediaUri", null);
        if (results.size() > 0) {
            filePath = ((Element) results.get(0)).getTextTrim();
            if (results.size() > 1) {
                thumbPath = ((Element) results.get(1)).getTextTrim();
            }
        }
        // /Mpeg7/Description/MultimediaContent/Image/MediaInformation/MediaProfile/MediaFormat/VisualCoding/Frame
        results = RetrievalToolkit.xpathQuery(documentRoot, "//MediaProfile/MediaFormat/VisualCoding/Frame[number(@height) < 121 and number(@width) < 121]", null);
        if (results.size() > 0) {
            Element frame = ((Element) results.get(0));
            Element profile = (Element) frame.getParent().getParent().getParent();
            Namespace mpeg7 = profile.getNamespace();
            Element uri = profile.getChild("MediaInstance", mpeg7).getChild("MediaLocator", mpeg7).getChild("MediaUri", mpeg7);
            thumbPath = uri.getTextTrim();
        }
        // quality
        results = RetrievalToolkit.xpathQuery(documentRoot, "//QualityRating[@type='subjective']/RatingValue", null);
        if (results.size() > 0) {
            quality = Integer.parseInt(((Element) results.get(0)).getTextTrim());
        } else {
            quality = -1;
        }
        // creating the summary:
//        HTMLSummary = getSummary();
        extractSummary();
        this.documentRoot = null;
        documentRoot = null;
    }

    public double getRelevance() {
        return relevance;
    }

    public int getQuality() {
        return quality;
    }

    public String getFilePath() {
        return filePath;
    }

    public String getThumbPath() {
        return thumbPath;
    }

    public void setThumbPath(String thumbPath) {
        this.thumbPath = thumbPath;
    }

    /**
     * Gets path to MPEG-7 file
     *
     * @return Path to MPEG-7 file
     */
    public String getDescriptionPath() {
        return descriptionPath;
    }

    /**
     * Summary of result
     *
     * @return Summary of result based on HTML 3.2
     */
    public String getHTMLSummary() {
        return HTMLSummary;
    }

    public String getSemanticDescriptionString() {
        return semanticDescriptionString;
    }

    public String getCreatorName() {
        return creatorName;
    }

    public String getFreeTextDescription() {
        return freeTextDescription;
    }

    public String getCreationTime() {
        return creationTime;
    }

    public String getImageFilePath() {
        return imageFilePath;
    }

    public String getImageSize() {
        return imageSize;
    }

    private void extractSummary() {
        creatorName = "";
        List l = RetrievalToolkit.xpathQuery(documentRoot, "//CreationInformation/Creation/Creator/Agent/Name", documentRoot.getNamespace());
        if (l.size() > 0) {
            Element name = (Element) l.get(0);
            creatorName += name.getChildTextTrim("FamilyName", name.getNamespace());
            creatorName += ", ";
            creatorName += name.getChildTextTrim("GivenName", name.getNamespace());
        }
        creatorName += " with " + getValueOfPath("//CreationInformation/Creation/CreationTool/Tool/Name");
        creationTime = getValueOfPath("//CreationInformation/Creation/CreationCoordinates/Date/TimePoint");

        // so we can build up some description on our own.
        LinkedList<String> agents = new LinkedList<String>();
        LinkedList<String> events = new LinkedList<String>();
        LinkedList<String> times = new LinkedList<String>();
        LinkedList<String> places = new LinkedList<String>();

        Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        l = RetrievalToolkit.xpathQuery(documentRoot, "//SemanticBase", documentRoot.getNamespace());
        for (Iterator iterator = l.iterator(); iterator.hasNext();) {
            Element elem = (Element) iterator.next();
            String label = null;
            label = elem.getChild("Label", elem.getNamespace()).getChildText("Name", elem.getNamespace()).trim();
            if (elem.getAttribute("type", xsi).getValue().startsWith("AgentObjectType")) {
                agents.add(label);
            } else if (elem.getAttribute("type", xsi).getValue().startsWith("EventType")) {
                events.add(label);
            } else if (elem.getAttribute("type", xsi).getValue().startsWith("SemanticTimeType")) {
                times.add(label);
            } else if (elem.getAttribute("type", xsi).getValue().startsWith("SemanticPlaceType")) {
                places.add(label);
            }
        }
        semanticDescriptionString = appendAll(agents) + ((appendAll(agents).contains(",")) ? " are at " : " is at ") + appendAll(places) + " for " + appendAll(events) + " in " + appendAll(times);

        // fre text annotation:
        String valueOfPath = getValueOfPath("//TextAnnotation/FreeTextAnnotation");
        freeTextDescription = valueOfPath;

        valueOfPath = getValueOfPath("//MediaInformation/MediaProfile/MediaInstance/MediaLocator/MediaUri");
        imageFilePath = valueOfPath.substring(valueOfPath.lastIndexOf('/') + 1);
        l = RetrievalToolkit.xpathQuery(documentRoot, "//MediaInformation/MediaProfile/MediaFormat/VisualCoding/Frame", documentRoot.getNamespace());
        if (l.size() > 0) {
            Element frame = (Element) l.get(0);
            imageSize = frame.getAttribute("width").getValue() + " x " + frame.getAttribute("height").getValue() + " pixels ";
        }
    }

    private String getValueOfPath(String path) {
        String result = null;
        List l = RetrievalToolkit.xpathQuery(documentRoot, path, documentRoot.getNamespace());
        if (l.size() > 0) {
            Element name = (Element) l.get(0);
            result = name.getTextTrim();
        }
        return result;
    }

    private String getSummary() {
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
//        outputter.setOmitEncoding(true);
//        outputter.setOmitDeclaration(true);
        String doc = null;
        Document _d = null;
        try {
            _d = transform(documentRoot.getDocument());
        } catch (JDOMException e) {
            debug("JDOMException " + e.getMessage());
        }
        if (_d != null) {
            String f_name = filePath;
            if (f_name.lastIndexOf('/') != -1) {
                f_name = f_name.substring(f_name.lastIndexOf('/') + 1);
            }
            if (f_name.lastIndexOf('\\') != -1) {
                f_name = f_name.substring(f_name.lastIndexOf('\\') + 1);
            }
            doc = outputter.outputString(_d);
            doc = "<html>" + doc.substring(doc.indexOf('>') + 1);
            doc = doc.replaceAll("<br />", "<br>");
            doc = doc.replaceAll("<head />", "");
            doc = doc.replaceAll("<td />", "<td>n.a.</td>");
            doc = doc.replaceAll("_relevance_", "" + df.format(relevance));
            doc = doc.replaceAll("_filename_", f_name);
//                        System.out.println(doc);
        }

        // get all input from the semantic description:
        doc = doc.replaceAll("_semanticdescription_", semanticDescriptionString);
        return doc;
    }

    private String appendAll(List<String> input) {
        StringBuilder sb = new StringBuilder(input.size() * 32);
        for (Iterator<String> iterator = input.iterator(); iterator.hasNext();) {
            sb.append(iterator.next());
            if (iterator.hasNext()) sb.append(", ");
        }
        return sb.toString();
    }

    private Document transform(Document in) throws JDOMException {
        Document d = null;
        try {
            Transformer transformer = getTransformer();
            JDOMResult out = new JDOMResult();
            transformer.transform(new JDOMSource(in), out);
            d = out.getDocument();
        } catch (TransformerException e) {
            debug("Transformation failed, TransformerException: " + e.getMessageAndLocation());
        } catch (TransformerFactoryConfigurationError error) {
            debug("Transformation failed, " + error.toString());
        } catch (IOException e) {
            debug("Transformation failed, IOException: " + e.getMessage());
        }
        return d;
    }

    private static Transformer getTransformer() throws IOException, TransformerConfigurationException {
        Transformer transformer = myTransformer;
        if (transformer == null) {
            URL resource = RetrievalToolkit.class.getResource("data/html-short.xsl");
            StreamSource source = new StreamSource(resource.openStream());
            transformer = TransformerFactory.newInstance().newTransformer(source);
        }
        return transformer;
    }

    private void debug(String message) {
        System.out.println("[at.lux.fotoretrieval.ResultListEntry]" + message);
    }

    public int compareTo(Object o) {
        ResultListEntry r = (ResultListEntry) o;
        return (int) Math.signum(r.getRelevance() - relevance);
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -