1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 /* 19 * $Id$ 20 */ 21 22 /* 23 * 24 * LoggingEntityResolver.java 25 * 26 */ 27 package org.apache.qetest.xsl; 28 29 import java.io.IOException; 30 31 import org.apache.qetest.Reporter; 32 import org.xml.sax.EntityResolver; 33 import org.xml.sax.InputSource; 34 import org.xml.sax.SAXException; 35 36 //------------------------------------------------------------------------- 37 38 /** 39 * Implementation of EntityResolver that logs all calls. 40 * Currently just provides default service; returns null. 41 * @author shane_curcuru@lotus.com 42 * @version $Id$ 43 */ 44 public class LoggingEntityResolver implements EntityResolver 45 { 46 47 /** No-op ctor since it's often useful to have one. */ LoggingEntityResolver()48 public LoggingEntityResolver(){} 49 50 /** 51 * Ctor that calls setReporter automatically. 52 * 53 * NEEDSDOC @param r 54 */ LoggingEntityResolver(Reporter r)55 public LoggingEntityResolver(Reporter r) 56 { 57 setReporter(r); 58 } 59 60 /** Our Reporter, who we tell all our secrets to. */ 61 private Reporter reporter; 62 63 /** 64 * Accesor methods for our Reporter. 65 * 66 * NEEDSDOC @param r 67 */ setReporter(Reporter r)68 public void setReporter(Reporter r) 69 { 70 if (r != null) 71 reporter = r; 72 } 73 74 /** 75 * Accesor methods for our Reporter. 76 * 77 * NEEDSDOC ($objectName$) @return 78 */ getReporter()79 public Reporter getReporter() 80 { 81 return (reporter); 82 } 83 84 /** Prefixed to all reporter msg output. */ 85 private String prefix = "ER:"; 86 87 /** Counters for how many entities we've 'resolved'. */ 88 private int entityCtr = 0; 89 90 /** 91 * Accesor methods for entity counter. 92 * 93 * NEEDSDOC ($objectName$) @return 94 */ getEntityCtr()95 public int getEntityCtr() 96 { 97 return entityCtr; 98 } 99 100 /** 101 * Cheap-o string representation of our state. 102 * 103 * NEEDSDOC ($objectName$) @return 104 */ getCounterString()105 public String getCounterString() 106 { 107 return (prefix + "Entities: " + getEntityCtr()); 108 } 109 110 /** Cheap-o string representation of last entity we resolved. */ 111 private String lastEntity = null; 112 113 /** 114 * NEEDSDOC Method setLastEntity 115 * 116 * 117 * NEEDSDOC @param s 118 */ setLastEntity(String s)119 protected void setLastEntity(String s) 120 { 121 lastEntity = s; 122 } 123 124 /** 125 * Accessor for string representation of last entity we resolved. 126 * 127 * NEEDSDOC ($objectName$) @return 128 */ getLastEntity()129 public String getLastEntity() 130 { 131 return lastEntity; 132 } 133 134 /** What loggingLevel to use for reporter.logMsg(). */ 135 private int level = Reporter.DEFAULT_LOGGINGLEVEL; 136 137 /** 138 * Accesor methods; don't think it needs to be synchronized. 139 * 140 * NEEDSDOC @param l 141 */ setLoggingLevel(int l)142 public void setLoggingLevel(int l) 143 { 144 level = l; 145 } 146 147 /** 148 * Accesor methods; don't think it needs to be synchronized. 149 * 150 * NEEDSDOC ($objectName$) @return 151 */ getLoggingLevel()152 public int getLoggingLevel() 153 { 154 return level; 155 } 156 157 /** 158 * Implement this method: just returns null for now. 159 * Also saves the last entity for later retrieval, and counts 160 * how many entities we've 'resolved' overall. 161 * @todo have a settable property to actually return as the InputSource 162 * 163 * NEEDSDOC @param publicId 164 * NEEDSDOC @param systemId 165 * 166 * NEEDSDOC ($objectName$) @return 167 * @exception SAXException never thrown 168 * @exception IOException never thrown 169 */ resolveEntity(String publicId, String systemId)170 public InputSource resolveEntity(String publicId, String systemId) 171 throws SAXException, IOException 172 { 173 174 entityCtr++; 175 176 setLastEntity(publicId + ";" + systemId); 177 178 if (reporter != null) 179 { 180 reporter.logMsg(level, 181 prefix + getLastEntity() + " " 182 + getCounterString()); 183 } 184 185 return null; 186 } 187 } 188