001package org.apache.maven.doxia.module.xhtml; 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 javax.swing.text.html.HTML.Attribute; 023 024import org.apache.maven.doxia.macro.MacroExecutionException; 025import org.apache.maven.doxia.parser.Parser; 026import org.apache.maven.doxia.parser.XhtmlBaseParser; 027import org.apache.maven.doxia.sink.Sink; 028import org.apache.maven.doxia.sink.SinkEventAttributeSet; 029 030import org.codehaus.plexus.component.annotations.Component; 031import org.codehaus.plexus.util.xml.pull.XmlPullParser; 032import org.codehaus.plexus.util.xml.pull.XmlPullParserException; 033 034/** 035 * Parse an xhtml model and emit events into a Doxia Sink. 036 * 037 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a> 038 * @version $Id$ 039 * @since 1.0 040 */ 041@Component( role = Parser.class, hint = "xhtml" ) 042public class XhtmlParser 043 extends XhtmlBaseParser 044 implements XhtmlMarkup 045{ 046 /** For boxed verbatim. */ 047 private boolean boxed; 048 049 /** Empty elements don't write a closing tag. */ 050 private boolean isEmptyElement; 051 052 /** {@inheritDoc} */ 053 protected void handleStartTag( XmlPullParser parser, Sink sink ) 054 throws XmlPullParserException, MacroExecutionException 055 { 056 isEmptyElement = parser.isEmptyElementTag(); 057 058 SinkEventAttributeSet attribs = getAttributesFromParser( parser ); 059 060 if ( parser.getName().equals( HTML.toString() ) ) 061 { 062 //Do nothing 063 return; 064 } 065 else if ( parser.getName().equals( HEAD.toString() ) ) 066 { 067 sink.head( attribs ); 068 } 069 else if ( parser.getName().equals( TITLE.toString() ) ) 070 { 071 sink.title( attribs ); 072 } 073 else if ( parser.getName().equals( META.toString() ) ) 074 { 075 String name = parser.getAttributeValue( null, Attribute.NAME.toString() ); 076 String content = parser.getAttributeValue( null, Attribute.CONTENT.toString() ); 077 078 if ( "author".equals( name ) ) 079 { 080 sink.author( null ); 081 082 sink.text( content ); 083 084 sink.author_(); 085 } 086 else if ( "date".equals( name ) ) 087 { 088 sink.date( null ); 089 090 sink.text( content ); 091 092 sink.date_(); 093 } 094 else 095 { 096 sink.unknown( "meta", new Object[] { Integer.valueOf( TAG_TYPE_SIMPLE ) }, attribs ); 097 } 098 } 099 /* 100 * The ADDRESS element may be used by authors to supply contact information 101 * for a model or a major part of a model such as a form. This element 102 * often appears at the beginning or end of a model. 103 */ 104 else if ( parser.getName().equals( ADDRESS.toString() ) ) 105 { 106 sink.author( attribs ); 107 } 108 else if ( parser.getName().equals( BODY.toString() ) ) 109 { 110 sink.body( attribs ); 111 } 112 else if ( parser.getName().equals( DIV.toString() ) ) 113 { 114 String divclass = parser.getAttributeValue( null, Attribute.CLASS.toString() ); 115 116 if ( "source".equals( divclass ) ) 117 { 118 this.boxed = true; 119 } 120 121 baseStartTag( parser, sink ); // pick up other divs 122 } 123 /* 124 * The PRE element tells visual user agents that the enclosed text is 125 * "preformatted". When handling preformatted text, visual user agents: 126 * - May leave white space intact. 127 * - May render text with a fixed-pitch font. 128 * - May disable automatic word wrap. 129 * - Must not disable bidirectional processing. 130 * Non-visual user agents are not required to respect extra white space 131 * in the content of a PRE element. 132 */ 133 else if ( parser.getName().equals( PRE.toString() ) ) 134 { 135 if ( boxed ) 136 { 137 attribs.addAttributes( SinkEventAttributeSet.BOXED ); 138 } 139 140 verbatim(); 141 142 sink.verbatim( attribs ); 143 } 144 else if ( !baseStartTag( parser, sink ) ) 145 { 146 if ( isEmptyElement ) 147 { 148 handleUnknown( parser, sink, TAG_TYPE_SIMPLE ); 149 } 150 else 151 { 152 handleUnknown( parser, sink, TAG_TYPE_START ); 153 } 154 155 if ( getLog().isDebugEnabled() ) 156 { 157 String position = "[" + parser.getLineNumber() + ":" 158 + parser.getColumnNumber() + "]"; 159 String tag = "<" + parser.getName() + ">"; 160 161 getLog().debug( "Unrecognized xhtml tag: " + tag + " at " + position ); 162 } 163 } 164 } 165 166 /** {@inheritDoc} */ 167 protected void handleEndTag( XmlPullParser parser, Sink sink ) 168 throws XmlPullParserException, MacroExecutionException 169 { 170 if ( parser.getName().equals( HTML.toString() ) ) 171 { 172 //Do nothing 173 return; 174 } 175 else if ( parser.getName().equals( HEAD.toString() ) ) 176 { 177 sink.head_(); 178 } 179 else if ( parser.getName().equals( TITLE.toString() ) ) 180 { 181 sink.title_(); 182 } 183 else if ( parser.getName().equals( BODY.toString() ) ) 184 { 185 consecutiveSections( 0, sink ); 186 187 sink.body_(); 188 } 189 else if ( parser.getName().equals( ADDRESS.toString() ) ) 190 { 191 sink.author_(); 192 } 193 else if ( parser.getName().equals( DIV.toString() ) ) 194 { 195 this.boxed = false; 196 baseEndTag( parser, sink ); 197 } 198 else if ( !baseEndTag( parser, sink ) ) 199 { 200 if ( !isEmptyElement ) 201 { 202 handleUnknown( parser, sink, TAG_TYPE_END ); 203 } 204 } 205 206 isEmptyElement = false; 207 } 208 209 /** {@inheritDoc} */ 210 protected void init() 211 { 212 super.init(); 213 214 this.boxed = false; 215 this.isEmptyElement = false; 216 } 217}