001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 * 
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 * 
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.log4j.or.sax;
019
020import org.apache.log4j.or.ObjectRenderer;
021
022import org.xml.sax.Attributes;
023
024/**
025   Render <code>org.xml.sax.Attributes</code> objects.
026
027   @author Ceki G&uuml;lc&uuml;
028   @since 1.2 */
029public class AttributesRenderer implements ObjectRenderer {
030
031  public
032  AttributesRenderer() {
033  }
034
035
036  /**
037     Render a {@link org.xml.sax.Attributes}.
038  */
039  public
040  String  doRender(Object o) {
041    if(o instanceof Attributes) {
042      StringBuffer sbuf = new StringBuffer();
043      Attributes a = (Attributes) o;
044      int len = a.getLength();
045      boolean first = true;
046      for(int i = 0; i < len; i++) {
047        if(first) {
048          first = false;
049        } else {
050          sbuf.append(", ");
051        }
052        sbuf.append(a.getQName(i));
053        sbuf.append('=');
054        sbuf.append(a.getValue(i));
055      }
056      return sbuf.toString();
057    } else {
058      try {
059        return o.toString();
060      } catch(Exception ex) {
061          return ex.toString();
062      }
063    }
064  }
065}
066