1 16 package org.apache.cocoon.xml.xlink; 17 18 import org.apache.cocoon.xml.AbstractXMLPipe; 19 import org.xml.sax.Attributes ; 20 import org.xml.sax.SAXException ; 21 import org.xml.sax.helpers.AttributesImpl ; 22 23 35 36 public abstract class XLinkPipe extends AbstractXMLPipe implements XLinkHandler 37 { 38 39 public static final String XLINK_NAMESPACE_URI = "http://www.w3.org/1999/xlink"; 40 public static final String XLINK_TYPE = "type"; 41 public static final String XLINK_HREF = "href"; 42 public static final String XLINK_ROLE = "role"; 43 public static final String XLINK_ARCROLE = "arcrole"; 44 public static final String XLINK_TITLE = "title"; 45 public static final String XLINK_SHOW = "show"; 46 public static final String XLINK_ACTUATE = "actuate"; 47 public static final String XLINK_LABEL = "label"; 48 public static final String XLINK_FROM = "from"; 49 public static final String XLINK_TO = "to"; 50 public static final String XLINK_TYPE_SIMPLE = "simple"; 51 public static final String XLINK_TYPE_EXTENDED = "extended"; 52 public static final String XLINK_TYPE_LOCATOR = "locator"; 53 public static final String XLINK_TYPE_ARC = "arc"; 54 public static final String XLINK_TYPE_RESOURCE = "resource"; 55 public static final String XLINK_TYPE_TITLE = "title"; 56 57 private String extendedLinkElementName = null; 58 private String extendedLinkElementURI = null; 59 private String linkLocatorElementName = null; 60 private String linkLocatorElementURI = null; 61 private String linkArcElementName = null; 62 private String linkArcElementURI = null; 63 64 public void startElement( String uri, String name, String raw, Attributes attr ) throws SAXException 65 { 66 String type = attr.getValue( XLINK_NAMESPACE_URI, XLINK_TYPE ); 67 if ( type != null ) 68 { 69 if ( type.equals( XLINK_TYPE_SIMPLE ) ) 70 { 71 if ( this.extendedLinkElementName != null ) 72 { 73 throw new SAXException ( "An XLink simple link cannot be included into an 'extended' element" ); 74 } 75 else if ( this.linkLocatorElementName != null ) 76 { 77 throw new SAXException ( "An XLink simple link cannot be included into a 'locator' element" ); 78 } 79 else if ( this.linkArcElementName != null ) 80 { 81 throw new SAXException ( "An XLink simple link cannot be included into an 'arc' element" ); 82 } 83 String href = attr.getValue( XLINK_NAMESPACE_URI, XLINK_HREF ); 84 String role = attr.getValue( XLINK_NAMESPACE_URI, XLINK_ROLE ); 85 String arcrole = attr.getValue( XLINK_NAMESPACE_URI, XLINK_ARCROLE ); 86 String title = attr.getValue( XLINK_NAMESPACE_URI, XLINK_TITLE ); 87 String show = attr.getValue( XLINK_NAMESPACE_URI, XLINK_SHOW ); 88 String actuate = attr.getValue( XLINK_NAMESPACE_URI, XLINK_ACTUATE ); 89 simpleLink( href, role, arcrole, title, show, actuate, uri, name, raw, attr ); 90 } 91 else if ( type.equals( XLINK_TYPE_EXTENDED ) ) 92 { 93 if ( this.extendedLinkElementName != null ) 94 { 95 throw new SAXException ( "An XLink extended link cannot include another 'extended' element" ); 96 } 97 else if ( this.linkLocatorElementName != null ) 98 { 99 throw new SAXException ( "An XLink extended link cannot be included into a 'locator' element" ); 100 } 101 else if ( this.linkArcElementName != null ) 102 { 103 throw new SAXException ( "An XLink extended link cannot be included into an 'arc' element" ); 104 } 105 String role = attr.getValue( XLINK_NAMESPACE_URI, XLINK_ROLE ); 106 String title = attr.getValue( XLINK_NAMESPACE_URI, XLINK_TITLE ); 107 this.extendedLinkElementName = name; 108 this.extendedLinkElementURI = uri; 109 startExtendedLink( role, title, uri, name, raw, attr ); 110 } 111 else if ( type.equals( XLINK_TYPE_LOCATOR ) ) 112 { 113 if ( this.extendedLinkElementName == null ) 114 { 115 throw new SAXException ( "An XLink locator must be included into an 'extended' element" ); 116 } 117 else if ( this.linkLocatorElementName != null ) 118 { 119 throw new SAXException ( "An XLink locator cannot be included into another 'locator' element" ); 120 } 121 else if ( this.linkArcElementName != null ) 122 { 123 throw new SAXException ( "An XLink locator cannot be included into an 'arc' element" ); 124 } 125 String href = attr.getValue( XLINK_NAMESPACE_URI, XLINK_HREF ); 126 String role = attr.getValue( XLINK_NAMESPACE_URI, XLINK_ROLE ); 127 String title = attr.getValue( XLINK_NAMESPACE_URI, XLINK_TITLE ); 128 String label = attr.getValue( XLINK_NAMESPACE_URI, XLINK_LABEL ); 129 this.linkLocatorElementName = name; 130 this.linkLocatorElementURI = uri; 131 startLocator( href, role, title, label, uri, name, raw, attr ); 132 } 133 else if ( type.equals( XLINK_TYPE_ARC ) ) 134 { 135 if ( this.extendedLinkElementName == null ) 136 { 137 throw new SAXException ( "An XLink arc must be included into an 'extended' element" ); 138 } 139 else if ( this.linkLocatorElementName != null ) 140 { 141 throw new SAXException ( "An XLink arc cannot be included into a 'locator' element" ); 142 } 143 else if ( this.linkArcElementName != null ) 144 { 145 throw new SAXException ( "An XLink arc cannot be included into another 'arc' element" ); 146 } 147 String arcrole = attr.getValue( XLINK_NAMESPACE_URI, XLINK_ARCROLE ); 148 String title = attr.getValue( XLINK_NAMESPACE_URI, XLINK_TITLE ); 149 String show = attr.getValue( XLINK_NAMESPACE_URI, XLINK_SHOW ); 150 String actuate = attr.getValue( XLINK_NAMESPACE_URI, XLINK_ACTUATE ); 151 String from = attr.getValue( XLINK_NAMESPACE_URI, XLINK_FROM ); 152 String to = attr.getValue( XLINK_NAMESPACE_URI, XLINK_TO ); 153 this.linkArcElementName = name; 154 this.linkArcElementURI = uri; 155 startArc( arcrole, title, show, actuate, from, to, uri, name, raw, attr ); 156 } 157 else if ( type.equals( XLINK_TYPE_RESOURCE ) ) 158 { 159 if ( this.extendedLinkElementName == null ) 160 { 161 throw new SAXException ( "An XLink resource must be included into an 'extended' element" ); 162 } 163 String role = attr.getValue( XLINK_NAMESPACE_URI, XLINK_ROLE ); 164 String title = attr.getValue( XLINK_NAMESPACE_URI, XLINK_TITLE ); 165 String label = attr.getValue( XLINK_NAMESPACE_URI, XLINK_LABEL ); 166 linkResource( role, title, label, uri, name, raw, attr ); 167 } 168 else if ( type.equals( XLINK_TYPE_TITLE ) ) 169 { 170 if ( ( this.extendedLinkElementName == null ) 171 && ( this.linkLocatorElementName == null ) 172 && ( this.linkArcElementName == null ) ) 173 { 174 throw new SAXException ( "An XLink title must be included into an 'extended', 'locator' or 'arc' element" ); 175 } 176 linkTitle( uri, name, raw, attr ); 177 } 178 else 179 { 180 super.startElement( uri, name, raw, attr ); 181 } 182 } 183 else 184 { 185 super.startElement( uri, name, raw, attr ); 186 } 187 } 188 189 public void endElement( String uri, String name, String raw ) throws SAXException 190 { 191 if ( ( name.equals( this.extendedLinkElementName ) ) && ( uri.equals( this.extendedLinkElementURI ) ) ) 192 { 193 this.extendedLinkElementName = null; 194 this.extendedLinkElementURI = null; 195 this.endExtendedLink( uri, name, raw ); 196 } 197 else if ( ( name.equals( this.linkLocatorElementName ) ) && ( uri.equals( this.linkLocatorElementURI ) ) ) 198 { 199 this.linkLocatorElementName = null; 200 this.linkLocatorElementURI = null; 201 this.endLocator( uri, name, raw ); 202 } 203 else if ( ( name.equals( this.linkArcElementName ) ) && ( uri.equals( this.linkArcElementURI ) ) ) 204 { 205 this.linkArcElementName = null; 206 this.linkArcElementURI = null; 207 this.endArc( uri, name, raw ); 208 } 209 else 210 { 211 super.endElement( uri, name, raw ); 212 } 213 } 214 215 217 public void simpleLink( String href, String role, String arcrole, String title, String show, String actuate, String uri, String name, String raw, Attributes attr ) throws SAXException 218 { 219 AttributesImpl newattr = new AttributesImpl ( attr ); 220 int hrefIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_HREF ); 221 int roleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_ROLE ); 222 int arcroleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_ARCROLE ); 223 int titleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_TITLE ); 224 int showIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_SHOW ); 225 int actuateIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_ACTUATE ); 226 if ( hrefIndex > -1 ) 227 newattr.setValue( hrefIndex, href ); 228 if ( roleIndex > -1 ) 229 newattr.setValue( roleIndex, role ); 230 if ( arcroleIndex > -1 ) 231 newattr.setValue( arcroleIndex, arcrole ); 232 if ( titleIndex > -1 ) 233 newattr.setValue( titleIndex, title ); 234 if ( showIndex > -1 ) 235 newattr.setValue( showIndex, show ); 236 if ( actuateIndex > -1 ) 237 newattr.setValue( actuateIndex, actuate ); 238 super.startElement( uri, name, raw, newattr ); 239 } 240 241 public void startExtendedLink( String role, String title, String uri, String name, String raw, Attributes attr ) throws SAXException 242 { 243 AttributesImpl newattr = new AttributesImpl ( attr ); 244 int roleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_ROLE ); 245 int titleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_TITLE ); 246 if ( roleIndex > -1 ) 247 newattr.setValue( roleIndex, role ); 248 if ( titleIndex > -1 ) 249 newattr.setValue( titleIndex, title ); 250 super.startElement( uri, name, raw, newattr ); 251 } 252 253 public void startLocator( String href, String role, String title, String label, String uri, String name, String raw, Attributes attr ) throws SAXException 254 { 255 AttributesImpl newattr = new AttributesImpl ( attr ); 256 int hrefIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_HREF ); 257 int roleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_ROLE ); 258 int titleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_TITLE ); 259 int labelIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_LABEL ); 260 if ( hrefIndex > -1 ) 261 newattr.setValue( hrefIndex, href ); 262 if ( roleIndex > -1 ) 263 newattr.setValue( roleIndex, role ); 264 if ( titleIndex > -1 ) 265 newattr.setValue( titleIndex, title ); 266 if ( labelIndex > -1 ) 267 newattr.setValue( labelIndex, label ); 268 super.startElement( uri, name, raw, newattr ); 269 } 270 271 public void startArc( String arcrole, String title, String show, String actuate, String from, String to, String uri, String name, String raw, Attributes attr ) throws SAXException 272 { 273 AttributesImpl newattr = new AttributesImpl ( attr ); 274 int arcroleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_ARCROLE ); 275 int titleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_TITLE ); 276 int showIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_SHOW ); 277 int actuateIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_ACTUATE ); 278 int fromIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_FROM ); 279 int toIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_TO ); 280 if ( arcroleIndex > -1 ) 281 newattr.setValue( arcroleIndex, arcrole ); 282 if ( titleIndex > -1 ) 283 newattr.setValue( titleIndex, title ); 284 if ( showIndex > -1 ) 285 newattr.setValue( showIndex, show ); 286 if ( actuateIndex > -1 ) 287 newattr.setValue( actuateIndex, actuate ); 288 if ( fromIndex > -1 ) 289 newattr.setValue( actuateIndex, from ); 290 if ( toIndex > -1 ) 291 newattr.setValue( actuateIndex, to ); 292 super.startElement( uri, name, raw, newattr ); 293 } 294 295 public void linkResource( String role, String title, String label, String uri, String name, String raw, Attributes attr ) throws SAXException 296 { 297 AttributesImpl newattr = new AttributesImpl ( attr ); 298 int roleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_ROLE ); 299 int titleIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_TITLE ); 300 int labelIndex = attr.getIndex( XLINK_NAMESPACE_URI, XLINK_LABEL ); 301 if ( roleIndex > -1 ) 302 newattr.setValue( roleIndex, role ); 303 if ( titleIndex > -1 ) 304 newattr.setValue( titleIndex, title ); 305 if ( labelIndex > -1 ) 306 newattr.setValue( labelIndex, label ); 307 super.startElement( uri, name, raw, newattr ); 308 } 309 310 public void linkTitle( String uri, String name, String raw, Attributes attr ) throws SAXException 311 { 312 super.startElement( uri, name, raw, attr ); 313 } 314 315 public void endExtendedLink( String uri, String name, String raw ) throws SAXException 316 { 317 super.endElement( uri, name, raw ); 318 } 319 320 public void endLocator( String uri, String name, String raw ) throws SAXException 321 { 322 super.endElement( uri, name, raw ); 323 } 324 325 public void endArc( String uri, String name, String raw ) throws SAXException 326 { 327 super.endElement( uri, name, raw ); 328 } 329 } 330 331 | Popular Tags |