Re: [xwiki-devs] [xwiki-notifications] r13949 - in sandbox/xwiki-plugin-officeimporter/src/main/java/com/xpn/xwiki/plugin/officeimporter: filter transformer
asiri (SVN) wrote:
Author: asiri Date: 2008-11-04 11:31:26 +0100 (Tue, 04 Nov 2008) New Revision: 13949
+ private void filter(Node node) + { + if (node.hasAttributes()) { + try { + node.getAttributes().removeNamedItem("style"); + } catch (DOMException ex) { + // Not a problem. + } + }
I don't like this... try-catch code is costly, since creating an exception takes a lot of time and memory. Can't you check if the 'style' attribute exists instead? And a catch block in general should indicate an exceptional execution, not a normal, expected case. -- Sergiu Dumitriu http://purl.org/net/sergiu/
On Wed, Nov 5, 2008 at 8:54 AM, Sergiu Dumitriu <[email protected]> wrote:
asiri (SVN) wrote:
Author: asiri Date: 2008-11-04 11:31:26 +0100 (Tue, 04 Nov 2008) New Revision: 13949
+ private void filter(Node node) + { + if (node.hasAttributes()) { + try { + node.getAttributes().removeNamedItem("style"); + } catch (DOMException ex) { + // Not a problem. + } + }
I don't like this... try-catch code is costly, since creating an exception takes a lot of time and memory. Can't you check if the 'style' attribute exists instead?
Fixed with : <code> if (node.hasAttributes() && node.getAttributes().getNamedItem("style") != null) { try { node.getAttributes().removeNamedItem("style"); } catch (DOMException ex) { // Should not occur. } } </code>
And a catch block in general should indicate an exceptional execution, not a normal, expected case.
Agreed. Thanks. - Asiri
-- Sergiu Dumitriu http://purl.org/net/sergiu/ _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
Asiri Rathnayake wrote:
On Wed, Nov 5, 2008 at 8:54 AM, Sergiu Dumitriu <[email protected]> wrote:
asiri (SVN) wrote:
Author: asiri Date: 2008-11-04 11:31:26 +0100 (Tue, 04 Nov 2008) New Revision: 13949 + private void filter(Node node) + { + if (node.hasAttributes()) { + try { + node.getAttributes().removeNamedItem("style"); + } catch (DOMException ex) { + // Not a problem. + } + }
I don't like this... try-catch code is costly, since creating an exception takes a lot of time and memory. Can't you check if the 'style' attribute exists instead?
Fixed with :
<code>
if (node.hasAttributes() && node.getAttributes().getNamedItem("style") != null) {
Isn't there a hasAttribute(attributeName) method? Just asking because this method exists in the DOM Level 2 Core specification. See http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-ElHasAttr
try { node.getAttributes().removeNamedItem("style"); } catch (DOMException ex) { // Should not occur. } }
</code>
And a catch block in general should indicate an exceptional execution, not a normal, expected case.
Agreed.
Thanks.
- Asiri
-- Sergiu Dumitriu http://purl.org/net/sergiu/ _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
_______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
Isn't there a hasAttribute(attributeName) method? Just asking because this method exists in the DOM Level 2 Core specification. See http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-ElHasAttr
It's not available in org.w3c.dom.Node but org.w3c.dom.Element (which extends Node) contains that method. I don't see a way to use Element instead of Node because Element doesn't seem to have a getChildElements() method. Thanks. - Asiri
Asiri Rathnayake wrote:
Isn't there a hasAttribute(attributeName) method? Just asking because this method exists in the DOM Level 2 Core specification. See http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-ElHasAttr
It's not available in org.w3c.dom.Node but org.w3c.dom.Element (which extends Node) contains that method. I don't see a way to use Element instead of Node because Element doesn't seem to have a getChildElements() method.
If Element extends Node, then it inherits all its methods, including getChildElements. -- Sergiu Dumitriu http://purl.org/net/sergiu/
On Wed, Nov 5, 2008 at 7:42 PM, Sergiu Dumitriu <[email protected]> wrote:
Asiri Rathnayake wrote:
Isn't there a hasAttribute(attributeName) method? Just asking because this method exists in the DOM Level 2 Core specification. See http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-ElHasAttr
It's not available in org.w3c.dom.Node but org.w3c.dom.Element (which extends Node) contains that method. I don't see a way to use Element instead of Node because Element doesn't seem to have a getChildElements() method.
If Element extends Node, then it inherits all its methods, including getChildElements.
getChildElements() is not available in either Node or Element. May be I explained wrong. What I meant to say is, I can iterate over Nodes in the dom tree, not Elements. Hope I didn't complicate more. :) Thanks. - Asiri
-- Sergiu Dumitriu http://purl.org/net/sergiu/ _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
Asiri Rathnayake wrote:
On Wed, Nov 5, 2008 at 7:42 PM, Sergiu Dumitriu <[email protected]> wrote:
Asiri Rathnayake wrote:
Isn't there a hasAttribute(attributeName) method? Just asking because this method exists in the DOM Level 2 Core specification. See http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-ElHasAttr
It's not available in org.w3c.dom.Node but org.w3c.dom.Element (which extends Node) contains that method. I don't see a way to use Element instead of Node because Element doesn't seem to have a getChildElements() method.
If Element extends Node, then it inherits all its methods, including getChildElements.
getChildElements() is not available in either Node or Element.
May be I explained wrong. What I meant to say is, I can iterate over Nodes in the dom tree, not Elements.
But you can always use instanceof and cast... -- Sergiu Dumitriu http://purl.org/net/sergiu/
On Wed, Nov 5, 2008 at 8:55 PM, Sergiu Dumitriu <[email protected]> wrote:
Asiri Rathnayake wrote:
On Wed, Nov 5, 2008 at 7:42 PM, Sergiu Dumitriu <[email protected]> wrote:
Asiri Rathnayake wrote:
Isn't there a hasAttribute(attributeName) method? Just asking because this method exists in the DOM Level 2 Core specification. See http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-ElHasAttr
It's not available in org.w3c.dom.Node but org.w3c.dom.Element (which extends Node) contains that method. I don't see a way to use Element instead of Node because Element doesn't seem to have a getChildElements() method.
If Element extends Node, then it inherits all its methods, including getChildElements.
getChildElements() is not available in either Node or Element.
May be I explained wrong. What I meant to say is, I can iterate over Nodes in the dom tree, not Elements.
But you can always use instanceof and cast...
I kind of thought of that. I mean in xhtml all tags should be Elements right ? But wasn't sure of this myself. And if the instanceof check fails, what should be the logic to handle that case ? Won't this complicate the code than it is now ? Thanks. - Asiri
-- Sergiu Dumitriu http://purl.org/net/sergiu/ _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
Asiri Rathnayake wrote:
On Wed, Nov 5, 2008 at 8:55 PM, Sergiu Dumitriu <[email protected]> wrote:
Asiri Rathnayake wrote:
On Wed, Nov 5, 2008 at 7:42 PM, Sergiu Dumitriu <[email protected]> wrote:
Asiri Rathnayake wrote:
Isn't there a hasAttribute(attributeName) method? Just asking because this method exists in the DOM Level 2 Core specification. See http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-ElHasAttr
It's not available in org.w3c.dom.Node but org.w3c.dom.Element (which extends Node) contains that method. I don't see a way to use Element instead of Node because Element doesn't seem to have a getChildElements() method. If Element extends Node, then it inherits all its methods, including getChildElements.
getChildElements() is not available in either Node or Element.
May be I explained wrong. What I meant to say is, I can iterate over Nodes in the dom tree, not Elements.
But you can always use instanceof and cast...
I kind of thought of that. I mean in xhtml all tags should be Elements right ? But wasn't sure of this myself. And if the instanceof check fails, what should be the logic to handle that case ? Won't this complicate the code than it is now ?
From the specs: "attributes of type NamedNodeMap, readonly A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise." So I think you should test for instanceof Element and then cast to Element to have access to hasAttribute method.
Thanks.
- Asiri
-- Sergiu Dumitriu http://purl.org/net/sergiu/ _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
_______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
Asiri Rathnayake wrote:
On Wed, Nov 5, 2008 at 8:55 PM, Sergiu Dumitriu <[email protected]> wrote:
Asiri Rathnayake wrote:
On Wed, Nov 5, 2008 at 7:42 PM, Sergiu Dumitriu <[email protected]> wrote:
Asiri Rathnayake wrote:
Isn't there a hasAttribute(attributeName) method? Just asking because this method exists in the DOM Level 2 Core specification. See http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-ElHasAttr
It's not available in org.w3c.dom.Node but org.w3c.dom.Element (which extends Node) contains that method. I don't see a way to use Element instead of Node because Element doesn't seem to have a getChildElements() method. If Element extends Node, then it inherits all its methods, including getChildElements.
getChildElements() is not available in either Node or Element.
May be I explained wrong. What I meant to say is, I can iterate over Nodes in the dom tree, not Elements.
But you can always use instanceof and cast...
I kind of thought of that. I mean in xhtml all tags should be Elements right ? But wasn't sure of this myself. And if the instanceof check fails, what should be the logic to handle that case ? Won't this complicate the code than it is now ?
If the object is not instanceof Element, then you don't do anything. Only elements can have attributes. The code won't be complicated. -- Sergiu Dumitriu http://purl.org/net/sergiu/
On Thu, Nov 6, 2008 at 11:04 AM, Sergiu Dumitriu <[email protected]> wrote:
Asiri Rathnayake wrote:
On Wed, Nov 5, 2008 at 8:55 PM, Sergiu Dumitriu <[email protected]> wrote:
Asiri Rathnayake wrote:
On Wed, Nov 5, 2008 at 7:42 PM, Sergiu Dumitriu <[email protected]> wrote:
Asiri Rathnayake wrote:
> Isn't there a hasAttribute(attributeName) method? Just asking because > this method exists in the DOM Level 2 Core specification. See > http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-ElHasAttr > It's not available in org.w3c.dom.Node but org.w3c.dom.Element (which extends Node) contains that method. I don't see a way to use Element instead of Node because Element doesn't seem to have a getChildElements() method. If Element extends Node, then it inherits all its methods, including getChildElements.
getChildElements() is not available in either Node or Element.
May be I explained wrong. What I meant to say is, I can iterate over Nodes in the dom tree, not Elements.
But you can always use instanceof and cast...
I kind of thought of that. I mean in xhtml all tags should be Elements right ? But wasn't sure of this myself. And if the instanceof check fails, what should be the logic to handle that case ? Won't this complicate the code than it is now ?
If the object is not instanceof Element, then you don't do anything. Only elements can have attributes. The code won't be complicated.
Fixed with : <code> if (node instanceof Element) { Element element = (Element) node; element.removeAttribute("style"); if (node.hasChildNodes()) { NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { filter(children.item(i)); } } } </code> Seems like we don't need to use the hasAttribute() method because the removeAttribute() javadoc says something like : ".... If no attribute with this name is found, this method has no effect. ..." Thanks Marius & Sergiu :) - Asiri
-- Sergiu Dumitriu http://purl.org/net/sergiu/ _______________________________________________ devs mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/devs
participants (3)
-
Asiri Rathnayake -
Marius Dumitru Florea -
Sergiu Dumitriu