source: MDService2/branches/MDService_simple3/src/eu/clarin/cmdi/mdservice/model/WorkspaceProfile.java @ 1838

Last change on this file since 1838 was 1838, checked in by gaba, 12 years ago

comments

File size: 23.4 KB
Line 
1package eu.clarin.cmdi.mdservice.model;
2
3import java.io.ByteArrayInputStream;
4import java.io.File;
5import java.io.FileWriter;
6import java.io.IOException;
7import java.io.InputStream;
8import java.io.StringWriter;
9import java.io.UnsupportedEncodingException;
10import java.text.DateFormat;
11import java.text.SimpleDateFormat;
12import java.util.Date;
13
14import javax.xml.namespace.QName;
15import javax.xml.parsers.DocumentBuilder;
16import javax.xml.parsers.DocumentBuilderFactory;
17import javax.xml.parsers.ParserConfigurationException;
18import javax.xml.transform.OutputKeys;
19import javax.xml.transform.Transformer;
20import javax.xml.transform.TransformerConfigurationException;
21import javax.xml.transform.TransformerException;
22import javax.xml.transform.TransformerFactory;
23import javax.xml.transform.TransformerFactoryConfigurationError;
24import javax.xml.transform.dom.DOMSource;
25import javax.xml.transform.stream.StreamResult;
26import javax.xml.xpath.XPath;
27import javax.xml.xpath.XPathConstants;
28import javax.xml.xpath.XPathExpression;
29import javax.xml.xpath.XPathExpressionException;
30import javax.xml.xpath.XPathFactory;
31
32import org.w3c.dom.DOMException;
33import org.w3c.dom.Document;
34import org.w3c.dom.Element;
35import org.w3c.dom.Node;
36import org.w3c.dom.NodeList;
37import org.xml.sax.SAXException;
38
39import net.sf.json.JSON;
40import net.sf.json.JSONObject;
41import net.sf.json.JSONSerializer;
42import net.sf.json.xml.XMLSerializer;
43import eu.clarin.cmdi.mdservice.internal.Utils;
44
45/**
46 * Main WorkspaceProfile functionality, it means loading data from file to DOM Document,
47 * retrieving data or  data parts  as XMLStream and possibilities of save changed or
48 * new XML elements.
49 * 
50 * @author gaba
51 */
52
53
54
55public class WorkspaceProfile{ 
56
57        private static WorkspaceProfile singleton;
58        private static Document workspace_doc;
59       
60        private String workspacefilename;
61        private String username;
62        private String elementtype;
63        private String qid;
64        private String qsid;
65        //TODO counter not working
66        private static final Integer START_QUERY = 1;
67       
68        // save element type
69        public static String SE_WORKSPACE = "workspace";
70        public static String SE_QUERYSET = "queryset";
71        public static String SE_QUERY = "query";
72        public static String SE_BOOKMARKSET = "bookmarkset";
73        public static String SE_BOOKMARK = "bookmark";
74       
75        public static String USER = "user";
76        public static String SERVER = "server";
77       
78        // xpaths definitions for lookup in workspaceprofile
79        public static String XPATH_EXPR_WORKSPACE = "//Profiles/WorkspaceProfiles/WorkspaceProfile[@user='dummy']";
80        public static String XPATH_EXPR_REPOSITORIES = "//Profiles/WorkspaceProfiles/WorkspaceProfile[@user='server']/Repositories";
81        public static String XPATH_EXPR_OPTIONS = "//Profiles/WorkspaceProfiles/WorkspaceProfile[@user='server']/Options";
82        public static String XPATH_EXPR_TERMSETS = "//Profiles/WorkspaceProfiles/WorkspaceProfile[@user='server']/Termsets";
83        public static String XPATH_EXPR_REPOPATH = "//Profiles/WorkspaceProfiles/WorkspaceProfile[@user='server']/Repositories/item[@name='dummy']/uri";
84        public static String XPATH_EXPR_QUERY_PARENT = "//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item[id='dummy']/Queries";
85        public static String XPATH_EXPR_QUERYSET = "//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item[id='dummy']";
86        public static String XPATH_EXPR_WORKSPACE_QUERY = "//Profiles/WorkspaceProfiles/WorkspaceProfile/Querysets/item/Queries/item[id='dummy']";
87        public static String XPATH_EXPR_BOOKMARKSET = "//Profiles/WorkspaceProfiles/WorkspaceProfile/Bookmarksets/item[id='dummy']";
88        public static String XPATH_EXPR_BOOKMARK_PARENT = "//Profiles/WorkspaceProfiles/WorkspaceProfile/Bookmarksets/item[id='dummy']/Bookmarks";
89       
90        public void setQsid(String qsid) {
91                this.qsid = qsid;
92        }
93
94        public String getQsid() {
95                return qsid;
96        }
97
98        public void setQid(String qid) {
99                this.qid = qid;
100        }
101
102        public String getQid() {
103                return qid;
104        }
105
106        public void setElementtype(String elementtype) {
107                this.elementtype = elementtype;
108        }
109
110        public String getElementtype() {
111                return elementtype;
112        }
113
114        public void setUsername(String username) {
115                this.username = username;
116        }
117
118        public String getUsername() {
119                return username;
120        }
121
122        /**
123         * Contructor provides initialisation.
124         *
125         * @return
126         */
127        public WorkspaceProfile() {
128                Init();
129        }
130       
131        /**
132         * Instance of WorkspaceProfile is created.
133         *
134         * @return
135         */
136        public static WorkspaceProfile getWorkspaceProfile() {
137                if (singleton == null) {
138                        singleton = new WorkspaceProfile();
139                } 
140                return singleton;
141        }
142       
143        /**
144         * Initialisation constists of
145         * a) loading configuration
146         * b) setting up the filename of workspaceprofile.xml
147         * c) calling the document initialisation
148         *
149         */
150        public void Init(){
151                //configure
152                Utils.loadConfig("mdservice", "mdservice.properties", this.getClass().getClassLoader());
153                workspacefilename = Utils.getConfig("workspaceprofile.path") + Utils.getConfig("workspace.file");
154                username = SERVER;
155               
156                if (workspace_doc == null){
157                        initDocument();
158                }
159        }
160       
161        /**
162         * Initialisation of the DOM Document, representation  of the XML file.
163         * If none workspaceprofile.xml exists, the new  DOM document containig profile data is
164         * created and saved as WorkspaceProfile.xml
165         *
166         */
167        public void initDocument() {
168               
169                Integer counter = START_QUERY;
170                File f = new File (workspacefilename);
171                       
172                if (!f.exists()) {
173                                        // create new  counter document
174                                        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
175                                DocumentBuilder docBuilder;
176                                        try {
177                                                docBuilder = docFactory.newDocumentBuilder();
178                                                workspace_doc = docBuilder.newDocument();
179                                                // append root tag <index >
180                                                Element root = (Element) workspace_doc.createElement("Profiles");
181                                                //root.setAttribute("idcounter", "1");
182                                                workspace_doc.appendChild(root);
183                                                Element e = workspace_doc.createElement("Querycounter");
184                                                root.appendChild(e);
185                                                e = workspace_doc.createElement("maxqid");
186                                                e.setNodeValue(counter.toString());
187                                                e = workspace_doc.createElement("WorkspaceProfiles");
188                                                root.appendChild(e);
189
190                                                //updateQuerycounter();
191                                                this.saveDocument();
192                                               
193                                                //log.debug("new document");
194                                        } catch (ParserConfigurationException e) {
195                                                // TODO Auto-generated catch block
196                                                e.printStackTrace();
197                                        }
198                    }
199                        else {
200                                try {
201                                       
202                                DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
203                                DocumentBuilder docBuilder;
204                                        try {
205                                                docBuilder = docFactory.newDocumentBuilder();               
206                                                try {
207                                                                workspace_doc = docBuilder.parse(f);//workspacefilename);
208                                                } catch (SAXException e) {
209                                                                // TODO Auto-generated catch block
210                                                               
211                                                                e.printStackTrace();
212                                                }
213                                        } catch (ParserConfigurationException e) {
214                                                        // TODO Auto-generated catch block
215                                                        e.printStackTrace();
216                                        }
217                                       
218                                 //read counter
219                                 //counter = new Integer(workspace_doc.getFirstChild().getFirstChild().getFirstChild().getNodeValue());
220                                 //init = true;
221                            }  catch (IOException ex){
222                                //log.debug("initCacheCounter:" + ex.toString());
223                                ex.printStackTrace();
224                            }
225                        }
226                       
227                    //return counter;
228                }
229
230        /**
231         * Saving DOM Document representing workspace data into WorkspaceProfile.xml
232         */
233        public void saveDocument() {
234                //public void writeQuerycounter (Integer i) {           
235                       
236                        // first update <index idcounter>
237                        //workspace_doc.getFirstChild().getAttributes().getNamedItem("idcounter").setNodeValue(i.toString());
238                       
239                        //log.debug("writeCacheCounter:" + i.toString());
240                        // write xml
241                        Transformer transformer;
242                        try {
243                                transformer = TransformerFactory.newInstance().newTransformer();
244                                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
245
246                                //initialize StreamResult with File object to save to file
247                                StreamResult result = new StreamResult(new StringWriter());
248                                DOMSource source = new DOMSource(workspace_doc);
249                                try {
250                                        transformer.transform(source, result);
251                                } catch (TransformerException e1) {
252                                        // TODO Auto-generated catch block
253                                        e1.printStackTrace();
254                                }
255
256                                String xmlString = result.getWriter().toString();
257
258                                File f = new File (workspacefilename);
259                                FileWriter fw;
260                                try {
261                                        fw = new FileWriter(f);
262                                        try {
263                                                fw.write(xmlString);
264                                                fw.close();
265                                        } catch (IOException e) {
266                                                // TODO Auto-generated catch block
267                                                e.printStackTrace();
268                                        }       
269                                } catch (IOException e) {
270                                        // TODO Auto-generated catch block
271                                        e.printStackTrace();
272                                }
273                        } catch (TransformerConfigurationException e2) {
274                                // TODO Auto-generated catch block
275                                e2.printStackTrace();
276                        } catch (TransformerFactoryConfigurationError e2) {
277                                // TODO Auto-generated catch block
278                                e2.printStackTrace();
279                        }
280        }
281       
282        /**
283         * Creates the XPath expression text from constant-defined text by replacing the 'dummy'
284         *
285         * @param xpath_expr
286         * @param new_dummy
287         * @return
288         */
289        public String getXPathExpressionDummy(String xpath_expr, String new_dummy){
290
291                return xpath_expr.replace("dummy", new_dummy);
292        }
293
294        /**
295         * TODO Utils ?
296         * Apply the xpath expression to retrieve the particular data from the workspace.
297         *
298         * @param xpath_str
299         * @param type
300         * @return
301         * @throws XPathExpressionException
302         */
303        public Object getWorkspaceData(String xpath_str, QName type) throws XPathExpressionException{
304                XPathFactory factory = XPathFactory.newInstance(); 
305            XPath xpath = factory.newXPath(); 
306            XPathExpression expr;
307                        expr = xpath.compile(xpath_str);
308                        //expression is evaluated with respect to a certain context node which is doc.
309                        Object result = null;
310                        try{
311                        result = expr.evaluate(workspace_doc, type);//XPathConstants.NODESET);
312                        } catch(Exception e){
313                               
314                        }
315
316             return result;
317        }
318        /*
319         * Creates new workspaceprofile structure.
320         *
321         */
322        public void addWorkspaceToDocument(){
323                //{'WorkspaceProfile':{'CustomTermsets':'null',
324                //                     'Termsets':'null',
325                //                     'Repositories':'null',
326                //                     'Querysets':'null','created':'null','lastchanged':'null','profilename':'null'}};
327                Element we = workspace_doc.createElement("WorkspaceProfile");
328                we.setAttribute("user", getUsername().toLowerCase());
329                workspace_doc.getFirstChild().appendChild(we);
330               
331                Element e = workspace_doc.createElement("CustomTermsets");
332                e.setNodeValue("null");
333                we.appendChild(e);
334               
335                e = workspace_doc.createElement("Termsets");
336                e.setNodeValue("null");
337                we.appendChild(e);
338               
339                e = workspace_doc.createElement("Repositories");
340                e.setNodeValue("null");
341                we.appendChild(e);
342               
343                e = workspace_doc.createElement("Querysets");
344                e.setNodeValue("null");
345                we.appendChild(e);
346               
347                DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
348                String strdate = dateFormat.format(new Date()).toString();
349                e = workspace_doc.createElement("created");
350                e.setNodeValue(strdate);
351                we.appendChild(e);
352               
353                e = workspace_doc.createElement("lastchanged");
354                e.setNodeValue(strdate);
355                we.appendChild(e);
356               
357                e = workspace_doc.createElement("profilename");
358                e.setNodeValue(getUsername().toLowerCase());
359                we.appendChild(e);
360        }
361       
362        /**
363         *
364         * Creates the XMLStream from the particular part of the workspaceprofile
365         * according the xpath expression text.
366         * If document is empty, none data is retrieved the new document is created.
367         */
368        public InputStream getXMLStream(String xpath_expr) throws XPathExpressionException, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError{
369                InputStream is;
370                NodeList list = (NodeList) getWorkspaceData(xpath_expr, XPathConstants.NODESET);//result;
371            if (list.getLength() < 1) {
372                addWorkspaceToDocument();
373                list = (NodeList) getWorkspaceData(xpath_expr, XPathConstants.NODESET);
374            }
375            if (list.getLength() > 1){
376                // error
377            }
378            // nodelist2stream
379            is = Utils.document2Stream(list.item(0));
380            return is;
381
382        }
383       
384        //todo .has("Queries")
385        public void parseDataInit(JSONObject json){
386                // workspaceelement
387                if (json.has("Querysets")) {
388                        this.setQid("0");
389                        this.setElementtype(WorkspaceProfile.SE_WORKSPACE);
390                } else {
391                        // Querysetelement
392                        if (json.has("Queries")) {
393                                this.setElementtype(WorkspaceProfile.SE_QUERYSET);
394                                this.setQsid(json.getString("id"));
395                        } else {
396                                if (json.has("Bookmarks")) {
397                                        this.setElementtype(WorkspaceProfile.SE_BOOKMARKSET);
398                                        this.setQsid(json.getString("id"));
399                                } else {
400                                        // Query-Bookmark-element
401                                        if (json.getString("bookmark").equals("1")) {
402                                                this.setElementtype(WorkspaceProfile.SE_BOOKMARK);
403                                        } else {
404                                                // Queryelement
405                                                this.setElementtype(WorkspaceProfile.SE_QUERY);
406                                        }
407                                        this.setQid(json.getString("id"));
408                                }
409                        }
410                }
411
412        }
413       
414        public String getXMLRootName(){
415                String rootname = "WorkspaceProfile";
416                if (this.elementtype.equals(WorkspaceProfile.SE_QUERY)){
417                        rootname = "item";
418                }
419                if (this.elementtype.equals(WorkspaceProfile.SE_QUERYSET)){
420                        rootname = "item";
421                }
422                return rootname;
423        }
424       
425        /**
426         * Save the data of the profile element defined in  json structure into DOM document , which
427         * represents the workspaceprofile.
428         *
429         * @param jsonData
430         * @return
431         * @throws ParserConfigurationException
432         * @throws SAXException
433         * @throws IOException
434         * @throws DOMException
435         * @throws XPathExpressionException
436         * @throws TransformerException
437         * @throws TransformerFactoryConfigurationError
438         */
439        public  String save(String jsonData) throws ParserConfigurationException, SAXException, IOException, DOMException, XPathExpressionException, TransformerException, TransformerFactoryConfigurationError{
440               
441        XMLSerializer serializer = new XMLSerializer();
442                JSON json = JSONSerializer.toJSON( jsonData );
443                // init qid, elementtype
444                parseDataInit((JSONObject)json);
445                serializer.setRootName(getXMLRootName()); //"WorkspaceProfile");       
446                serializer.setElementName("item");
447        serializer.setTypeHintsEnabled(false); 
448        String xml = serializer.write( json );
449        //log.debug("XML:" + xml);
450        InputStream is_xml = new ByteArrayInputStream( xml.getBytes( ) );
451        // add to document
452        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
453        DocumentBuilder builder = factory.newDocumentBuilder();
454        Document new_doc = builder.parse( is_xml );
455        String newid = DocumentReplaceNewElement(new_doc);
456      //TODO
457        saveDocument();
458       
459                return newid.toString();
460               
461        }
462       
463        public String createNewId(){//String name){
464                String name = "maxid";
465                String id = workspace_doc.getElementsByTagName(name).item(0).getTextContent();
466                Integer i = Integer.parseInt(id) + 1;
467                id = i.toString();
468                workspace_doc.getElementsByTagName(name).item(0).setTextContent(id);
469                return id.toString();
470        }
471       
472        /**
473         * Appends or replace the profile element (new_doc) into workspaceprofile DOM Document.
474         *
475         * @param new_doc
476         * @return
477         * @throws XPathExpressionException
478         */
479        public  String  DocumentReplaceNewElement(Document new_doc) throws XPathExpressionException{
480                String newid = "";
481                if (this.elementtype.equals(WorkspaceProfile.SE_WORKSPACE)){
482                        NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_QUERY_PARENT, this.qsid),XPathConstants.NODESET);
483                // remove
484                //childeNode
485                if (wsnodelist.getLength() > 0){
486                        ((Node)wsnodelist.item(0)).getParentNode().removeChild(wsnodelist.item(0));
487                }
488               
489                Node node = new_doc.getFirstChild();
490                Node node2 = workspace_doc.adoptNode(node.cloneNode(true));
491                ((Element)node2).setAttribute("user", this.getUsername());
492                workspace_doc.getElementsByTagName("WorkspaceProfiles").item(0).appendChild(node2);
493                }
494                if (this.elementtype.equals(WorkspaceProfile.SE_QUERY)){
495// new query
496                        Node node = new_doc.getFirstChild();
497                        if (this.qid.equals("0")){
498                                newid = createNewId();//"maxqid");       
499                                new_doc.getElementsByTagName("id").item(0).setTextContent(newid);
500                        Element root = workspace_doc.getDocumentElement();
501                        Node node2 = workspace_doc.adoptNode(node.cloneNode(true));
502                        node2 = workspace_doc.renameNode(node2, "", "item");
503                   // apend to specific queryset
504                                NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_QUERY_PARENT, this.qsid),XPathConstants.NODESET);;
505                                Node anode = wsnodelist.item(0);
506                                if (anode.getTextContent().trim().toLowerCase().equals("null")){
507                                        anode.setTextContent("");
508                                }
509                        anode.appendChild(node2);
510                        } 
511                       
512                        else {
513                                // edit existing query
514                                NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_WORKSPACE_QUERY, this.qsid),XPathConstants.NODESET);;;
515                        // remove
516                        //childeNode
517                        if (wsnodelist.getLength() > 0){
518                                //Element root = workspace_doc.getDocumentElement();
519                                Node node2 = workspace_doc.adoptNode(node.cloneNode(true));
520                                node2 = workspace_doc.renameNode(node2, "", "item");
521                                Node qnode  = (Node)wsnodelist.item(0);
522                                //root.replaceChild(node2,(Node)wsnodelist.item(0));   
523                               
524                                Element parentElement = (Element)qnode.getParentNode();
525                                        parentElement.insertBefore(node2, qnode);
526                                        qnode.getParentNode().removeChild(qnode);
527                                        parentElement.normalize();
528                        }
529                        }
530                }
531                if (this.elementtype.equals(WorkspaceProfile.SE_BOOKMARK)){
532                        // new bookmark
533                                                Node node = new_doc.getFirstChild();
534                                                if (this.qid.equals("0")){
535                                                        newid = createNewId();//"maxbid");       
536                                                        new_doc.getElementsByTagName("id").item(0).setTextContent(newid);
537                                                Element root = workspace_doc.getDocumentElement();
538                                                Node node2 = workspace_doc.adoptNode(node.cloneNode(true));
539                                                node2 = workspace_doc.renameNode(node2, "", "item");
540                                           // apend to specific bookmarkset
541                                                        NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_BOOKMARK_PARENT, this.qsid),XPathConstants.NODESET);
542                                                        Node anode = wsnodelist.item(0);
543                                                        if (anode.getTextContent().trim().toLowerCase().equals("null")){
544                                                                anode.setTextContent("");
545                                                        }
546                                                anode.appendChild(node2);
547                                                } 
548                                               
549                                                else {
550                                                        // edit existing query
551                                                        NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_WORKSPACE_QUERY, this.qsid),XPathConstants.NODESET);
552                                                // remove
553                                                //childeNode
554                                                if (wsnodelist.getLength() > 0){
555                                                        //Element root = workspace_doc.getDocumentElement();
556                                                        Node node2 = workspace_doc.adoptNode(node.cloneNode(true));
557                                                        node2 = workspace_doc.renameNode(node2, "", "item");
558                                                        Node qnode  = (Node)wsnodelist.item(0);
559                                                        //root.replaceChild(node2,(Node)wsnodelist.item(0));   
560                                                       
561                                                        Element parentElement = (Element)qnode.getParentNode();
562                                                                parentElement.insertBefore(node2, qnode);
563                                                                qnode.getParentNode().removeChild(qnode);
564                                                                parentElement.normalize();
565                                                }
566                                                }
567                                        }
568                //new queryset
569                if (this.elementtype.equals(WorkspaceProfile.SE_QUERYSET)){
570                        // new queryset
571                                                Node node = new_doc.getFirstChild();
572                                                if (this.qsid.equals("0")){
573                                                        newid = createNewId();//"maxqsid");     
574                                                        new_doc.getElementsByTagName("id").item(0).setTextContent(newid);
575                                                Element root = workspace_doc.getDocumentElement();
576                                                Node node2 = workspace_doc.adoptNode(node.cloneNode(true));
577                                                node2 = workspace_doc.renameNode(node2, "", "item");
578                                           // apend to specific querysets
579                                                NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_WORKSPACE, this.qsid),XPathConstants.NODESET);
580                                                Element welem = (Element)wsnodelist.item(0);
581                                                        Node anode = welem.getElementsByTagName("Querysets").item(0);
582                                                        if (anode.getTextContent().trim().toLowerCase().equals("null")){
583                                                                anode.setTextContent("");
584                                                        }
585                                                        anode.appendChild(node2);
586                                                } 
587                                               
588                                                else {
589                                                        // edit existing queryset
590                                                        NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_QUERYSET, this.qsid),XPathConstants.NODESET);
591                                                // remove
592                                                //childeNode
593                                                if (wsnodelist.getLength() > 0){
594                                                        //Element root = workspace_doc.getDocumentElement();
595                                                        Node node2 = workspace_doc.adoptNode(node.cloneNode(true));
596                                                        node2 = workspace_doc.renameNode(node2, "", "item");
597                                                        Node qset  = (Node)wsnodelist.item(0);
598                                                       
599                                                        Element parentElement = (Element)qset.getParentNode();
600                                                                parentElement.insertBefore(node2, qset);
601                                                                qset.getParentNode().removeChild(qset);
602                                                                parentElement.normalize();
603                                                }
604                                                }
605                                        }
606               
607                //new bookmarkset
608                if (this.elementtype.equals(WorkspaceProfile.SE_BOOKMARKSET)){
609                        // new queryset
610                                                Node node = new_doc.getFirstChild();
611                                                if (this.qsid.equals("0")){
612                                                        newid = createNewId();//"maxbsid");     
613                                                        new_doc.getElementsByTagName("id").item(0).setTextContent(newid);
614                                                Element root = workspace_doc.getDocumentElement();
615                                                Node node2 = workspace_doc.adoptNode(node.cloneNode(true));
616                                                node2 = workspace_doc.renameNode(node2, "", "item");
617                                           // apend to specific querysets
618                                                NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_WORKSPACE, this.qsid),XPathConstants.NODESET);
619                                                Element welem = (Element)wsnodelist.item(0);
620                                                Node anode = welem.getElementsByTagName("Bookmarksets").item(0);
621                                               if (anode.getTextContent().trim().toLowerCase().equals("null")){
622                                                                anode.setTextContent("");
623                                                        }
624                                                anode.appendChild(node2);
625                                                } 
626                                               
627                                                else {
628                                                        // edit existing bookmarkset
629                                                        NodeList wsnodelist = (NodeList) getWorkspaceData(this.getXPathExpressionDummy(WorkspaceProfile.XPATH_EXPR_BOOKMARKSET, this.qsid),XPathConstants.NODESET);
630                                                // remove
631                                                //childeNode
632                                                if (wsnodelist.getLength() > 0){
633                                                        //Element root = workspace_doc.getDocumentElement();
634                                                        Node node2 = workspace_doc.adoptNode(node.cloneNode(true));
635                                                        node2 = workspace_doc.renameNode(node2, "", "item");
636                                                        Node qset  = (Node)wsnodelist.item(0);
637                                                       
638                                                        Element parentElement = (Element)qset.getParentNode();
639                                                                parentElement.insertBefore(node2, qset);
640                                                                qset.getParentNode().removeChild(qset);
641                                                                parentElement.normalize();
642                                                }
643                                                }
644                                        }
645                return newid;
646        }
647       
648        //TODO Utils
649        public static InputStream createStream(String value) throws TransformerException, TransformerFactoryConfigurationError, ParserConfigurationException, UnsupportedEncodingException{
650        DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
651        DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
652        Document doc = docBuilder.newDocument();
653
654        ////////////////////////
655        //Creating the XML tree
656        Element root = doc.createElement("root");
657        root.setTextContent(value);
658        doc.appendChild(root);
659       
660        DOMSource source = new DOMSource(doc);   
661        StringWriter xmlAsWriter = new StringWriter();   
662        StreamResult result = new StreamResult(xmlAsWriter);     
663        TransformerFactory.newInstance().newTransformer().transform(source, result);   
664         
665        // write changes   
666        ByteArrayInputStream is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8")); 
667           
668
669       return is;
670
671
672        }
673       
674}
675
676
Note: See TracBrowser for help on using the repository browser.