Clover coverage report -
Coverage timestamp: Sun Nov 1 2009 23:08:24 UTC
file stats: LOC: 344   Methods: 14
NCLOC: 177   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SetContentHandler.java 0% 0% 0% 0%
coverage
 1    /*
 2    * Licensed to the Apache Software Foundation (ASF) under one or more
 3    * contributor license agreements. See the NOTICE file distributed with
 4    * this work for additional information regarding copyright ownership.
 5    * The ASF licenses this file to You under the Apache License, Version 2.0
 6    * (the "License"); you may not use this file except in compliance with
 7    * the License. You may obtain a copy of the License at
 8    *
 9    * http://www.apache.org/licenses/LICENSE-2.0
 10    *
 11    * Unless required by applicable law or agreed to in writing, software
 12    * distributed under the License is distributed on an "AS IS" BASIS,
 13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14    * See the License for the specific language governing permissions and
 15    * limitations under the License.
 16    *
 17    * $Id: SetContentHandler.java 645329 2008-04-06 23:27:25Z natalia $
 18    */
 19   
 20    package org.apache.xindice.xml.sax;
 21   
 22    import org.apache.commons.logging.Log;
 23    import org.apache.commons.logging.LogFactory;
 24    import org.apache.xindice.util.XMLUtilities;
 25    import org.apache.xindice.util.XindiceRuntimeException;
 26    import org.xml.sax.Attributes;
 27    import org.xml.sax.SAXException;
 28    import org.xml.sax.helpers.DefaultHandler;
 29    import org.xmldb.api.modules.XMLResource;
 30   
 31    import java.util.HashMap;
 32    import java.util.Iterator;
 33    import java.util.Map;
 34   
 35    /**
 36    * Simple ContentHandler that just converts the SAX event stream into a text
 37    * representation of the document and stores it in the associated resource.
 38    *
 39    * <small>
 40    * The only place this class currently used is in
 41    * {@link org.apache.xindice.client.xmldb.resources.XMLResourceImpl#setContentAsSAX}.
 42    * Instead of this class, null-transform using TrAX API can be used.
 43    * </small>
 44    *
 45    * @version $Revision: 645329 $, $Date: 2008-04-06 23:27:25 +0000 (Sun, 06 Apr 2008) $
 46    */
 47    public class SetContentHandler extends DefaultHandler {
 48   
 49    private static final Log log = LogFactory.getLog(SetContentHandler.class);
 50   
 51    protected XMLResource resource = null;
 52    protected StringBuffer newContent = null;
 53    protected Map namespaces = null;
 54   
 55  0 public SetContentHandler(XMLResource resource) {
 56  0 this.resource = resource;
 57  0 namespaces = new HashMap();
 58    }
 59   
 60    /**
 61    * Receive notification of the beginning of the document.
 62    *
 63    * @exception SAXException Description of Exception
 64    * @see org.xml.sax.ContentHandler#startDocument
 65    */
 66  0 public void startDocument()
 67    throws SAXException {
 68  0 newContent = new StringBuffer();
 69  0 newContent.append("<?xml version=\"1.0\"?>");
 70    }
 71   
 72   
 73    /**
 74    * Receive notification of the end of the document.
 75    *
 76    * @exception SAXException Description of Exception
 77    * @see org.xml.sax.ContentHandler#endDocument
 78    */
 79  0 public void endDocument()
 80    throws SAXException {
 81  0 try {
 82  0 resource.setContent(newContent.toString());
 83    } catch (Exception e) {
 84  0 log.warn("Exception in endDocument", e);
 85  0 throw new SAXException(e);
 86    }
 87    }
 88   
 89   
 90    /**
 91    * Receive notification of the start of a Namespace mapping.
 92    *
 93    * @param prefix The Namespace prefix being declared.
 94    * @param uri The Namespace URI mapped to the prefix.
 95    * @exception SAXException Description of Exception
 96    * @see org.xml.sax.ContentHandler#startPrefixMapping
 97    */
 98  0 public void startPrefixMapping(String prefix, String uri)
 99    throws SAXException {
 100  0 namespaces.put(prefix, uri);
 101    }
 102   
 103   
 104    /**
 105    * Receive notification of the end of a Namespace mapping.
 106    *
 107    * @param prefix The Namespace prefix being declared.
 108    * @exception SAXException Description of Exception
 109    * @see org.xml.sax.ContentHandler#endPrefixMapping
 110    */
 111  0 public void endPrefixMapping(String prefix)
 112    throws SAXException {
 113  0 namespaces.remove(prefix);
 114    }
 115   
 116   
 117  0 private String getLocalName(String qn) {
 118  0 if (qn.indexOf(':') != -1) {
 119  0 return qn.substring(qn.indexOf(':') + 1);
 120    } else {
 121  0 return qn;
 122    }
 123    }
 124   
 125  0 private String getQNameAtt(String uri, String localName) throws SAXException {
 126  0 if ("".equals(uri)) {
 127  0 return localName;
 128    }
 129   
 130    /* Look for prefix */
 131  0 String prefix = null;
 132  0 Iterator prefixes = namespaces.keySet().iterator();
 133  0 while (prefixes.hasNext()) {
 134  0 String key = (String) prefixes.next();
 135  0 if ((!("".equals(key))) && namespaces.get(key).equals(uri)) {
 136  0 prefix = key;
 137  0 break;
 138    }
 139    }
 140   
 141  0 if (prefix == null) {
 142  0 throw new SAXException("No declared prefix for namespace '" +
 143    uri + "'.");
 144    }
 145   
 146  0 return prefix + ":" + localName;
 147    }
 148   
 149   
 150  0 private String getQNameElement(String uri, String localName) throws SAXException {
 151  0 if ("".equals(uri)) {
 152  0 if (namespaces.get("") != null) {
 153  0 throw new SAXException("default namespace is declared here!");
 154    } else {
 155   
 156  0 return localName;
 157    }
 158    }
 159   
 160    /* Look for prefix */
 161  0 String prefix = null;
 162  0 Iterator prefixes = namespaces.keySet().iterator();
 163  0 while (prefixes.hasNext()) {
 164  0 String key = (String) prefixes.next();
 165  0 if (namespaces.get(key).equals(uri)) {
 166  0 prefix = key;
 167  0 break;
 168    }
 169    }
 170   
 171  0 if (prefix == null) {
 172  0 throw new SAXException("No declared prefix for namespace '"
 173    + uri + "'.");
 174    }
 175   
 176  0 return ("".equals(prefix) ? localName : prefix + ":" + localName);
 177    }
 178   
 179   
 180    /**
 181    * Receive notification of the start of an element.
 182    *
 183    * @param attributes The specified or defaulted attributes.
 184    * @param uri Description of Parameter
 185    * @param localName Description of Parameter
 186    * @param qName Description of Parameter
 187    * @exception SAXException Description of Exception
 188    * @see org.xml.sax.ContentHandler#startElement
 189    */
 190  0 public void startElement(String uri, String localName, String qName, Attributes attributes)
 191    throws SAXException {
 192   
 193  0 newContent.append("<");
 194   
 195    /* Make up a correct qName if necessary */
 196  0 if ("".equals(qName)) {
 197  0 newContent.append(getQNameElement(uri, localName));
 198    } else {
 199  0 newContent.append(qName);
 200    }
 201   
 202  0 for (int i = 0; i < attributes.getLength(); i++) {
 203  0 String qn = attributes.getQName(i);
 204   
 205    // Make up a correct qName if necessary
 206  0 if ("".equals(qn)) {
 207  0 qn = getQNameAtt(attributes.getURI(i), attributes.getLocalName(i));
 208    }
 209  0 newContent.append(" ");
 210  0 newContent.append(qn);
 211  0 newContent.append("=");
 212  0 newContent.append("\"");
 213  0 try {
 214  0 newContent.append(XMLUtilities.escape(attributes.getValue(i), true));
 215    } catch (XindiceRuntimeException e) {
 216  0 throw new SAXException(e);
 217    }
 218  0 newContent.append("\"");
 219   
 220    // Avoid duplicate namespace declarations
 221  0 if (qn.equals("xmlns")) {
 222  0 namespaces.remove("");
 223    }
 224   
 225  0 if (qn.startsWith("xmlns:")) {
 226  0 String ln = getLocalName(qn);
 227  0 namespaces.remove(ln);
 228    }
 229    }
 230   
 231  0 Iterator iter = namespaces.keySet().iterator();
 232  0 while (iter.hasNext()) {
 233  0 String key = (String) iter.next();
 234  0 newContent.append(" xmlns");
 235  0 if (key.length() > 0) {
 236  0 newContent.append(":");
 237  0 newContent.append(key);
 238    }
 239  0 newContent.append("=");
 240  0 newContent.append("\"");
 241  0 newContent.append(namespaces.get(key));
 242  0 newContent.append("\"");
 243  0 namespaces.remove(key);
 244    }
 245   
 246  0 newContent.append(">");
 247    }
 248   
 249   
 250    /**
 251    * Receive notification of the end of an element.
 252    *
 253    * @param uri Description of Parameter
 254    * @param localName Description of Parameter
 255    * @param qName Description of Parameter
 256    * @exception SAXException Description of Exception
 257    * @see org.xml.sax.ContentHandler#endElement
 258    */
 259  0 public void endElement(String uri, String localName, String qName)
 260    throws SAXException {
 261  0 newContent.append("</");
 262   
 263  0 if ("".equals(qName)) {
 264  0 qName = getQNameElement(uri, localName);
 265    }
 266  0 newContent.append(qName);
 267  0 newContent.append(">");
 268    }
 269   
 270   
 271    /**
 272    * Receive notification of character data inside an element.
 273    *
 274    * @param ch The characters.
 275    * @param start The start position in the character array.
 276    * @param length The number of characters to use from the
 277    * character array.
 278    * @exception SAXException Description of Exception
 279    * @see org.xml.sax.ContentHandler#characters
 280    */
 281  0 public void characters(char ch[], int start, int length) throws SAXException {
 282  0 try {
 283  0 newContent.append(XMLUtilities.escape(ch, start, length, true));
 284    } catch (XindiceRuntimeException e) {
 285  0 throw new SAXException(e);
 286    }
 287    }
 288   
 289   
 290    /**
 291    * Receive notification of ignorable whitespace in element content.
 292    *
 293    * @param ch The whitespace characters.
 294    * @param start The start position in the character array.
 295    * @param length The number of characters to use from the
 296    * character array.
 297    * @exception SAXException Description of Exception
 298    * @see org.xml.sax.ContentHandler#ignorableWhitespace
 299    */
 300  0 public void ignorableWhitespace(char ch[], int start, int length)
 301    throws SAXException {
 302  0 int i = 0;
 303  0 while (i < length) {
 304  0 newContent.append(ch[start + i]);
 305  0 i++;
 306    }
 307    }
 308   
 309   
 310    /**
 311    * Receive notification of a processing instruction.
 312    *
 313    * @param target The processing instruction target.
 314    * @param data The processing instruction data, or null if
 315    * none is supplied.
 316    * @exception SAXException Description of Exception
 317    * @see org.xml.sax.ContentHandler#processingInstruction
 318    */
 319  0 public void processingInstruction(String target, String data)
 320    throws SAXException {
 321  0 newContent.append("<?");
 322  0 newContent.append(target);
 323  0 newContent.append(" ");
 324   
 325  0 if (data != null) {
 326  0 newContent.append(data);
 327    }
 328   
 329  0 newContent.append("?>");
 330    }
 331   
 332   
 333    /**
 334    * Receive notification of a skipped entity.
 335    *
 336    * @param name The name of the skipped entity.
 337    * @exception SAXException Description of Exception
 338    * @see org.xml.sax.ContentHandler#processingInstruction
 339    */
 340  0 public void skippedEntity(String name)
 341    throws SAXException {
 342    // no op
 343    }
 344    }