View Javadoc
1   package org.apache.maven.doxia.module.xdoc;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.regex.Matcher;
24  import java.util.regex.Pattern;
25  
26  import org.apache.maven.doxia.markup.XmlMarkup;
27  import org.apache.maven.doxia.xsd.AbstractXmlValidatorTest;
28  import org.codehaus.plexus.util.StringUtils;
29  
30  /**
31   * Test XDOC files with namespace.
32   *
33   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
34   * @since 1.0
35   */
36  public class XdocValidatorTest
37      extends AbstractXmlValidatorTest
38  {
39      /** The xsd to use */
40      private static final File XDOC_XSD = new File( getBasedir(), "/src/main/resources/xdoc-2.0.xsd" );
41  
42      /** {@inheritDoc} */
43      protected String[] getIncludes()
44      {
45          return new String[] { "**/*.xml", "**/xdoc/*" };
46      }
47  
48      /** {@inheritDoc} */
49      protected String addNamespaces( String content )
50      {
51          Pattern pattern = Pattern.compile( ".*<([A-Za-z][A-Za-z0-9:_.-]*)([^>]*)>.*" );
52          Matcher matcher = pattern.matcher( content );
53          if ( matcher.find() )
54          {
55              String root = matcher.group( 1 );
56              String value = matcher.group( 2 );
57  
58              if ( !value.contains( XDOC_XSD.getName() ) )
59              {
60                  String faqs =
61                      "<" + root + " xmlns=\"http://maven.apache.org/XDOC/2.0\""
62                          + "  xmlns:xsi=\"" + XmlMarkup.XML_NAMESPACE + "\""
63                          + "  xsi:schemaLocation=\"http://maven.apache.org/XDOC/2.0 " + XDOC_XSD.toURI() + "\" ";
64  
65                  return StringUtils.replace( content, "<" + root, faqs );
66              }
67          }
68  
69          return content;
70      }
71  
72      @Override
73      public void testValidateFiles()
74      {
75          // TODO: super.testValidateFiles() only validates files from doxia-test-docs, what's the point?
76      }
77  }