cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Robin Green" <gree...@hotmail.com>
Subject [PATCH] why id() does not work in Cocoon
Date Thu, 27 Apr 2000 20:33:34 GMT
Thanks for the clue, lefty! It still took a while to figure out though.

The problem is, the DOM feature to get elements by their ID is only 
available in Xerces as a vendor-specific feature, or in DOM2, but not in 
DOM1. And AFAIK Cocoon doesn't require DOM2. Thus a bit of nasty hacking: 
(this builds, but I don't know if it _works_)


--- forked-cocoon/src/org/apache/cocoon/transformer/XalanTransformer.java    
    Mon Mar 20 22:46:22 2000
+++ 
forked-cocoon/build/src/org/apache/cocoon/transformer/XalanTransformer.java 
Thu Apr 27 20:24:42 2000
@@ -4,7 +4,7 @@
                    The Apache Software License, Version 1.1
  
============================================================================

- Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
+ Copyright (C) 1999-2000 The Apache Software Foundation. All rights 
reserved.

  Redistribution and use in source and binary forms, with or without 
modifica-
  tion, are permitted provided that the following conditions are met:
@@ -51,6 +51,7 @@
package org.apache.cocoon.transformer;

import java.io.*;
+import java.lang.reflect.Method;
import java.net.*;
import java.util.*;
import org.w3c.dom.*;
@@ -106,6 +107,7 @@
     }

     class XMLParser extends XMLParserLiaisonDefault {
+
         Parser parser;
         Document document;

@@ -145,5 +147,35 @@
         public boolean supportsSAX() {
             return true;
         }
+
+       public Element getElementByID(String id, Document doc) {
+           // If we know we are using DOM2 or above we can do
+           // return doc.getElementById (id);
+
+           // But this ugly hack should work with any DOM
+           // First test for version number, to avoid hiding real
+           // exceptions below
+           if (isDOM2 (doc)) {
+               try {
+                   Method m = doc.getClass ().getMethod
+                       ("getElementById", new Class [] {String.class});
+                   return (Element) m.invoke (doc, new Object [] {id});
+               }
+               catch (Exception ex) {
+                   if (ex instanceof RuntimeException) {
+                       throw (RuntimeException) ex;
+                   }
+
+                   // should never get here
+                   throw new Error ("Caught unexpected " + ex);
+               }
+           }
+           return null;
+       }
+
+       /** Tests if doc implements DOM2 or higher. */
+       private boolean isDOM2 (Document doc) {
+           return doc.getImplementation ().hasFeature ("XML", "2.0");
+       }
     }
-}
\ No newline at end of file
+}

________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com


Mime
View raw message