No StyleTag.setStyleCode() ?

Help
Gil
2009-04-23
2013-04-27
  • Gil
    Gil
    2009-04-23

    I'm trying to parse an HTML file and then filter the contents of <style> and <script> tags. 

    For <script> tags I noticed that there are both ScriptTag.getScriptCode() and ScriptTag.setScriptCode(String contents) methods.  However, for StyleTag objects, there is only the StyleTag.getScriptCode() method. 

    Does anyone know why there isn't a StyleTag.setStyleCode(String contents) method?  Should I be doing something else?

    Thank you very much for your help!

     
    • Derrick Oswald
      Derrick Oswald
      2009-04-23

      There is a member variable in the ScriptTag class for holding the text contents, but not in StyleTag. It should probably be removed.

      You should be able to do the equivalent to a setStyleCode() by creating a text node, putting it in a NodeList and assigning that as the children collection:

      TextNode text = new TextNode ("the new code");
      NodeList children = new NodeList (text);
      style_tag.setChildren (children);

       
    • Gil
      Gil
      2009-04-24

      Thank you for your help, you put me in the right direction.  I tried what you suggested, but I had a little trouble because I was walking the whole tree using a TreeNodeWalker and when I tried to get to the next node after doing the style_tag.setChildren(newChildren), it would throw a NullPointerException. 

      Since Style nodes generally only have 1 text child node (I think at least), I decided to go with something like this:

                  Node childTextNode = styleNode.getFirstChild();
                 
                  if (childTextNode != null) {
                      String originalContents = childTextNode.getText();
                      String replacedContents = doMyReplacements(originalContents);
                      childTextNode.setText(replacedContents);
                  }

      Thank you again for your help!