KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lenya > cms > cocoon > acting > HTMLFormSaveAction


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 /* $Id: HTMLFormSaveAction.java 109754 2004-12-04 02:13:25Z antonio $ */
19
20 package org.apache.lenya.cms.cocoon.acting;
21
22 import java.io.File JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.xml.parsers.DocumentBuilder JavaDoc;
29 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
30 import javax.xml.transform.Transformer JavaDoc;
31 import javax.xml.transform.TransformerFactory JavaDoc;
32 import javax.xml.transform.dom.DOMSource JavaDoc;
33 import javax.xml.transform.dom.DOMResult JavaDoc;
34 import javax.xml.transform.stream.StreamResult JavaDoc;
35 import javax.xml.transform.stream.StreamSource JavaDoc;
36
37 import org.apache.avalon.framework.parameters.Parameters;
38 import org.apache.avalon.framework.thread.ThreadSafe;
39 import org.apache.cocoon.acting.AbstractConfigurableAction;
40 import org.apache.cocoon.environment.ObjectModelHelper;
41 import org.apache.cocoon.environment.Redirector;
42 import org.apache.cocoon.environment.Request;
43 import org.apache.cocoon.environment.SourceResolver;
44 import org.apache.commons.lang.StringUtils;
45 import org.apache.lenya.xml.DocumentHelper;
46 import org.apache.lenya.xml.RelaxNG;
47 import org.apache.lenya.xml.XPath;
48
49 import org.w3c.dom.Document JavaDoc;
50 import org.w3c.dom.Node JavaDoc;
51 import org.w3c.dom.NodeList JavaDoc;
52
53 import org.xmldb.common.xml.queries.XObject;
54 import org.xmldb.common.xml.queries.XPathQuery;
55 import org.xmldb.common.xml.queries.XPathQueryFactory;
56 import org.xmldb.common.xml.queries.XUpdateQuery;
57 import org.xmldb.xupdate.lexus.XUpdateQueryImpl;
58
59 /**
60  * FIXME: org.apache.xpath.compiler.XPathParser seems to have problems when
61  * namespaces are not declared within the root element. Unfortunately the XSLTs
62  * (during Cocoon transformation) are moving the namespaces to the elements
63  * which use them! One hack might be to parse the tree for namespaces
64  * (Node.getNamespaceURI), collect them and add them to the document root
65  * element, before sending it through the org.apache.xpath.compiler.XPathParser
66  * (called by XPathAPI)
67  *
68  * FIXME: There seems to be another problem with default namespaces
69  *
70  * WARNING: Internet Explorer sends X and Y coordinates for image buttons. These
71  * have to be treated differently. Mozilla does not send these coordinates.
72  */

73 public class HTMLFormSaveAction extends AbstractConfigurableAction implements ThreadSafe {
74
75     class XUpdateAttributes {
76         public String JavaDoc xupdateAttrExpr = "";
77         public String JavaDoc tagID = "";
78
79         public XUpdateAttributes(String JavaDoc xupdateAttrExpr, String JavaDoc tagID) {
80             this.xupdateAttrExpr = xupdateAttrExpr;
81             this.tagID = tagID;
82         }
83     }
84
85     /**
86      * Save data to temporary file
87      *
88      * @param redirector a <code>Redirector</code> value
89      * @param resolver a <code>SourceResolver</code> value
90      * @param objectModel a <code>Map</code> value
91      * @param source a <code>String</code> value
92      * @param parameters a <code>Parameters</code> value
93      *
94      * @return a <code>Map</code> value
95      *
96      * @exception Exception if an error occurs
97      */

98     public Map JavaDoc act(Redirector redirector, SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc source,
99             Parameters parameters) throws Exception JavaDoc {
100         File JavaDoc sitemap = new File JavaDoc(new URL JavaDoc(resolver.resolveURI("").getURI()).getFile());
101         File JavaDoc file = new File JavaDoc(sitemap.getAbsolutePath() + File.separator
102                 + parameters.getParameter("file"));
103         File JavaDoc schema = new File JavaDoc(sitemap.getAbsolutePath() + File.separator
104                 + parameters.getParameter("schema"));
105         File JavaDoc unnumberTagsXSL = new File JavaDoc(sitemap.getAbsolutePath() + File.separator
106                 + parameters.getParameter("unnumberTagsXSL"));
107         File JavaDoc numberTagsXSL = new File JavaDoc(sitemap.getAbsolutePath() + File.separator
108                 + parameters.getParameter("numberTagsXSL"));
109
110         Request request = ObjectModelHelper.getRequest(objectModel);
111
112         if (request.getParameter("cancel") != null) {
113             getLogger().warn(".act(): Editing has been canceled");
114             file.delete();
115             return null;
116         } else {
117             if (file.isFile()) {
118                 getLogger().debug(".act(): Save modifications to " + file.getAbsolutePath());
119
120                 try {
121                     Document JavaDoc document = null;
122                     DocumentBuilderFactory JavaDoc parserFactory = DocumentBuilderFactory.newInstance();
123                     parserFactory.setValidating(false);
124                     parserFactory.setNamespaceAware(true);
125                     parserFactory.setIgnoringElementContentWhitespace(true);
126                     DocumentBuilder JavaDoc builder = parserFactory.newDocumentBuilder();
127                     document = builder.parse(file.getAbsolutePath());
128                     System.setProperty("org.xmldb.common.xml.queries.XPathQueryFactory",
129                             "org.xmldb.common.xml.queries.xalan2.XPathQueryFactoryImpl");
130
131                     XPathQuery xpath = XPathQueryFactory.newInstance().newXPathQuery();
132                     XUpdateQuery xq = new XUpdateQueryImpl();
133
134                     String JavaDoc editSelect = null;
135                     Enumeration JavaDoc params = request.getParameterNames();
136                     while (params.hasMoreElements()) {
137                         String JavaDoc pname = (String JavaDoc) params.nextElement();
138                         getLogger().debug("Parameter: " + pname + " ("
139                                 + request.getParameter(pname) + ")");
140
141                         // Extract the xpath to edit
142
if (editSelect == null && pname.indexOf("edit[") >= 0
143                                 && pname.endsWith("].x")) {
144                             editSelect = pname.substring(5, pname.length() - 3);
145                             getLogger().debug("Edit: " + editSelect);
146                         }
147
148                         // Make sure we are dealing with an xupdate statement,
149
// else skip
150
if (pname.indexOf("<xupdate:") == 0) {
151                             String JavaDoc select = pname.substring(pname.indexOf("select") + 8);
152                             select = select.substring(0, select.indexOf("\""));
153                             getLogger().debug(".act() Select Node: " + select);
154
155                             // Check if node exists
156
xpath.setQString(select);
157                             XObject result = xpath.execute(document);
158                             NodeList JavaDoc selectionNodeList = result.nodeset();
159                             if (selectionNodeList.getLength() == 0) {
160                                 getLogger()
161                                         .debug(".act(): Node does not exist (might have been deleted during update): "
162                                                 + select);
163                             } else {
164                                 String JavaDoc xupdateModifications = null;
165                                 // now check for the different xupdate
166
// statements, and handle appropriately
167
if (pname.indexOf("xupdate:update-parent") > 0) {
168                                     getLogger().debug("UPDATE PARENT Node: " + pname);
169                                     // CDATA updates need to be handled
170
// seperately
171
if (pname.indexOf("<![CDATA[") > 0) {
172                                         xupdateModifications = updateCDATA(request, pname, true);
173                                     } else {
174                                         xupdateModifications = update(request,
175                                                 pname,
176                                                 select,
177                                                 selectionNodeList,
178                                                 true);
179                                     }
180                                 } else if (pname.indexOf("xupdate:update") > 0) {
181                                     getLogger().debug("UPDATE Node: " + pname);
182                                     // CDATA updates need to be handled
183
// seperately
184
if (pname.indexOf("<![CDATA[") > 0) {
185                                         xupdateModifications = updateCDATA(request, pname, false);
186                                     } else {
187                                         xupdateModifications = update(request,
188                                                 pname,
189                                                 select,
190                                                 selectionNodeList,
191                                                 false);
192                                     }
193                                 } else if (pname.indexOf("xupdate:append") > 0
194                                         && pname.endsWith(">.x")) {
195                                     xupdateModifications = append(pname.substring(0,
196                                             pname.length() - 2));
197                                     // insert-before: in case of select/option
198
} else if (pname.indexOf("xupdate:insert-before") > 0
199                                         && pname.endsWith("/>")) {
200                                     if (!request.getParameter(pname).equals("null")) {
201                                         xupdateModifications = insertBefore(request
202                                                 .getParameter(pname));
203                                     }
204                                     // insert-before: in case of image
205
} else if (pname.indexOf("xupdate:insert-before") > 0
206                                         && pname.endsWith(">.x")) {
207                                     xupdateModifications = insertBefore(pname.substring(0, pname
208                                             .length() - 2));
209                                     // insert-after: in case of select/option
210
} else if (pname.indexOf("xupdate:insert-after") > 0
211                                         && pname.endsWith("/>")) {
212                                     if (!request.getParameter(pname).equals("null")) {
213                                         xupdateModifications = insertAfter(request
214                                                 .getParameter(pname));
215                                     }
216                                     // insert-after: in case of image
217
} else if (pname.indexOf("xupdate:insert-after") > 0
218                                         && pname.endsWith(">.x")) {
219                                     xupdateModifications = insertAfter(pname.substring(0, pname
220                                             .length() - 2));
221                                 } else if (pname.indexOf("xupdate:remove") > 0
222                                         && pname.endsWith("/>.x")) {
223                                     xupdateModifications = remove(pname.substring(0,
224                                             pname.length() - 2));
225                                 } else if (pname.endsWith(">.y")) {
226                                     getLogger().debug("Don't handle this: " + pname);
227                                 } else {
228                                     getLogger().debug("Don't handle this either: " + pname);
229                                 }
230
231                                 // Get hidden namespaces
232
String JavaDoc namespaces = request.getParameter("namespaces");
233
234                                 // Add XML declaration
235
// NOTE: select/option is generating parameter
236
// which should be considered as null
237
if (xupdateModifications != null) {
238                                     xupdateModifications = "<?xml version=\"1.0\"?>"
239                                             + addHiddenNamespaces(namespaces, xupdateModifications);
240                                 }
241
242                                 // now run the assembled xupdate query
243
if (xupdateModifications != null) {
244                                     getLogger().info("Execute XUpdate Modifications: "
245                                             + xupdateModifications);
246                                     xq.setQString(xupdateModifications);
247                                     xq.execute(document);
248                                 } else {
249                                     getLogger()
250                                             .debug("Parameter did not match any xupdate command: "
251                                                     + pname);
252                                 }
253                             }
254                         }
255                     }
256
257                     // Uncomment this for debugging
258
/*
259                      * java.io.StringWriter writer = new java.io.StringWriter();
260                      * org.apache.xml.serialize.OutputFormat OutFormat = new
261                      * org.apache.xml.serialize.OutputFormat("xml", "UTF-8",
262                      * true); org.apache.xml.serialize.XMLSerializer serializer =
263                      * new org.apache.xml.serialize.XMLSerializer(writer,
264                      * OutFormat);
265                      * serializer.asDOMSerializer().serialize((Document)
266                      * document); log.error(".act(): XUpdate Result:
267                      * \n"+writer.toString());
268                      */

269
270                     // validate against relax ng after the updates
271
if (schema.isFile()) {
272                         DocumentHelper.writeDocument(document, new File JavaDoc(file.getCanonicalPath()
273                                 + ".validate"));
274                         String JavaDoc message = validateDocument(schema, new File JavaDoc(file.getCanonicalPath()
275                                 + ".validate"), unnumberTagsXSL);
276                         if (message != null) {
277                             getLogger().error("RELAX NG Validation failed: " + message);
278                             HashMap JavaDoc hmap = new HashMap JavaDoc();
279                             hmap.put("message", "RELAX NG Validation failed: " + message);
280                             return hmap;
281                         }
282                     } else {
283                         getLogger().warn("No such schema: " + schema.getAbsolutePath());
284                     }
285
286                     Document JavaDoc renumberedDocument = renumberDocument(document,
287                             unnumberTagsXSL,
288                             numberTagsXSL);
289                     DocumentHelper.writeDocument(renumberedDocument, file);
290
291                     // check to see if we save and exit
292
if (request.getParameter("save") != null) {
293                         getLogger().info(".act(): Save");
294                         return null;
295                     } else {
296                         /*
297                          * We don't exit
298                          */

299                         HashMap JavaDoc hmap = new HashMap JavaDoc();
300                         if (editSelect != null) {
301                             hmap.put("editSelect", editSelect);
302                         }
303                         return hmap;
304                     }
305                 } catch (NullPointerException JavaDoc e) {
306                     getLogger().error("NullPointerException", e);
307                     HashMap JavaDoc hmap = new HashMap JavaDoc();
308                     hmap.put("message", "NullPointerException");
309                     return hmap;
310                 } catch (Exception JavaDoc e) {
311                     getLogger().error("Exception: " + e.getMessage(), e);
312                     HashMap JavaDoc hmap = new HashMap JavaDoc();
313                     if (e.getMessage() != null) {
314                         hmap.put("message", e.getMessage());
315                     } else {
316                         hmap.put("message", "No message (" + e.getClass().getName() + ")");
317                     }
318                     return hmap;
319                 }
320             } else {
321                 getLogger().error("No such file: " + file.getAbsolutePath());
322                 HashMap JavaDoc hmap = new HashMap JavaDoc();
323                 hmap.put("message", "No such file: " + file.getAbsolutePath());
324                 return hmap;
325             }
326         }
327     }
328
329     /**
330      * Get attributes from original node
331      *
332      * @param node Original node
333      */

334     private XUpdateAttributes getAttributes(Node JavaDoc node) {
335
336         String JavaDoc xupdateString = "";
337         String JavaDoc tagID = "";
338         org.w3c.dom.NamedNodeMap JavaDoc attributes = node.getAttributes();
339         if (attributes != null) {
340             for (int i = 0; i < attributes.getLength(); i++) {
341                 org.w3c.dom.Attr JavaDoc attribute = (org.w3c.dom.Attr JavaDoc) attributes.item(i);
342                 getLogger().debug(".getAttributes(): " + attribute.getName() + " "
343                         + attribute.getValue());
344                 if (!attribute.getName().equals("tagID")) {
345                     String JavaDoc namespace = attribute.getNamespaceURI();
346                     getLogger().debug(".getAttributes(): Namespace: " + namespace);
347                     String JavaDoc namespaceAttribute = "";
348                     if (namespace != null) {
349                         namespaceAttribute = " namespace=\"" + namespace + "\"";
350                     }
351                     xupdateString = xupdateString + "<xupdate:attribute name=\""
352                             + attribute.getName() + "\"" + namespaceAttribute + ">"
353                             + attribute.getValue() + "</xupdate:attribute>";
354                 } else {
355                     xupdateString = xupdateString
356                             + "<xupdate:attribute name=\"tagID\">temp</xupdate:attribute>";
357                     tagID = attribute.getValue();
358                 }
359             }
360         } else {
361             xupdateString = "";
362         }
363         getLogger().debug("Attributes: " + xupdateString);
364
365         return new XUpdateAttributes(xupdateString, tagID);
366     }
367
368     /**
369      * Get attributes from actual update
370      *
371      * @param update The actual update
372      */

373     private XUpdateAttributes getAttributes(String JavaDoc update, String JavaDoc tagID) {
374         getLogger().debug(update);
375
376         String JavaDoc xupdateString = "<xupdate:attribute name=\"tagID\">temp</xupdate:attribute>";
377
378         String JavaDoc[] attributes = update.substring(0, update.indexOf(">")).split(" ");
379         for (int i = 1; i < attributes.length; i++) {
380             // TODO: beware of white spaces
381
int index = attributes[i].indexOf("=");
382             if (index > 0) {
383                 String JavaDoc name = attributes[i].substring(0, index);
384                 String JavaDoc value = attributes[i].substring(index + 2, attributes[i].length() - 1);
385                 if (name.indexOf("xmlns") < 0) {
386                     xupdateString = xupdateString + "<xupdate:attribute name=\"" + name + "\">"
387                             + value + "</xupdate:attribute>";
388                 }
389             }
390         }
391
392         getLogger().debug("Attributes: " + xupdateString);
393
394         return new XUpdateAttributes(xupdateString, tagID);
395     }
396
397     /**
398      * xupdate:update
399      *
400      * @param parent If true then parent element is part of update and
401      * attributes need to be updated resp. added or deleted
402      */

403     private String JavaDoc update(Request request, String JavaDoc pname, String JavaDoc select, NodeList JavaDoc selectionNodeList,
404             boolean parent) {
405         getLogger().debug("Update node: " + select);
406
407         Node JavaDoc nodeToCopy = selectionNodeList.item(0);
408         // deal with attribute values here..
409
if (nodeToCopy.getNodeType() == Node.ATTRIBUTE_NODE) {
410             getLogger().debug("Update attribute: " + select);
411
412             String JavaDoc xupdateUpdate = pname + request.getParameter(pname) + "</xupdate:update>";
413             return "<xupdate:modifications xmlns:xupdate=\"http://www.xmldb.org/xupdate\">"
414                     + xupdateUpdate + "</xupdate:modifications>";
415             /*
416              * And deal with mixed content here.. NOTE: Lexus has trouble with
417              * mixed content. As Workaround we insert-after the new node, remove
418              * the original node and replace the temporary tagID by the original
419              * tagID.
420              */

421         } else {
422             getLogger().debug("Update element: " + select);
423
424             String JavaDoc namespace = nodeToCopy.getNamespaceURI();
425             String JavaDoc namespaceAttribute = "";
426             if (namespace != null) {
427                 namespaceAttribute = " namespace=\"" + namespace + "\"";
428             }
429             // NOTE: getAttributes adds the attribute tagID with value "temp",
430
// which will be replaced further down
431
XUpdateAttributes xa = getAttributes(nodeToCopy);
432             String JavaDoc xupdateInsertAfter = null;
433             if (parent) {
434                 xa = getAttributes(request.getParameter(pname), xa.tagID);
435                 xupdateInsertAfter = "<xupdate:insert-after select=\"" + select
436                         + " \"><xupdate:element name=\""
437                         + new XPath(select).getNameWithoutPredicates() + "\"" + namespaceAttribute
438                         + ">" + xa.xupdateAttrExpr + removeParent(request.getParameter(pname))
439                         + "</xupdate:element></xupdate:insert-after>";
440             } else {
441                 xupdateInsertAfter = "<xupdate:insert-after select=\"" + select
442                         + " \"><xupdate:element name=\""
443                         + new XPath(select).getNameWithoutPredicates() + "\"" + namespaceAttribute
444                         + ">" + xa.xupdateAttrExpr + request.getParameter(pname)
445                         + "</xupdate:element></xupdate:insert-after>";
446             }
447             getLogger().debug(".update(): Update Node (insert-after): " + xupdateInsertAfter);
448
449             String JavaDoc xupdateRemove = "<xupdate:remove select=\"" + select + " \"/>";
450             getLogger().debug(".update(): Update Node (remove): " + xupdateRemove);
451
452             String JavaDoc xupdateUpdateAttribute = "<xupdate:update select=\""
453                     + new XPath(select).removePredicates(select) + "[@tagID='temp']/@tagID"
454                     + " \">" + xa.tagID + "</xupdate:update>";
455             getLogger().debug(".update(): Update Node (update tagID attribute): "
456                     + xupdateUpdateAttribute);
457
458             return "<xupdate:modifications xmlns:xupdate=\"http://www.xmldb.org/xupdate\">"
459                     + xupdateInsertAfter + xupdateRemove + xupdateUpdateAttribute
460                     + "</xupdate:modifications>";
461         }
462     }
463
464     /**
465      * xupdate:update CDATA
466      *
467      * @param parent if true then attributes of parent will also be updated
468      */

469     private String JavaDoc updateCDATA(Request request, String JavaDoc pname, boolean parent) {
470         String JavaDoc xupdateUpdate = pname + request.getParameter(pname) + "]]></xupdate:update>";
471         return "<xupdate:modifications xmlns:xupdate=\"http://www.xmldb.org/xupdate\">"
472                 + xupdateUpdate + "</xupdate:modifications>";
473     }
474
475     /**
476      * xupdate:append
477      */

478     private String JavaDoc append(String JavaDoc pname) {
479         getLogger().debug(".append() APPEND Node: " + pname);
480         return "<xupdate:modifications xmlns:xupdate=\"http://www.xmldb.org/xupdate\">" + pname
481                 + "</xupdate:modifications>";
482     }
483
484     /**
485      * xupdate:insert-before
486      */

487     private String JavaDoc insertBefore(String JavaDoc pname) {
488         getLogger().debug(".insertBefore() INSERT-BEFORE Node: " + pname);
489         return "<xupdate:modifications xmlns:xupdate=\"http://www.xmldb.org/xupdate\">" + pname
490                 + "</xupdate:modifications>";
491     }
492
493     /**
494      * xupdate:insert-after
495      */

496     private String JavaDoc insertAfter(String JavaDoc pname) {
497         getLogger().debug(".insertAfter() INSERT-AFTER Node: " + pname);
498         return "<xupdate:modifications xmlns:xupdate=\"http://www.xmldb.org/xupdate\">" + pname
499                 + "</xupdate:modifications>";
500     }
501
502     /**
503      * xupdate:remove
504      */

505     private String JavaDoc remove(String JavaDoc pname) {
506         getLogger().debug(".remove() REMOVE Node: " + pname);
507         return "<xupdate:modifications xmlns:xupdate=\"http://www.xmldb.org/xupdate\">" + pname
508                 + "</xupdate:modifications>";
509     }
510
511     /**
512      * Validate document
513      */

514     private String JavaDoc validateDocument(File JavaDoc schema, File JavaDoc file, File JavaDoc unnumberTagsXSL) {
515         try {
516             // Remove tagIDs
517
TransformerFactory JavaDoc tf = TransformerFactory.newInstance();
518             Transformer JavaDoc t = tf.newTransformer(new StreamSource JavaDoc(unnumberTagsXSL));
519             t.transform(new StreamSource JavaDoc(file), new StreamResult JavaDoc(new File JavaDoc(file.getAbsolutePath()
520                     + ".unnumber")));
521
522             // Validate
523
return RelaxNG.validate(schema, new File JavaDoc(file.getAbsolutePath() + ".unnumber"));
524         } catch (Exception JavaDoc e) {
525             getLogger().error("Validating failed:", e);
526             return "" + e;
527         }
528     }
529
530     /**
531      * Renumber document
532      */

533     private Document JavaDoc renumberDocument(Document JavaDoc doc, File JavaDoc unnumberTagsXSL, File JavaDoc numberTagsXSL) {
534
535         try {
536             DocumentBuilderFactory JavaDoc parserFactory = DocumentBuilderFactory.newInstance();
537             parserFactory.setValidating(false);
538             parserFactory.setNamespaceAware(true);
539             parserFactory.setIgnoringElementContentWhitespace(true);
540             DocumentBuilder JavaDoc builder = parserFactory.newDocumentBuilder();
541
542             TransformerFactory JavaDoc tf = TransformerFactory.newInstance();
543
544             // Remove tagIDs
545
Transformer JavaDoc ut = tf.newTransformer(new StreamSource JavaDoc(unnumberTagsXSL));
546             Document JavaDoc unnumberedDocument = builder.newDocument();
547             ut.transform(new DOMSource JavaDoc(doc), new DOMResult JavaDoc(unnumberedDocument));
548
549             // Add tagIDs
550
Transformer JavaDoc nt = tf.newTransformer(new StreamSource JavaDoc(numberTagsXSL));
551             Document JavaDoc renumberedDocument = builder.newDocument();
552             nt.transform(new DOMSource JavaDoc(unnumberedDocument), new DOMResult JavaDoc(renumberedDocument));
553
554             return renumberedDocument;
555         } catch (Exception JavaDoc e) {
556             getLogger().error("" + e);
557         }
558
559         return null;
560     }
561
562     /**
563      * Remove parent element
564      */

565     private String JavaDoc removeParent(String JavaDoc xmlSnippet) {
566         String JavaDoc xmlSnippetWithoutParent = xmlSnippet;
567         xmlSnippetWithoutParent = xmlSnippetWithoutParent.substring(xmlSnippetWithoutParent
568                 .indexOf(">") + 1);
569         xmlSnippetWithoutParent = StringUtils.reverse(xmlSnippetWithoutParent);
570         xmlSnippetWithoutParent = xmlSnippetWithoutParent.substring(xmlSnippetWithoutParent
571                 .indexOf("<") + 1);
572         xmlSnippetWithoutParent = StringUtils.reverse(xmlSnippetWithoutParent);
573         return xmlSnippetWithoutParent;
574     }
575
576     /**
577      * Add namespaces to xupdate
578      */

579     private String JavaDoc addHiddenNamespaces(String JavaDoc namespaces, String JavaDoc xupdateModifications) {
580         getLogger().debug("Namespaces: " + namespaces);
581
582         if (namespaces == null) {
583             getLogger().debug("No additional namespaces");
584             return xupdateModifications;
585         }
586
587         String JavaDoc[] namespace = namespaces.split(" ");
588         String JavaDoc ns = "";
589         for (int i = 0; i < namespace.length; i++) {
590             if ((ns.indexOf(namespace[i]) < 0) && (xupdateModifications.indexOf(namespace[i]) < 0)) {
591                 ns = ns + " " + namespace[i];
592             } else {
593                 getLogger().debug("Redundant namespace: " + namespace[i]);
594             }
595         }
596
597         int endOfFirstNode = xupdateModifications.indexOf(">");
598         return xupdateModifications.substring(0, endOfFirstNode) + " " + ns
599                 + xupdateModifications.substring(endOfFirstNode);
600     }
601 }
Popular Tags