/**
* This file is part of Archiv-Editor.
*
* The software Archiv-Editor serves as a client user interface for working with
* the Person Data Repository. See: pdr.bbaw.de
*
* The software Archiv-Editor was developed at the Berlin-Brandenburg Academy
* of Sciences and Humanities, Jägerstr. 22/23, D-10117 Berlin.
* www.bbaw.de
*
* Copyright (C) 2010-2013 Berlin-Brandenburg Academy
* of Sciences and Humanities
*
* The software Archiv-Editor was developed by @author: Christoph Plutte.
*
* Archiv-Editor is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Archiv-Editor 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Archiv-Editor.
* If not, see <http://www.gnu.org/licenses/lgpl-3.0.html>.
*/
package org.bbaw.pdr.ae.identifiers.searchservice.textgrid;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Vector;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.bbaw.pdr.ae.identifiers.searchservice.textgrid.internal.TextGridSaxHandler;
import org.bbaw.pdr.ae.metamodel.IAEPresentable;
import org.bbaw.pdr.ae.view.identifiers.interfaces.IConcurrenceSearchService;
import org.bbaw.pdr.ae.view.identifiers.model.ConcurrenceData;
import org.bbaw.pdr.ae.view.identifiers.model.ConcurrenceQuery;
public class TextGridSearchService implements IConcurrenceSearchService
{
private static final String url = "http://ref.dariah.eu/pndsearch/pndquery.xql";
private Vector<String> _normNameTokens;
public TextGridSearchService()
{
}
@Override
public String getLabel()
{
return "TextGrid PND Service";
}
@Override
public String getImageString()
{
return null;
}
@Override
public int getPriority()
{
return 0;
}
@Override
public String getValue()
{
return "TextGrid PND Service";
}
@Override
public void setValue(String value)
{
}
@Override
public int compareTo(IAEPresentable arg0)
{
return 0;
}
@Override
public String getContent()
{
return "TextGrid PND Service";
}
@Override
public int getCursorPosition()
{
return 0;
}
@Override
public String getDescription()
{
return "TextGrid PND Service";
}
@Override
public URL buildUrl(ConcurrenceQuery query)
{
String link = url + "?";
URL url;
if (query != null)
{
if (query.getNormName() != null && query.getNormName().trim().length() > 0)
{
try {
link += "ln=" + (URLEncoder.encode(query.getNormName().trim().split("\\s")[0], "UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String normName = new String (query.getNormName().trim());
_normNameTokens = new Vector<String>();
if (normName.contains(" "))
{
for (String s : normName.split(" "))
{
if (!_normNameTokens.contains(s))
{
_normNameTokens.add(s);
}
}
}
else
{
_normNameTokens.add(normName);
}
}
}
try
{
link.replace("\\s", "%20");
// System.out.println("textgrid link " + link);
url = new URL(link);
return url;
}
catch (MalformedURLException e)
{
e.printStackTrace();
return null;
}
}
@Override
public Vector<ConcurrenceData> parseSearchResult(String xml)
{
if (xml != null)
{
SAXParserFactory factory = SAXParserFactory.newInstance();
try
{
InputStream xmlInput = new ByteArrayInputStream(xml.getBytes("UTF-8"));
SAXParser saxParser = factory.newSAXParser();
TextGridSaxHandler handler = new TextGridSaxHandler();
saxParser.parse(xmlInput, handler);
return checkAgainstQuery(handler.getConcurrenceDatas());
}
catch (Throwable err)
{
err.printStackTrace();
}
}
return null;
}
private Vector<ConcurrenceData> checkAgainstQuery(
Vector<ConcurrenceData> concurrenceDatas) {
Vector<ConcurrenceData> result = new Vector<ConcurrenceData>();
for (ConcurrenceData cd : concurrenceDatas)
{
if (cd.getNormName() != null && filterNormName(cd.getNormName()))
{
result.add(cd);
}
}
return result;
}
private boolean filterNormName(String normName)
{
int score = 0;
for (String s : _normNameTokens)
{
if (normName.contains(s))
{
score++;
}
}
if (score >= _normNameTokens.size() - 2)
{
return true;
}
return false;
}
}