Mercurial > hg > mpdl-group
comparison software/eXist/mpdl-modules/src/org/exist/xquery/modules/mpdldoc/GetESciDocs.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.mpdldoc; | |
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.escidoc.ESciDocRestSession; | |
40 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; | |
41 import de.mpg.mpiwg.berlin.mpdl.general.MpdlConstants; | |
42 | |
43 /** | |
44 * @author Josef Willenborg (jwillenborg@mpiwg-berlin.mpg.de) | |
45 */ | |
46 public class GetESciDocs extends BasicFunction { | |
47 | |
48 public final static FunctionSignature signature = | |
49 new FunctionSignature( | |
50 new QName("escidoc-get-docs", MPDLDocModule.NAMESPACE_URI, MPDLDocModule.PREFIX), | |
51 "A function which delivers all eSciDoc documents restricted to the first argument: docbase." + | |
52 "Second argument is the cookieId.", | |
53 new SequenceType[] { | |
54 new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE), | |
55 new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE) | |
56 }, | |
57 new SequenceType(Type.NODE, Cardinality.EXACTLY_ONE)); | |
58 | |
59 public GetESciDocs(XQueryContext context) { | |
60 super(context, signature); | |
61 } | |
62 | |
63 public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { | |
64 try { | |
65 Sequence firstSeq = args[0]; | |
66 Sequence secondSeq = args[1]; | |
67 if (firstSeq.isEmpty() || secondSeq.isEmpty()) | |
68 return Sequence.EMPTY_SEQUENCE; | |
69 String firstSeqStrValue = firstSeq.getStringValue(); | |
70 String docBase = null; | |
71 if (! firstSeqStrValue.equals("")) | |
72 docBase = firstSeqStrValue; | |
73 String docBaseContainerId = MpdlConstants.MPDL_ESCIDOC_ECHO_CONTAINER_ID; | |
74 if (docBase != null && docBase.equals("archimedes")) | |
75 docBaseContainerId = MpdlConstants.MPDL_ESCIDOC_ARCHIMEDES_CONTAINER_ID; | |
76 String secondSeqStrValue = secondSeq.getStringValue(); | |
77 String eSciDocCookieId = null; | |
78 if (! secondSeqStrValue.equals("")) | |
79 eSciDocCookieId = secondSeqStrValue; | |
80 ESciDocRestSession eSciDocSession = ESciDocRestSession.getInstance(eSciDocCookieId); | |
81 String containerXmlStr = eSciDocSession.getContainer(docBaseContainerId); | |
82 ArrayList<String> containerIdsOfDocBaseContainer = eSciDocSession.getContainerIds(containerXmlStr); | |
83 ArrayList<String> containerTitlesOfDocBaseContainer = eSciDocSession.getContainerTitles(containerXmlStr); | |
84 DocumentImpl doc = null; | |
85 if (containerIdsOfDocBaseContainer != null) { | |
86 MemTreeBuilder builder = context.getDocumentBuilder(); | |
87 builder.startElement("", "documents", "documents", null); | |
88 for (int i=0; i<containerIdsOfDocBaseContainer.size(); i++) { | |
89 builder.startElement("", "doc", "doc", null); | |
90 builder.startElement("", "container-id", "container-id", null); | |
91 String containerId = containerIdsOfDocBaseContainer.get(i); | |
92 builder.characters(containerId); | |
93 builder.endElement(); | |
94 builder.startElement("", "exist-id", "exist-id", null); | |
95 String containerTitle = containerTitlesOfDocBaseContainer.get(i); | |
96 String existId = ""; | |
97 if (containerTitle != null) { | |
98 int beginIndex = containerTitle.indexOf("document-id:"); | |
99 int endIndex = containerTitle.indexOf(".xml", beginIndex); | |
100 if (beginIndex > 0 && endIndex > 0) { | |
101 existId = containerTitle.substring(beginIndex + 13, endIndex + 4); | |
102 builder.characters(existId); | |
103 } | |
104 } | |
105 builder.endElement(); | |
106 builder.endElement(); | |
107 } | |
108 builder.endElement(); | |
109 doc = ((DocumentImpl)builder.getDocument()); | |
110 } else { | |
111 return Sequence.EMPTY_SEQUENCE; | |
112 } | |
113 return doc; | |
114 } catch (ApplicationException e) { | |
115 throw new XPathException(e); | |
116 } | |
117 } | |
118 | |
119 } |