comparison software/eXist/mpdl-modules/src/org/exist/xquery/modules/mpdltext/GetLexEntriesByFormName.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.memtree.DocumentImpl;
29 import org.exist.memtree.MemTreeBuilder;
30 import org.exist.xquery.BasicFunction;
31 import org.exist.xquery.Cardinality;
32 import org.exist.xquery.FunctionSignature;
33 import org.exist.xquery.XPathException;
34 import org.exist.xquery.XQueryContext;
35 import org.exist.xquery.value.Sequence;
36 import org.exist.xquery.value.SequenceType;
37 import org.exist.xquery.value.Type;
38
39 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException;
40 import de.mpg.mpiwg.berlin.mpdl.lt.lex.app.Lexica;
41 import de.mpg.mpiwg.berlin.mpdl.lt.lex.app.Lexicon;
42 import de.mpg.mpiwg.berlin.mpdl.lt.lex.app.LexiconEntry;
43 import de.mpg.mpiwg.berlin.mpdl.lt.lex.db.LexHandler;
44
45 /**
46 * @author Josef Willenborg (jwillenborg@mpiwg-berlin.mpg.de)
47 */
48 public class GetLexEntriesByFormName extends BasicFunction {
49
50 public final static FunctionSignature signature =
51 new FunctionSignature(
52 new QName("get-lex-entries-by-form-name", MPDLTextModule.NAMESPACE_URI, MPDLTextModule.PREFIX),
53 "A function which delivers the lex entries of a given form name and language " +
54 "by the MPDL language technology",
55 new SequenceType[] { new SequenceType(Type.STRING, Cardinality.ZERO_OR_MORE),
56 new SequenceType(Type.STRING, Cardinality.ZERO_OR_MORE) },
57 new SequenceType(Type.NODE, Cardinality.EXACTLY_ONE));
58
59 public GetLexEntriesByFormName(XQueryContext context) {
60 super(context, signature);
61 }
62
63 public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
64 Sequence langSeq = args[0];
65 Sequence formNameSeq = args[1];
66 String language = "";
67 String formName = "";
68 if (langSeq.isEmpty() || formNameSeq.isEmpty())
69 return Sequence.EMPTY_SEQUENCE;
70 language = langSeq.getStringValue();
71 formName = formNameSeq.getStringValue();
72 ArrayList<Lexicon> lexicons = null;
73 try {
74 ArrayList<Lexicon> statLexicons = Lexica.getInstance().getLexicons(language);
75 if (statLexicons != null) {
76 LexHandler lexHandler = LexHandler.getInstance();
77 for (int i=0; i<statLexicons.size(); i++) {
78 Lexicon lexicon = statLexicons.get(i).clone(); // clone without lexicon entries
79 LexiconEntry lexEntry = lexHandler.readEntry(lexicon.getName(), formName);
80 if (lexEntry != null) {
81 lexicon.addEntry(lexEntry); // add entries to the cloned object
82 if (lexicons == null)
83 lexicons = new ArrayList<Lexicon>();
84 lexicons.add(lexicon);
85 }
86 }
87 }
88 } catch (ApplicationException e) {
89 throw new XPathException(e);
90 }
91 DocumentImpl doc = null;
92 if (lexicons != null) {
93 MemTreeBuilder builder = context.getDocumentBuilder();
94 builder.startElement("", "lexica", "lexica", null);
95 for (int i=0; i<lexicons.size(); i++) {
96 Lexicon lexicon = lexicons.get(i);
97 builder.startElement("", "lexicon", "lexicon", null);
98 builder.startElement("", "name", "name", null);
99 builder.characters(lexicon.getName());
100 builder.endElement();
101 builder.startElement("", "description", "description", null);
102 builder.characters(lexicon.getDescription());
103 builder.endElement();
104 builder.startElement("", "entries", "entries", null);
105 ArrayList<LexiconEntry> entries = lexicon.getEntries();
106 for (int j=0; j<entries.size(); j++) {
107 builder.startElement("", "entry", "entry", null);
108 LexiconEntry entry = entries.get(j);
109 builder.startElement("", "form", "form", null);
110 builder.characters(entry.getFormName());
111 builder.endElement();
112 builder.startElement("", "content", "content", null);
113 builder.startElement("", "xml-valid", "xml-valid", null);
114 String xmlValid = "false";
115 if (entry.isXmlValid())
116 xmlValid = "true";
117 builder.characters(xmlValid);
118 builder.endElement();
119 builder.startElement("", "original-entry", "original-entry", null);
120 builder.characters(entry.getOriginalEntry());
121 builder.endElement();
122 builder.startElement("", "repaired-entry", "repaired-entry", null);
123 builder.characters(entry.getRepairedEntry());
124 builder.endElement();
125 builder.endElement();
126 builder.endElement();
127 }
128 builder.endElement();
129 builder.endElement();
130 }
131 builder.endElement();
132 doc = ((DocumentImpl)builder.getDocument());
133 } else {
134 return Sequence.EMPTY_SEQUENCE;
135 }
136 return doc;
137 }
138
139
140 }