Author: vsiveton Date: Wed Jan 28 12:09:08 2009 New Revision: 738463 URL: http://svn.apache.org/viewvc?rev=738463&view=rev Log: o removed errors when validating an xml with xsd and entities like
Modified: maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/site/xdoc/entityTest.xml maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java Modified: maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/site/xdoc/entityTest.xml URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/site/xdoc/entityTest.xml?rev=738463&r1=738462&r2=738463&view=diff ============================================================================== --- maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/site/xdoc/entityTest.xml (original) +++ maven/doxia/doxia-sitetools/trunk/doxia-site-renderer/src/test/resources/site/xdoc/entityTest.xml Wed Jan 28 12:09:08 2009 @@ -18,8 +18,20 @@ under the License. --> -<!-- Voluntarily no XSD --> -<document> +<!DOCTYPE document [ + <!-- These are the entity sets for ISO Latin 1 characters for the XHTML --> + <!ENTITY % HTMLlat1 PUBLIC "-//W3C//ENTITIES Latin 1 for XHTML//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent"> + %HTMLlat1; + <!-- These are the entity sets for special characters for the XHTML --> + <!ENTITY % HTMLsymbol PUBLIC "-//W3C//ENTITIES Symbols for XHTML//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent"> + %HTMLsymbol; + <!-- These are the entity sets for symbol characters for the XHTML --> + <!ENTITY % HTMLspecial PUBLIC "-//W3C//ENTITIES Special for XHTML//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent"> + %HTMLspecial; +]> +<document xmlns="http://maven.apache.org/XDOC/2.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 file:../../../../../../../doxia/doxia-modules/doxia-module-xdoc/src/main/resources/xdoc-2.0.xsd"> <properties> <title>Test entities, cdatas and comments</title> </properties> Modified: maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java URL: http://svn.apache.org/viewvc/maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java?rev=738463&r1=738462&r2=738463&view=diff ============================================================================== --- maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java (original) +++ maven/doxia/doxia/trunk/doxia-core/src/main/java/org/apache/maven/doxia/parser/AbstractXmlParser.java Wed Jan 28 12:09:08 2009 @@ -523,19 +523,16 @@ hasDoctype = true; } - // 2 if no doctype, check for an xmlns instance + // 2 check for an xmlns instance boolean hasXsd = false; - if ( !hasDoctype ) + matcher = PATTERN_TAG.matcher( content ); + if ( matcher.find() ) { - matcher = PATTERN_TAG.matcher( content ); - if ( matcher.find() ) - { - String value = matcher.group( 2 ); + String value = matcher.group( 2 ); - if ( value.indexOf( XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI ) != -1 ) - { - hasXsd = true; - } + if ( value.indexOf( XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI ) != -1 ) + { + hasXsd = true; } } @@ -546,7 +543,7 @@ { getLog().debug( "Validating the content..." ); } - getXmlReader().parse( new InputSource( new StringReader( content ) ) ); + getXmlReader( hasXsd && hasDoctype ).parse( new InputSource( new StringReader( content ) ) ); } } catch ( IOException e ) @@ -568,10 +565,11 @@ } /** + * @param hasDtdAndXsd to flag the <code>ErrorHandler</code>. * @return an xmlReader instance. * @throws SAXException if any */ - private XMLReader getXmlReader() + private XMLReader getXmlReader( boolean hasDtdAndXsd ) throws SAXException { if ( xmlReader == null ) @@ -585,6 +583,8 @@ xmlReader.setEntityResolver( new CachedFileEntityResolver() ); } + ( (MessagesErrorHandler) xmlReader.getErrorHandler() ).setHasDtdAndXsd( hasDtdAndXsd ); + return xmlReader; } @@ -602,13 +602,27 @@ private static final int TYPE_FATAL = 3; + /** @see org/apache/xerces/impl/msg/XMLMessages.properties#MSG_ELEMENT_NOT_DECLARED */ + private static final Pattern ELEMENT_TYPE_PATTERN = + Pattern.compile( "Element type \".*\" must be declared.", Pattern.DOTALL ); + private final Log log; + private boolean hasDtdAndXsd; + public MessagesErrorHandler( Log log ) { this.log = log; } + /** + * @param hasDtdAndXsd the hasDtdAndXsd to set + */ + protected void setHasDtdAndXsd( boolean hasDtdAndXsd ) + { + this.hasDtdAndXsd = hasDtdAndXsd; + } + /** {...@inheritdoc} */ public void warning( SAXParseException e ) throws SAXException @@ -620,7 +634,20 @@ public void error( SAXParseException e ) throws SAXException { - processException( TYPE_ERROR, e ); + // Workaround for Xerces complaints when an XML with XSD needs also a <!DOCTYPE []> to specify entities + // like + // See http://xsd.stylusstudio.com/2001Nov/post08021.htm + if ( !hasDtdAndXsd ) + { + processException( TYPE_ERROR, e ); + return; + } + + Matcher m = ELEMENT_TYPE_PATTERN.matcher( e.getMessage() ); + if ( !m.find() ) + { + processException( TYPE_ERROR, e ); + } } /** {...@inheritdoc} */