001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.markup.transformer;
018
019import org.apache.wicket.AttributeModifier;
020import org.apache.wicket.Component;
021import org.apache.wicket.markup.MarkupResourceStream;
022import org.apache.wicket.model.IModel;
023import org.apache.wicket.model.Model;
024
025/**
026 * A container which output markup will be processes by a XSLT processor prior to writing the output
027 * into the web response. The *.xsl resource must be located in the same path as the nearest parent
028 * with an associated markup and must have a filename equal to the component's id.
029 * <p>
030 * The containers tag will be the root element of the xml data applied for transformation to ensure
031 * the xml data are well formed (single root element). In addition the attribute <code>xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.3-strict.dtd</code>
032 * is added to the root element to allow the XSL processor to handle the wicket namespace.
033 * <p>
034 * Similar to this container, a <code>IBehavior</code> is available which does the same, but does
035 * not require an additional Container.
036 * 
037 * @see org.apache.wicket.markup.transformer.XsltTransformerBehavior
038 * 
039 * @author Juergen Donnerstag
040 */
041public class XsltOutputTransformerContainer extends AbstractOutputTransformerContainer
042{
043        private static final long serialVersionUID = 1L;
044
045        /** An optional xsl file path */
046        private final String xslFile;
047
048        /**
049         * Instead of using the default mechanism to determine the associated XSL file, it is given by
050         * the user.
051         * 
052         * @see org.apache.wicket.Component#Component(String, IModel)
053         * 
054         * @param id
055         *            the wicket:id
056         * @param model
057         *            the model (unused)
058         * @param xslFilePath
059         *            XSL input file path
060         */
061        public XsltOutputTransformerContainer(final String id, final IModel<?> model,
062                final String xslFilePath)
063        {
064                super(id);
065
066                xslFile = xslFilePath;
067
068                // The containers tag will be transformed as well. Thus we make sure that
069                // the xml provided to the xsl processor is well formed (has a single
070                // root element)
071                setTransformBodyOnly(false);
072
073                // Make the XSLT processor happy and allow him to handle the wicket
074                // tags and attributes which are in the wicket namespace
075                add(AttributeModifier.replace("xmlns:wicket",
076                        Model.of(MarkupResourceStream.WICKET_XHTML_DTD)));
077        }
078
079        /**
080         * Construct
081         * 
082         * @param id
083         *            the wicket:id
084         * @param model
085         *            the model (unused)
086         * @see org.apache.wicket.Component#Component(String, IModel)
087         */
088        public XsltOutputTransformerContainer(final String id, final IModel<?> model)
089        {
090                this(id, model, null);
091        }
092
093        /**
094         * Construct
095         * 
096         * @param id
097         *            the wicket:id
098         * @see org.apache.wicket.Component#Component(String)
099         */
100        public XsltOutputTransformerContainer(final String id)
101        {
102                this(id, null, null);
103        }
104
105        @Override
106        public CharSequence transform(final Component component, final CharSequence output)
107                throws Exception
108        {
109                return new XsltTransformer(xslFile).transform(component, output);
110        }
111}