| LEFT | RIGHT |
|---|---|
| 1 #!/usr/bin/ruby1.8 -w | 1 #!/usr/bin/ruby1.8 -w |
| 2 # | 2 # |
| 3 # Copyright:: Copyright 2009 Google Inc. | 3 # Copyright:: Copyright 2009 Google Inc. |
| 4 # Original Author:: Ryan Brown (mailto:ribrdb@google.com) | 4 # Original Author:: Ryan Brown (mailto:ribrdb@google.com) |
| 5 # | 5 # |
| 6 # Licensed under the Apache License, Version 2.0 (the "License"); | 6 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 # you may not use this file except in compliance with the License. | 7 # you may not use this file except in compliance with the License. |
| 8 # You may obtain a copy of the License at | 8 # You may obtain a copy of the License at |
| 9 # | 9 # |
| 10 # http://www.apache.org/licenses/LICENSE-2.0 | 10 # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 # | 11 # |
| 12 # Unless required by applicable law or agreed to in writing, software | 12 # Unless required by applicable law or agreed to in writing, software |
| 13 # distributed under the License is distributed on an "AS IS" BASIS, | 13 # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 # See the License for the specific language governing permissions and | 15 # See the License for the specific language governing permissions and |
| 16 # limitations under the License. | 16 # limitations under the License. |
| 17 | 17 |
| 18 require 'rubygems' | 18 require 'rubygems' |
| 19 require 'rack' | 19 require 'rack' |
| 20 require 'rexml/document' | 20 require 'rexml/document' |
| 21 require 'rexml/formatters/pretty' | |
| 22 | 21 |
| 23 class WebXmlBuilder < Rack::Builder | 22 class WebXmlBuilder < Rack::Builder |
| 24 DUMMY_APP = Proc.new{|env|} | 23 DUMMY_APP = Proc.new{|env|} |
| 25 | 24 |
| 26 def initialize(&block) | 25 def initialize(&block) |
| 27 @path = "/" | 26 @path = "/" |
| 28 @paths = Hash.new {|h, k| h[k] = []} | 27 @paths = Hash.new {|h, k| h[k] = []} |
| 29 instance_eval(&block) if block_given? | 28 instance_eval(&block) if block_given? |
| 30 end | 29 end |
| 31 | 30 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 46 ensure | 45 ensure |
| 47 @path = saved_path | 46 @path = saved_path |
| 48 end | 47 end |
| 49 end | 48 end |
| 50 end | 49 end |
| 51 | 50 |
| 52 def run(app) | 51 def run(app) |
| 53 @paths[@path] << [app, [], nil] | 52 @paths[@path] << [app, [], nil] |
| 54 end | 53 end |
| 55 | 54 |
| 56 def write | 55 def to_xml |
| 57 xml = REXML::Document.new.add_element('web-app') | 56 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
| |
| 58 xml.add_attribute("xmlns", "http://java.sun.com/xml/ns/javaee") | 57 xml.add_attribute("xmlns", "http://java.sun.com/xml/ns/javaee") |
| 59 xml.add_attribute("version", "2.5") | 58 xml.add_attribute("version", "2.5") |
| 60 each_path do |path, objects| | 59 each_path do |path, objects| |
| 61 pattern = path + "*" | 60 pattern = path + "*" |
| 62 objects.each do |object, args, block| | 61 objects.each do |object, args, block| |
| 63 if object.respond_to? :to_xml | 62 if object.respond_to? :to_xml |
| 64 object.to_xml(pattern, xml, *args, &block) | 63 object.to_xml(pattern, xml, *args, &block) |
| 65 end | 64 end |
| 66 end | 65 end |
| 67 end | 66 end |
| 68 Dir.mkdir('WEB-INF') unless File.exists?('WEB-INF') | 67 xml |
| 69 open('WEB-INF/web.xml', 'w') do |out| | |
| 70 formatter = REXML::Formatters::Pretty.new | |
| 71 formatter.compact = true | |
| 72 formatter.write(xml, out) | |
| 73 end | |
| 74 end | 68 end |
| 75 | 69 |
| 76 private | 70 private |
| 77 | 71 |
| 78 def each_path | 72 def each_path |
| 79 @paths.sort {|a, b| b[0].length - a[0].length}.each do |path, value| | 73 @paths.sort {|a, b| b[0].length - a[0].length}.each do |path, value| |
| 80 yield path, value | 74 yield path, value |
| 81 end | 75 end |
| 82 end | 76 end |
| 83 end | 77 end |
| 84 | 78 |
| 85 class JavaServlet | 79 class JavaServlet |
| 86 def initialize(klass, name=nil) | 80 def initialize(klass, name=nil) |
| 87 @klass = klass.to_s | 81 @klass = klass.to_s |
| 88 @name = (name || klass).to_s | 82 @name = (name || klass).to_s |
| 89 end | 83 end |
| 90 | 84 |
| 91 def call(env) | 85 def call(env) |
| 92 raise RuntimeError, "JavaServlet should be dispatched by web.xml" | 86 raise RuntimeError, "JavaServlet should be dispatched by web.xml" |
| 93 end | 87 end |
| 94 | 88 |
| 95 def to_xml(pattern, xml) | 89 def to_xml(pattern, xml) |
| 96 servlet = REXML::Element.new('servlet') | 90 servlet = xml.add_element('servlet') |
| 97 servlet.add_element('servlet-name').add_text(@name) | 91 servlet.add_element('servlet-name').add_text(@name) |
| 98 servlet.add_element('servlet-class').add_text(@klass) | 92 servlet.add_element('servlet-class').add_text(@klass) |
| 99 xml.add_element(servlet) | 93 map = xml.add_elemtn('servlet-mapping') |
| 100 map = REXML::Element.new('servlet-mapping') | |
| 101 map.add_element('servlet-name').add_text(@name) | 94 map.add_element('servlet-name').add_text(@name) |
| 102 map.add_element('url-pattern').add_text(pattern.to_s) | 95 map.add_element('url-pattern').add_text(pattern.to_s) |
| 103 xml.add_element(map) | |
| 104 end | 96 end |
| 105 end | 97 end |
| 106 | 98 |
|
woodie
2009/07/02 03:33:09
I feel like 'e' would be fine here
e = REXML::Ele
ribrdb
2009/07/02 22:32:15
I like having something to distinguish between the
| |
| 107 class JavaServletFilter | 99 class JavaServletFilter |
| 108 def self.to_xml(pattern, xml, klass, name=nil) | 100 def self.to_xml(pattern, xml, klass, name=nil) |
| 109 name ||= klass | 101 name ||= klass |
| 110 filter = REXML::Element.new('filter') | 102 filter = xml.add_element('filter') |
| 111 filter.add_element('filter-name').add_text(name.to_s) | 103 filter.add_element('filter-name').add_text(name.to_s) |
| 112 filter.add_element('filter-class').add_text(klass.to_s) | 104 filter.add_element('filter-class').add_text(klass.to_s) |
| 113 xml.add_element(filter) | 105 map = xml.add_element('filter-mapping') |
| 114 map = REXML::Element.new('filter-mapping') | |
| 115 map.add_element('filter-name').add_text(name.to_s) | 106 map.add_element('filter-name').add_text(name.to_s) |
| 116 map.add_element('url-pattern').add_text(pattern.to_s) | 107 map.add_element('url-pattern').add_text(pattern.to_s) |
| 117 xml.add_element(map) | |
| 118 end | 108 end |
| 119 | 109 |
| 120 def self.new(app, *args, &block) | 110 def self.new(app, *args, &block) |
| 121 app | 111 app |
| 122 end | 112 end |
| 123 end | 113 end |
| 124 | 114 |
| 125 class JavaContextListener | 115 class JavaContextListener |
| 126 def self.to_xml(pattern, xml, klass) | 116 def self.to_xml(pattern, xml, klass) |
| 127 listener = REXML::Element.new('listener') | 117 listener = xml.add_element('listener') |
| 128 listener.add_element('listener-class').add_text(klass.to_s) | 118 listener.add_element('listener-class').add_text(klass.to_s) |
| 129 xml.add_element(listener) | |
| 130 end | 119 end |
| 131 | 120 |
| 132 def self.new(app, *args, &block) | 121 def self.new(app, *args, &block) |
| 133 app | 122 app |
| 134 end | 123 end |
| 135 end | 124 end |
| 136 | 125 |
| 137 class JavaContextParams | 126 class JavaContextParams |
| 138 def self.to_xml(pattern, xml, params) | 127 def self.to_xml(pattern, xml, params) |
| 139 params.each do |name, value| | 128 params.each do |name, value| |
| 140 xml_params = REXML::Element.new('context-param') | 129 e = xml.add_element('context-param') |
| 141 xml_params.add_element('param-name').add_text(name.to_s) | 130 e.add_element('param-name').add_text(name.to_s) |
| 142 xml_params.add_element('param-value').add_text(value.to_s) | 131 e.add_element('param-value').add_text(value.to_s) |
| 143 xml.add_element(xml_params) | |
| 144 end | 132 end |
| 145 end | 133 end |
| 146 | 134 |
| 147 def self.new(app, *args, &block) | 135 def self.new(app, *args, &block) |
| 148 app | 136 app |
| 149 end | 137 end |
| 150 end | 138 end |
| LEFT | RIGHT |