Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(118)

Unified Diff: lib/appengine-tools/web-xml.rb

Issue 89083: Start the appengine-tools gem (Closed) SVN Base: http://appengine-jruby.googlecode.com/svn/trunk/appengine-tools/
Patch Set: Clean up xml formatting. Created 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side by-side-diff with in-line comments
Download patch
Index: lib/appengine-tools/web-xml.rb
===================================================================
--- lib/appengine-tools/web-xml.rb (revision 0)
+++ lib/appengine-tools/web-xml.rb (revision 0)
@@ -0,0 +1,138 @@
+#!/usr/bin/ruby1.8 -w
+#
+# Copyright:: Copyright 2009 Google Inc.
+# Original Author:: Ryan Brown (mailto:ribrdb@google.com)
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+require 'rubygems'
+require 'rack'
+require 'rexml/document'
+
+class WebXmlBuilder < Rack::Builder
+ DUMMY_APP = Proc.new{|env|}
+
+ def initialize(&block)
+ @path = "/"
+ @paths = Hash.new {|h, k| h[k] = []}
+ instance_eval(&block) if block_given?
+ end
+
+ def use(middleware, *args, &block)
+ if middleware.respond_to? :to_xml
+ @paths[@path] << [middleware, args, block]
+ else
+ @paths[@path] << [middleware.new(DUMMY_APP, *args, &block)]
+ end
+ end
+
+ def map(path, &block)
+ if path !~ %r{^https?://} # we can only natively support path matching
+ saved_path = @path
+ @path = [@path, path].join('/').squeeze('/')
+ begin
+ instance_eval(&block) if block_given?
+ ensure
+ @path = saved_path
+ end
+ end
+ end
+
+ def run(app)
+ @paths[@path] << [app, [], nil]
+ end
+
+ def to_xml
+ xml = REXML::Document.new.add_element('web-app')
woodie 2009/07/03 00:59:21 It may be more appropriate to use doc doc = REXML
+ xml.add_attribute("xmlns", "http://java.sun.com/xml/ns/javaee")
+ xml.add_attribute("version", "2.5")
+ each_path do |path, objects|
+ pattern = path + "*"
+ objects.each do |object, args, block|
+ if object.respond_to? :to_xml
+ object.to_xml(pattern, xml, *args, &block)
+ end
+ end
+ end
+ xml
+ end
+
+private
+
+ def each_path
+ @paths.sort {|a, b| b[0].length - a[0].length}.each do |path, value|
+ yield path, value
+ end
+ end
+end
+
+class JavaServlet
+ def initialize(klass, name=nil)
+ @klass = klass.to_s
+ @name = (name || klass).to_s
+ end
+
+ def call(env)
+ raise RuntimeError, "JavaServlet should be dispatched by web.xml"
+ end
+
+ def to_xml(pattern, xml)
+ servlet = xml.add_element('servlet')
+ servlet.add_element('servlet-name').add_text(@name)
+ servlet.add_element('servlet-class').add_text(@klass)
+ map = xml.add_elemtn('servlet-mapping')
+ map.add_element('servlet-name').add_text(@name)
+ map.add_element('url-pattern').add_text(pattern.to_s)
+ end
+end
+
+class JavaServletFilter
+ def self.to_xml(pattern, xml, klass, name=nil)
+ name ||= klass
+ filter = xml.add_element('filter')
+ filter.add_element('filter-name').add_text(name.to_s)
+ filter.add_element('filter-class').add_text(klass.to_s)
+ map = xml.add_element('filter-mapping')
+ map.add_element('filter-name').add_text(name.to_s)
+ map.add_element('url-pattern').add_text(pattern.to_s)
+ end
+
+ def self.new(app, *args, &block)
+ app
+ end
+end
+
+class JavaContextListener
+ def self.to_xml(pattern, xml, klass)
+ listener = xml.add_element('listener')
+ listener.add_element('listener-class').add_text(klass.to_s)
+ end
+
+ def self.new(app, *args, &block)
+ app
+ end
+end
+
+class JavaContextParams
+ def self.to_xml(pattern, xml, params)
+ params.each do |name, value|
+ e = xml.add_element('context-param')
+ e.add_element('param-name').add_text(name.to_s)
+ e.add_element('param-value').add_text(value.to_s)
+ end
+ end
+
+ def self.new(app, *args, &block)
+ app
+ end
+end
Property changes on: lib/appengine-tools/web-xml.rb
___________________________________________________________________
Name: svn:executable
+ *

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld r497