001 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry5.internal.services;
016
017 import org.apache.tapestry5.SymbolConstants;
018 import org.apache.tapestry5.internal.parser.ComponentTemplate;
019 import org.apache.tapestry5.ioc.Invokable;
020 import org.apache.tapestry5.ioc.OperationTracker;
021 import org.apache.tapestry5.ioc.Resource;
022 import org.apache.tapestry5.ioc.annotations.Symbol;
023
024 import java.net.URL;
025 import java.util.Map;
026
027 /**
028 * Parses Tapestry XML template files into {@link ComponentTemplate} instances.
029 * A new instance of {@link SaxTemplateParser} is created for each document
030 * parsed.
031 *
032 * @since 5.1.0.0
033 */
034 public class TemplateParserImpl implements TemplateParser
035 {
036 private final Map<String, URL> configuration;
037
038 private final boolean defaultCompressWhitespace;
039
040 private final OperationTracker tracker;
041
042 public TemplateParserImpl(Map<String, URL> configuration,
043
044 @Symbol(SymbolConstants.COMPRESS_WHITESPACE)
045 boolean defaultCompressWhitespace, OperationTracker tracker)
046 {
047 this.configuration = configuration;
048 this.defaultCompressWhitespace = defaultCompressWhitespace;
049 this.tracker = tracker;
050 }
051
052 public ComponentTemplate parseTemplate(final Resource templateResource)
053 {
054 if (!templateResource.exists())
055 throw new RuntimeException(ServicesMessages.missingTemplateResource(templateResource));
056
057 return tracker.invoke("Parsing component template " + templateResource, new Invokable<ComponentTemplate>()
058 {
059 public ComponentTemplate invoke()
060 {
061 return new SaxTemplateParser(templateResource, configuration).parse(defaultCompressWhitespace);
062 }
063 });
064 }
065
066 public Map<String, URL> getDTDURLMappings()
067 {
068 return configuration;
069 }
070 }