Mercurial > hg > mpdl-group
comparison software/eXist/mpdl-modules/src/org/exist/xquery/modules/mpdltext/GetDonatusQueryVariants.java @ 0:408254cf2f1d
Erstellung
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 24 Nov 2010 17:24:23 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:408254cf2f1d |
---|---|
1 /* | |
2 * eXist Open Source Native XML Database: Extension module | |
3 * Copyright (C) 2008 Josef Willenborg | |
4 * jwillenborg@mpiwg-berlin.mpg.de | |
5 * http://www.mpiwg-berlin.mpg.de | |
6 * | |
7 * This program is free software; you can redistribute it and/or | |
8 * modify it under the terms of the GNU Lesser General Public License | |
9 * as published by the Free Software Foundation; either version 2 | |
10 * of the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 * | |
21 * $Id: TextModule.java $ | |
22 */ | |
23 package org.exist.xquery.modules.mpdltext; | |
24 | |
25 import java.util.ArrayList; | |
26 | |
27 import org.exist.dom.QName; | |
28 import org.exist.xquery.BasicFunction; | |
29 import org.exist.xquery.Cardinality; | |
30 import org.exist.xquery.FunctionSignature; | |
31 import org.exist.xquery.XPathException; | |
32 import org.exist.xquery.XQueryContext; | |
33 import org.exist.xquery.value.Sequence; | |
34 import org.exist.xquery.value.SequenceType; | |
35 import org.exist.xquery.value.StringValue; | |
36 import org.exist.xquery.value.Type; | |
37 import org.exist.xquery.value.ValueSequence; | |
38 | |
39 import de.mpg.mpiwg.berlin.mpdl.donatus.xmlrpc.DonatusCache; | |
40 import de.mpg.mpiwg.berlin.mpdl.donatus.xmlrpc.DonatusVariant; | |
41 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; | |
42 | |
43 /** | |
44 * @author Josef Willenborg (jwillenborg@mpiwg-berlin.mpg.de) | |
45 */ | |
46 public class GetDonatusQueryVariants extends BasicFunction { | |
47 | |
48 public final static FunctionSignature signature = | |
49 new FunctionSignature( | |
50 new QName("get-donatus-query-variants", MPDLTextModule.NAMESPACE_URI, MPDLTextModule.PREFIX), | |
51 "A function which delivers morphological variants (seperated by |) of a given Lucene query string of a given " + | |
52 "language over the lemma of that variant by the Donatus language technology", | |
53 new SequenceType[] { new SequenceType(Type.STRING, Cardinality.ZERO_OR_MORE), | |
54 new SequenceType(Type.STRING, Cardinality.ZERO_OR_MORE) }, | |
55 new SequenceType(Type.STRING, Cardinality.ZERO_OR_MORE)); | |
56 | |
57 public GetDonatusQueryVariants(XQueryContext context) { | |
58 super(context, signature); | |
59 } | |
60 | |
61 public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { | |
62 Sequence langSeq = args[0]; | |
63 Sequence luceneQueryStringSeq = args[1]; | |
64 String language = ""; | |
65 String luceneQueryString = ""; | |
66 if (langSeq.isEmpty() || luceneQueryStringSeq.isEmpty()) | |
67 return Sequence.EMPTY_SEQUENCE; | |
68 language = langSeq.getStringValue(); | |
69 luceneQueryString = luceneQueryStringSeq.getStringValue(); | |
70 ArrayList<DonatusVariant> resultVariants = null; | |
71 try { | |
72 DonatusCache donatusCache = DonatusCache.getInstance(); | |
73 resultVariants = donatusCache.getQueryVariants(language, luceneQueryString); | |
74 } catch (ApplicationException e) { | |
75 throw new XPathException(e); | |
76 } | |
77 ValueSequence result = new ValueSequence(); | |
78 String resultStr = ""; | |
79 int size = resultVariants.size(); | |
80 for (int i=0; i<size; i++) { | |
81 String variantStr = resultVariants.get(i).getForm(); | |
82 resultStr = resultStr + variantStr + "|"; | |
83 } | |
84 if (size == 0) { | |
85 result.add(new StringValue("")); | |
86 } else { | |
87 resultStr = resultStr.substring(0, resultStr.length() - 1); // without last | character | |
88 result.add(new StringValue(resultStr)); | |
89 } | |
90 return result; | |
91 } | |
92 } |