comparison software/eXist/mpdl-modules/src/org/exist/xquery/modules/mpdltext/ExternalObject.java @ 6:2396a569e446

new functions: externalObjects, normalizer, Unicode2Betacode
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 08 Feb 2011 14:54:09 +0100
parents
children 1ec29fdd0db8
comparison
equal deleted inserted replaced
5:94305c504178 6:2396a569e446
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 import java.util.Date;
27
28 import org.exist.dom.QName;
29 import org.exist.xquery.BasicFunction;
30 import org.exist.xquery.Cardinality;
31 import org.exist.xquery.FunctionSignature;
32 import org.exist.xquery.XPathException;
33 import org.exist.xquery.XQueryContext;
34 import org.exist.xquery.value.Sequence;
35 import org.exist.xquery.value.SequenceType;
36 import org.exist.xquery.value.StringValue;
37 import org.exist.xquery.value.Type;
38 import org.exist.xquery.value.ValueSequence;
39
40 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException;
41 import de.mpg.mpiwg.berlin.mpdl.externalObjects.app.ExtElement;
42 import de.mpg.mpiwg.berlin.mpdl.externalObjects.app.ExternalObjectsHandler;
43
44 /**
45 * @author Josef Willenborg (jwillenborg@mpiwg-berlin.mpg.de)
46 */
47 public class ExternalObject extends BasicFunction {
48
49 public final static FunctionSignature signature =
50 new FunctionSignature(
51 new QName("externalObject", MPDLTextModule.NAMESPACE_URI, MPDLTextModule.PREFIX),
52 "A function which add, update, delete or read external elements",
53 new SequenceType[] { new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE),
54 new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE),
55 new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE) },
56 new SequenceType(Type.STRING, Cardinality.EXACTLY_ONE));
57
58 public ExternalObject(XQueryContext context) {
59 super(context, signature);
60 }
61
62 public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
63 Sequence operation = args[0]; // read, update or delete
64 Sequence type = args[1];
65 Sequence object = args[2];
66 if (operation.isEmpty() || type.isEmpty())
67 return Sequence.EMPTY_SEQUENCE;
68 String operationStr = operation.getStringValue();
69 String typeStr = type.getStringValue();
70 String objectStr = object.getStringValue();
71
72 ValueSequence result = null;
73 String resultStr = "";
74 try {
75 if (typeStr.equals("element")) {
76 ExtElement e = ExtElement.parseXmlStr(objectStr);
77 if (operation.equals("create") || operation.equals("update")) {
78 Date now = new Date();
79 e.setModificationDate(now);
80 }
81 String documentId = e.getDocumentId();
82 String pageNumber = e.getPageNumber();
83 if (operationStr.equals("read")) {
84 ExternalObjectsHandler externalObjectsHandler = ExternalObjectsHandler.getInstance();
85 ArrayList<ExtElement> elems = externalObjectsHandler.readExternalElements(documentId, pageNumber);
86 if (elems != null && elems.size() > 0) {
87 resultStr = "<result>";
88 for (int i=0; i<elems.size(); i++) {
89 ExtElement elem = elems.get(i);
90 String elemXmlStr = elem.getXmlString();
91 resultStr = resultStr + elemXmlStr;
92 }
93 resultStr = resultStr + "</result>";
94 }
95 } else if (operationStr.equals("create")) {
96 // TODO
97 } else if (operationStr.equals("update")) {
98 // TODO
99 } else if (operationStr.equals("delete")) {
100 // TODO
101 }
102 } else if (typeStr.equals("query")) {
103 // TODO
104 }
105 result = new ValueSequence();
106 result.add(new StringValue(resultStr));
107 } catch (ApplicationException e) {
108 throw new XPathException(e);
109 }
110 return result;
111 }
112
113 }