001/* 002 * Copyright 2012 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.core.util; 017 018import org.apache.excalibur.xml.sax.ContentHandlerProxy; 019import org.xml.sax.ContentHandler; 020import org.xml.sax.SAXException; 021import org.xml.sax.ext.LexicalHandler; 022 023/** 024 * "Proxy" handler ignoring startDocument() and endDocument() calls 025 */ 026public class IgnoreRootHandler extends ContentHandlerProxy implements LexicalHandler 027{ 028 private LexicalHandler _lexicalHandler; 029 030 /** 031 * Constructor 032 * @param contentHandler the contentHandler to pass SAX events to. In case the <code>ContentHandler</code> also implements the <code>LexicalHandler</code> interface, it will be honoured. 033 */ 034 public IgnoreRootHandler(ContentHandler contentHandler) 035 { 036 super(contentHandler); 037 038 if (contentHandler instanceof LexicalHandler) 039 { 040 _lexicalHandler = (LexicalHandler) contentHandler; 041 } 042 } 043 044 /** 045 * Constructor 046 * @param contentHandler the contentHandler to pass SAX events to 047 * @param lexicalHandler the lexicalHandler to pass lexical events to. May be null. 048 */ 049 public IgnoreRootHandler(ContentHandler contentHandler, LexicalHandler lexicalHandler) 050 { 051 super(contentHandler); 052 053 _lexicalHandler = lexicalHandler; 054 } 055 056 @Override 057 public void startDocument() throws SAXException 058 { 059 // empty method 060 } 061 062 @Override 063 public void endDocument() throws SAXException 064 { 065 // empty method 066 } 067 068 public void comment(char[] ch, int start, int length) throws SAXException 069 { 070 if (_lexicalHandler != null) 071 { 072 _lexicalHandler.comment(ch, start, length); 073 } 074 } 075 076 public void startCDATA() throws SAXException 077 { 078 if (_lexicalHandler != null) 079 { 080 _lexicalHandler.startCDATA(); 081 } 082 } 083 084 public void endCDATA() throws SAXException 085 { 086 if (_lexicalHandler != null) 087 { 088 _lexicalHandler.endCDATA(); 089 } 090 } 091 092 @Override 093 public void startDTD(String name, String publicId, String systemId) throws SAXException 094 { 095 // Ignore but send it as a comment for understanding purpose 096 String comment = "IGNORING DTD " + name + " PUBLIC \"" + publicId + "\" \"" + systemId + "\""; 097 _lexicalHandler.comment(comment.toCharArray(), 0, comment.length()); 098 } 099 100 @Override 101 public void endDTD() throws SAXException 102 { 103 // empty method 104 } 105 106 public void startEntity(String name) throws SAXException 107 { 108 if (_lexicalHandler != null) 109 { 110 _lexicalHandler.startEntity(name); 111 } 112 } 113 114 public void endEntity(String name) throws SAXException 115 { 116 if (_lexicalHandler != null) 117 { 118 _lexicalHandler.endEntity(name); 119 } 120 } 121}