- Add a fallback parser in case Parse::Lex is not available
[videosite.git] / videosite / JSArrayParser.pm
diff --git a/videosite/JSArrayParser.pm b/videosite/JSArrayParser.pm
new file mode 100644 (file)
index 0000000..cadf8e8
--- /dev/null
@@ -0,0 +1,56 @@
+#
+# This is a stub class for more complex JS*ArrayParser objects.
+# It's new() method usually does _not_ return an object of
+# type videosite::JSArrayParser, but rather a child object of itself
+# which is able to do some actual work. The only time new() retuns
+# an videosite::JSArrayParser onject is when no child objects are
+# available.
+#
+
+package videosite::JSArrayParser;
+
+use strict;
+
+sub new {
+    my $class = shift;
+    my $self = {};
+
+    if ($class ne __PACKAGE__) {
+        # We were called from a child object. Return ourselves.
+        return bless($self, $class);
+    }
+
+    # Try to find a child object which is available and return that.
+    #
+    # See if videosite::JSLexArrayParser is available
+    eval {
+        require videosite::JSLexArrayParser;
+    };
+
+    unless($@) {
+        # Available. Return a JSLexArrayParser object
+        return videosite::JSLexArrayParser->new();
+    }
+
+    # See if JSSimleArrayParser is available
+    eval {
+        require videosite::JSSimpleArrayParser;
+    };
+
+    unless ($@) {
+        # Available. Return a JSSimpleArrayParser object
+        return videosite::JSSimpleArrayParser->new();
+    }
+
+    # Nothing available. Return ourselves.
+    return bless($self, $class);
+}
+
+sub parse {
+    my $self = shift;
+
+    # No functionality here
+    return undef;
+}
+
+1;