Clover coverage report - JBind Project
Coverage timestamp: Fr Mai 28 2004 11:17:36 CEST
file stats: LOC: 161   Methods: 6
NCLOC: 123   Classes: 1
This license of Clover is provided to support the development of JBind only. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover.
 
 Source file Conditionals Statements Methods TOTAL
MarshalVisitor.java 81,8% 87,1% 100% 86,7%
 1   
 /*
 2   
  * JBind
 3   
  *
 4   
  * Copyright (c) by Stefan Wachter. All rights reserved.
 5   
  *
 6   
  * Usage, modification, and redistribution is subject to license terms that are
 7   
  * available at 'http://www.jbind.org'. The JBind license is like the
 8   
  * 'Apache Software License V 1.1'.
 9   
  */
 10   
 package org.jbind.xml.core.bridge;
 11   
 
 12   
 import java.io.IOException;
 13   
 import java.io.Writer;
 14   
 import java.util.ArrayList;
 15   
 import java.util.Collections;
 16   
 import java.util.HashMap;
 17   
 import java.util.Iterator;
 18   
 import java.util.List;
 19   
 import java.util.Map;
 20   
 import java.util.Stack;
 21   
 
 22   
 import org.jbind.xml.base.StringUtil;
 23   
 import org.jbind.xml.core.data.IImpl;
 24   
 import org.jbind.xml.msg.XmlException;
 25   
 import org.jbind.xml.msg.XmlMessages;
 26   
 
 27   
 public class MarshalVisitor implements IDataImplVisitor {
 28   
 
 29   
   private Writer myWriter;
 30   
   private Stack myMappings = null;
 31   
   private Map myTopLevelAttributes;
 32   
   private List myNewNamespaces = null;
 33   
 
 34  6
   public MarshalVisitor() {}
 35   
 
 36  105
   private String getQName(String aNamespace, String aLocalName) {
 37  105
     String res = null;
 38  105
     if ((null != aNamespace) && !"".equals(aNamespace)) {
 39  89
       Map currentMapping = (Map)myMappings.peek();
 40  89
       String prefix = (String)currentMapping.get(aNamespace);
 41  89
       if (null == prefix) {
 42  14
         int i = 0;
 43  14
         do {
 44  24
           String p = "p" + i++;
 45  24
           if (!currentMapping.values().contains(p)) {
 46  14
             prefix = p;
 47  14
             Map m = new HashMap(currentMapping);
 48  14
             m.put(aNamespace, p);
 49  14
             myMappings.pop();
 50  14
             myMappings.push(m);
 51  14
             if (null == myNewNamespaces) {
 52  5
               myNewNamespaces = new ArrayList();
 53   
             }
 54  14
             myNewNamespaces.add(aNamespace);
 55   
           }
 56  24
         } while (prefix == null);
 57   
       }
 58  89
       res = prefix + ":" + aLocalName;
 59   
     } else {
 60  16
       res = aLocalName;
 61   
     }
 62  105
     return res;
 63   
   }
 64   
 
 65  43
   public void visitElementImplStart(IElementImpl anImpl) throws XmlException {
 66  43
     try {
 67  43
       myMappings.push(myMappings.peek());
 68   
 
 69  43
       String elemNamespace = anImpl.getNamespace();
 70  43
       String elemLocalName = anImpl.getPartName();
 71  43
       String elemQName = getQName(elemNamespace, elemLocalName);
 72   
 
 73  43
       myWriter.write("<" + elemQName);
 74   
 
 75  43
       for (Iterator i = anImpl.iterAttributeImpls(); i.hasNext(); ) {
 76  19
         IAttributeImpl a = (IAttributeImpl)i.next();
 77  19
         String attrNamespace = a.getNamespace();
 78  19
         String attrLocalName = a.getPartName();
 79  19
         String attrQName = getQName(attrNamespace, attrLocalName);
 80  19
         String attrValue = StringUtil.toXml(a.getTextContent());
 81  19
         myWriter.write(" " + attrQName + "=\"" + attrValue + "\"");
 82   
       }
 83   
 
 84  43
       if (null != myTopLevelAttributes) {
 85  0
         for (Iterator i = myTopLevelAttributes.entrySet().iterator();
 86  0
                 i.hasNext(); ) {
 87  0
           Map.Entry me = (Map.Entry)i.next();
 88  0
           myWriter.write(" " + me.getKey() + "=\"" + StringUtil.toXml((String)me.getValue()) + "\"");
 89   
         }
 90  0
         myTopLevelAttributes = null;
 91   
       }
 92   
 
 93  43
       if (null != myNewNamespaces) {
 94  11
         Map currentMappings = (Map)myMappings.peek();
 95  11
         for (Iterator i = myNewNamespaces.iterator(); i.hasNext(); ) {
 96  15
           String newNamespace = (String)i.next();
 97  15
           String newPrefix = (String)currentMappings.get(newNamespace);
 98  15
           if ("".equals(newPrefix)) {
 99  0
             myWriter.write(" xmlns=\"" + newNamespace + "\"");
 100   
           } else {
 101  15
             myWriter.write(" xmlns:" + newPrefix + "=\"" + newNamespace + "\"");
 102   
           }
 103   
         }
 104  11
         myNewNamespaces = null;
 105   
       }
 106   
 
 107  43
       myWriter.write(">");
 108   
     } catch (IOException e) {
 109  0
       throw new XmlException(XmlMessages.wrappedException(e, anImpl));
 110   
     }
 111   
 
 112   
   }
 113   
 
 114  43
   public void visitElementImplEnd(IElementImpl anImpl) throws XmlException {
 115  43
     try {
 116  43
       String elemNamespace = anImpl.getNamespace();
 117  43
       String elemLocalName = anImpl.getPartName();
 118  43
       String elemQName = getQName(elemNamespace, elemLocalName);
 119  43
       myWriter.write("</" + elemQName + ">");
 120  43
       myMappings.pop();
 121   
     } catch (IOException e) {
 122  0
       throw new XmlException(XmlMessages.wrappedException(e, anImpl));
 123   
     }
 124   
   }
 125   
 
 126  35
   public void visitTextImpl(ITextImpl anImpl) throws XmlException {
 127  35
     try {
 128  35
       myWriter.write(StringUtil.toXml(anImpl.getTextContent()));
 129   
     } catch (IOException e) {
 130  0
       throw new XmlException(XmlMessages.wrappedException(e, anImpl));
 131   
     }
 132   
   }
 133   
 
 134   
   /**
 135   
    * Marshals a data implementation. The marshalling starts straight with the root
 136   
    * element, i.e. any XML prolog must be written in advance.
 137   
    *
 138   
    * @param anImpl <i>(required)</i>. The data implementation to marshal.
 139   
    * @param aWriter <i>(required)</i>.
 140   
    * @param aNamespaceToPrefixMapping <i>(optional)</i>. An initial mapping.
 141   
    * @param aTopLevelAttributes <i>(optional)</i>. Additional attributes that
 142   
    * are added to the top level element. This can be used to add schema location
 143   
    * attributes.
 144   
    */
 145  6
   public synchronized void marshal(IImpl anImpl, Writer aWriter, Map aNamespaceToPrefixMapping, Map aTopLevelAttributes) throws XmlException {
 146  6
     myWriter = aWriter;
 147  6
     myTopLevelAttributes = aTopLevelAttributes;
 148  6
     Map mapping = (null != aNamespaceToPrefixMapping) ? aNamespaceToPrefixMapping : Collections.EMPTY_MAP;
 149  6
     myMappings = new Stack();
 150  6
     myMappings.push(mapping);
 151  6
     myNewNamespaces = new ArrayList();
 152  6
     myNewNamespaces.addAll(mapping.keySet());
 153  6
     anImpl.accept(this);
 154  6
     try {
 155  6
       aWriter.flush();
 156   
     } catch (IOException e) {
 157  0
       throw new XmlException(XmlMessages.wrappedException(e, anImpl));
 158   
     }
 159   
   }
 160   
 }
 161