How to extend built-in objects?

edited June 2016 in Questions about Code

I'm building a fancy XML parser/editor right now, and the syntax of my XML file gives every element a unique ID attribute. I wrote a function that returns a child element if it has an attribute whose value... You know what? Just take a look.

XML getChildAttr(XML Parent, String Attribute, String Value) {
  if (Parent.hasChildren()) {
    for (XML childN : Parent.getChildren()) {
      if (childN.getString(Attribute) != null) {
        if (childN.getString(Attribute).equals(Value)) {
          println(childN);
          return childN;
        }
      }
    }
  }
  return null;
}

I wish to be able to call it like this: Parent.getChildAttr(Attribute, Value);, but that would require it to be a method of the XML class. My question is this: Is it possible to add methods to built-in classes? If it is, how would I do so?

Answers

  • Look at extends?

  • edited June 2016

    Do something like this, then? I was really hoping to be able to alter the XML class itself instead of making a new one.

    class XMLN extends XML {
      XML getChildAttr(XML Parent, String Attribute, String Value) {
        if (Parent.hasChildren()) {
          for (XML childN : Parent.getChildren()) {
            if (childN.getString(Attribute) != null) {
              if (childN.getString(Attribute).equals(Value)) {
                println(childN);
                return childN;
              }
            }
          }
        }
      return null;
    }
    

    Wait... Do I need a constructor for this?

  • edited June 2016 Answer ✓
    • As you had noticed by now, Java can't append members to objects on-the-fly.
    • At least not w/o some brutal, very complex secret techniques. :ar!
    • Instead we need to use extends to established a derived class.
    • However in Processing, class XML isn't supposed to be directly instantiated.
    • Rather we need to use loadXML() or parseXML() functions for it.
    • That's where the real problem lies: Both of them always return an XML, the original 1. :-<
    • But what you need is your hackish derived class instance to be returned. #-o
    • So you're gonna need to define a similar function for loadXML(), but returning a hacked XML.
    • Below's some template attempt I did. You're still gonna need to customize it: I-)
    1. https://GitHub.com/processing/processing/blob/master/core/src/processing/core/PApplet.java#L5787
    2. https://GitHub.com/processing/processing/blob/master/core/src/processing/data/XML.java#L144

    // forum.Processing.org/two/discussion/17028/how-to-extend-built-in-objects
    // GoToLoop (2016-Jun-07)
    
    import java.io.Reader;
    import javax.xml.parsers.ParserConfigurationException;
    import org.xml.sax.SAXException;
    
    XML loadXMLExtended(String filename) {
      return loadXMLExtended(filename, null);
    }
    
    XML loadXMLExtended(String filename, String options) {
      try {
        return new XMLExtended(createReader(filename), options);
      }
      catch (IOException e) {
        throw new RuntimeException(e);
      }
      catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
      }
      catch (SAXException e) {
        throw new RuntimeException(e);
      }
    }
    
    static class XMLExtended extends XML {
      XMLExtended (Reader input) throws
        IOException, ParserConfigurationException, SAXException {
        super(input, null);
      }
    
      XMLExtended (Reader input, String options) throws
        IOException, ParserConfigurationException, SAXException {
        super(input, options);
      }
    
      XML getChildAttr(XML parent, String attribute, String Value) {
        return parent;
      }
    }
    
    void setup() {
      exit();
    }
    
  • oh gosh. I'm gonna just stick with my boring little function, then. Code readability should never be worth using hackish cheats, which themselves are hard to decipher.

  • edited June 2016
    • Making a derived class isn't a hack. Calling it such was just a tongue-in-cheek. :P
    • And we can always place the whole solution aside in a different tab in the PDE, away from our main sketch's sight. ;))
    • Then we just need to call our customized & "hacked" loadXMLExtended() in place of the Processing's original loadXML(). ;)
Sign In or Register to comment.