001package org.apache.maven.doxia.module.xdoc;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.regex.Matcher;
024import java.util.regex.Pattern;
025
026import org.apache.maven.doxia.markup.XmlMarkup;
027import org.apache.maven.doxia.xsd.AbstractXmlValidatorTest;
028import org.codehaus.plexus.util.StringUtils;
029
030/**
031 * Test XDOC files with namespace.
032 *
033 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
034 * @version $Id$
035 * @since 1.0
036 */
037public class XdocValidatorTest
038    extends AbstractXmlValidatorTest
039{
040    /** The xsd to use */
041    private static final File XDOC_XSD = new File( getBasedir(), "/src/main/resources/xdoc-2.0.xsd" );
042
043    /** {@inheritDoc} */
044    protected String[] getIncludes()
045    {
046        return new String[] { "**/*.xml", "**/xdoc/*" };
047    }
048
049    /** {@inheritDoc} */
050    protected String addNamespaces( String content )
051    {
052        Pattern pattern = Pattern.compile( ".*<([A-Za-z][A-Za-z0-9:_.-]*)([^>]*)>.*" );
053        Matcher matcher = pattern.matcher( content );
054        if ( matcher.find() )
055        {
056            String root = matcher.group( 1 );
057            String value = matcher.group( 2 );
058
059            if ( value.indexOf( XDOC_XSD.getName() ) == -1 )
060            {
061                String faqs =
062                    "<" + root + " xmlns=\"http://maven.apache.org/XDOC/2.0\""
063                        + "  xmlns:xsi=\"" + XmlMarkup.XML_NAMESPACE + "\""
064                        + "  xsi:schemaLocation=\"http://maven.apache.org/XDOC/2.0 " + XDOC_XSD.toURI() + "\" ";
065
066                return StringUtils.replace( content, "<" + root, faqs );
067            }
068        }
069
070        return content;
071    }
072
073    @Override
074    public void testValidateFiles()
075        throws Exception
076    {
077        // TODO: super.testValidateFiles() only validates files from doxia-test-docs, what's the point?
078    }
079}