SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 241 | Next

Ola Bini

"Practical JRuby on Rails Web 2.0 Projects: Bringing Ruby on Rails to Java"

However, it??™s still good
to be able to add this later on (see Listing 8-5).
Listing 8-5. In the JDocument Class Definition
def validate_with schema
schema = schema.content if JDocument === schema
@factory.set_namespace_aware true
@factory.set_validating true
@factory.set_attribute "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema"
@factory.set_attribute "http://java.sun.com/xml/jaxp/properties/schemaSource",
input_source_for(schema)
builder = @factory.new_document_builder
builder.set_error_handler RaisingErrorHandler.new
builder.parse input_source_for(content)
end
You need to set a few options on the factory, and then create an InputSource for the
schema provided. I??™ll show the input_source_for method later; it creates an InputSource in
different ways depending on the type of schema. After you??™ve set the validation options, you
can just parse the content again with a RaisingErrorHandler:
CHAPTER 8 ?–  CONTENT RENDERING 159
def transform_with! style, options = {}
style = style.content if JDocument === style
f = TransformerFactory.new_instance
t = f.new_transformer source_for(render_options(style,options))
s = StringIO.new
t.transform source_for(@content), StreamResult.new(IOOutputStream.new(s))
@content = s.string
end
This transformation code does almost exactly the same thing as our previous XSLT translation
example. The only new parts are the source_for and render_options methods. These
are all helpers to make the code easier to read:
private
def render_options content, options
options.


Pages:
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253