Clover coverage report - JBind Project
Coverage timestamp: Fr Mai 28 2004 11:17:36 CEST
file stats: LOC: 293   Methods: 37
NCLOC: 239   Classes: 2
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
ElementImpl.java 55,6% 78,4% 89,2% 76%
 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.instance.impl;
 11   
 
 12   
 import java.util.Collections;
 13   
 import java.util.HashMap;
 14   
 import java.util.Iterator;
 15   
 import java.util.ListIterator;
 16   
 import java.util.Map;
 17   
 import java.util.Stack;
 18   
 
 19   
 import org.jbind.base.UnexpectedException;
 20   
 import org.jbind.util.collection.FilterIterator;
 21   
 import org.jbind.util.collection.FilterListIterator;
 22   
 import org.jbind.util.collection.TransformingIterator;
 23   
 import org.jbind.util.collection.TransformingListIterator;
 24   
 import org.jbind.xml.base.ILocation;
 25   
 import org.jbind.xml.base.INamespaces;
 26   
 import org.jbind.xml.core.base.ITextContentMemento;
 27   
 import org.jbind.xml.core.base.ITextContentProvider;
 28   
 import org.jbind.xml.core.bridge.IAttributeImpl;
 29   
 import org.jbind.xml.core.bridge.IDataImplVisitor;
 30   
 import org.jbind.xml.core.bridge.IDefaultAttrCreator;
 31   
 import org.jbind.xml.core.bridge.IElementImpl;
 32   
 import org.jbind.xml.core.bridge.IImplContainer;
 33   
 import org.jbind.xml.core.bridge.ITextImpl;
 34   
 import org.jbind.xml.core.content.IElemRefOrDecl;
 35   
 import org.jbind.xml.core.data.IAnyTypeData;
 36   
 import org.jbind.xml.core.data.IImpl;
 37   
 import org.jbind.xml.msg.XmlException;
 38   
 
 39   
 public class ElementImpl extends DataImpl implements IElementImpl {
 40   
 
 41   
   private ImplList myImplList;
 42   
   private AttributeImpls myAttributeImpls;
 43   
 
 44   
   private String myDefaultTextContent = null;
 45   
 
 46   
   private Map myPrefixToNamespaceMapping = null;
 47   
 
 48  2753
   public ElementImpl(IImplContainer aParent, String aNamespace, String aName, Map aPrefixMapping, ILocation aLocation) {
 49  2753
     super(aParent, aNamespace, aName, aLocation);
 50  2753
     myAttributeImpls = new AttributeImpls(this);
 51  2753
     myImplList = new ImplList(this);
 52  2753
     myPrefixToNamespaceMapping = aPrefixMapping;
 53   
   }
 54   
 
 55  476
   public void accept(IDataImplVisitor aVisitor) throws XmlException {
 56  476
     aVisitor.visitElementImplStart(this);
 57  476
     for (Iterator i = myImplList.iterImpls(); i.hasNext(); ) {
 58  483
       IImpl impl = (IImpl)i.next();
 59  483
       impl.accept(aVisitor);
 60   
     }
 61  476
     aVisitor.visitElementImplEnd(this);
 62   
   }
 63   
 
 64  4
   public IElementImpl addElement(String aNamespace, String aQName) {
 65  4
     return myImplList.addElement(aNamespace, aQName);
 66   
   }
 67   
 
 68  2524
   public IElementImpl addElement(String aNamespace, String aQName, Map aPrefixMapping, ILocation aLocation) {
 69  2524
     return myImplList.addElement(aNamespace, aQName, aPrefixMapping, aLocation);
 70   
   }
 71   
 
 72  9021
   public ListIterator iterChildren() {
 73  9021
     return myImplList.iterImpls();
 74   
   }
 75   
 
 76  2123
   public ListIterator iterElementData(final String aNamespace, final String aName) {
 77  2123
     return new TransformingListIterator(new FilterListIterator(myImplList.iterImpls(), new FilterListIterator.Test() {
 78   
 
 79  3055
       public boolean test(Object anObject) {
 80  3055
         if (!(anObject instanceof IElementImpl)) {
 81  600
           return false;
 82   
         }
 83  2455
         IElementImpl e = (IElementImpl)anObject;
 84  2455
         if (e.getData() == null) {
 85  259
           return false;
 86   
         }
 87   
         // namespace & name test
 88  2196
         String namespace = null;
 89  2196
         String name = null;
 90  2196
         IElemRefOrDecl elem = e.getData().getSubstitutionHead_();
 91  2196
         if (null != elem) {
 92  79
           namespace = elem.getNamespace();
 93  79
           name = elem.getName();
 94   
         } else {
 95  2117
           namespace = e.getNamespace();
 96  2117
           name = e.getPartName();
 97   
         }
 98  2196
         return ((aNamespace == null) || "*".equals(aNamespace) || aNamespace.equals(namespace)) && ((aName == null) || "*".equals(aName) || aName.equals(name));
 99   
 
 100   
       }
 101   
     }), new TransformingIterator.ITransformation() {
 102  2039
       public Object transform(Object anObject) {
 103  2039
         return ((IElementImpl)anObject).getData();
 104   
       }
 105   
     });
 106   
   }
 107   
 
 108  5766
   public boolean isNil() {
 109  5766
     boolean res = false;
 110  5766
     IAttributeImpl a = myAttributeImpls.getAttributeImpl(INamespaces.XML_SCHEMA_INSTANCE, "nil");
 111  5766
     if (null != a) {
 112  4
       res = "true".equals(a.getTextContent());
 113   
     }
 114  5766
     return res;
 115   
   }
 116   
 
 117  6
   public IAttributeImpl addAttribute(String aNamespace, String aQName, ITextContentProvider aProvider, boolean anIsDefault, ILocation aLocation) {
 118  6
     return myAttributeImpls.addAttribute(aNamespace, aQName, aProvider, anIsDefault, aLocation);
 119   
   }
 120   
 
 121  2196
   public IAttributeImpl addAttribute(String aNamespace, String aQName, String aValue, boolean anIsDefault, ILocation aLocation) {
 122  2196
     return myAttributeImpls.addAttribute(aNamespace, aQName, aValue, anIsDefault, aLocation);
 123   
   }
 124   
 
 125  1369
   public IAnyTypeData getAttributeData(String aNamespace, String aName) {
 126  1369
     IAttributeImpl impl = getAttributeImpl(aNamespace, aName);
 127  1369
     IAnyTypeData res = null;
 128  1369
     if (impl != null) {
 129  473
       res = impl.getData();
 130   
     }
 131  1369
     return res;
 132   
   }
 133   
 
 134  11961
   public IAttributeImpl getAttributeImpl(String aNamespace, String aName) {
 135  11961
     return myAttributeImpls.getAttributeImpl(aNamespace, aName);
 136   
   }
 137   
 
 138  2828
   public Iterator iterAttributeImpls() {
 139  2828
     return myAttributeImpls.iterImpls();
 140   
   }
 141   
 
 142  10
   public void removeAttribute(String aNamespace, String aName) throws XmlException {
 143  10
     myAttributeImpls.removeAttribute(aNamespace, aName);
 144   
   }
 145   
 
 146  1987
   public void setDefaultAttrCreator(IDefaultAttrCreator aCreator) {
 147  1987
     myAttributeImpls.setDefaultAttrCreator(aCreator);
 148   
   }
 149   
 
 150  30
   public Iterator iterAttributes(final String aNamespace, final String aName) {
 151  30
     return new TransformingIterator(new FilterIterator(myAttributeImpls.iterImpls(), new FilterIterator.ICondition() {
 152   
 
 153  36
       public boolean evaluate(Object anObject) {
 154  36
         IAttributeImpl a = (IAttributeImpl)anObject;
 155  36
         boolean res = "*".equals(aNamespace) || (null == aNamespace) || aNamespace.equals(a.getNamespace());
 156  36
         if (res) {
 157  36
           if (!"*".equals(aName) && (aName != null)) {
 158  0
             res = aName.equals(a.getPartName());
 159   
           }
 160   
         }
 161  36
         return res && (a.getData() != null);
 162   
       }
 163   
     }), new TransformingIterator.ITransformation() {
 164  24
       public Object transform(Object anObject) {
 165  24
         return ((IAttributeImpl)anObject).getData();
 166   
       }
 167   
     });
 168   
   }
 169   
 
 170  88
   public String getDefaultTextContent() {
 171  88
     return myDefaultTextContent;
 172   
   }
 173   
 
 174  2420
   public void setDefaultTextContent(String aString) {
 175  2420
     myDefaultTextContent = aString;
 176   
   }
 177   
 
 178  679
   public ITextImpl addText(String aText, ILocation aLocation) {
 179  679
     return myImplList.addText(aText, aLocation);
 180   
   }
 181   
 
 182  0
   public boolean isDefault() {
 183  0
     return (myDefaultTextContent != null) && !hasTextContent();
 184   
   }
 185   
 
 186  236
   public Map getPrefixToNamespaceMapping() {
 187  236
     if (myPrefixToNamespaceMapping == null) {
 188  0
       return Collections.EMPTY_MAP;
 189   
     }
 190  236
     return myPrefixToNamespaceMapping;
 191   
   }
 192   
 
 193  0
   public String getNamespaceForPrefix(String aPrefix) {
 194  0
     return (String)getPrefixToNamespaceMapping().get(aPrefix);
 195   
   }
 196   
 
 197  0
   public String getPrefixForNamespace(String aNamespace, boolean aCreate) {
 198  0
     if ((myPrefixToNamespaceMapping == null) && aCreate) {
 199  0
       myPrefixToNamespaceMapping = new HashMap();
 200   
     }
 201  0
     if (myPrefixToNamespaceMapping != null) {
 202  0
       for (Iterator i = myPrefixToNamespaceMapping.entrySet().iterator();
 203  0
               i.hasNext(); ) {
 204  0
         Map.Entry me = (Map.Entry)i.next();
 205  0
         if (aNamespace.equals(me.getValue())) {
 206  0
           return (String)me.getKey();
 207   
         }
 208   
       }
 209  0
       if (!aCreate) {
 210  0
         return null;
 211   
       }
 212  0
       int i = 0;
 213  0
       while (true) {
 214  0
         String p = "p" + i++;
 215  0
         if (!myPrefixToNamespaceMapping.containsKey(p)) {
 216  0
           myPrefixToNamespaceMapping.put(p, aNamespace);
 217  0
           return p;
 218   
         }
 219   
       }
 220   
     }
 221  0
     return null;
 222   
   }
 223   
 
 224   
   //
 225   
   // text content
 226   
   //
 227   
 
 228  0
   public boolean hasTextContent() {
 229  0
     return !"".equals(getTextContent());
 230   
   }
 231   
 
 232  7
   public ITextContentMemento saveTextContent() {
 233  7
     return new TextMemento(getTextContent(), null);
 234   
   }
 235   
 
 236  1
   public void restoreTextContent(ITextContentMemento aMemento) {
 237  1
     TextMemento m = (TextMemento)aMemento;
 238  1
     setTextContent(m.textContent);
 239   
   }
 240   
 
 241  387
   public String getTextContent() {
 242  387
     TextCollector collector = new TextCollector();
 243  387
     String res;
 244  387
     try {
 245  387
       accept(collector);
 246  387
       res = collector.getTextContent();
 247   
     } catch (XmlException e) {
 248  0
       throw new UnexpectedException(e);
 249   
     }
 250  387
     return res;
 251   
   }
 252   
 
 253  12
   public void setTextContent(ITextContentProvider aTextContentProvider) {
 254  12
     myImplList.clear();
 255  12
     myImplList.addText(aTextContentProvider, null);
 256   
   }
 257   
 
 258  3
   public void setTextContent(String aString) {
 259  3
     myImplList.clear();
 260  3
     if (!"".equals(aString)) {
 261  3
       myImplList.addText(aString, null);
 262   
     }
 263   
   }
 264   
 
 265   
   private static class TextCollector implements IDataImplVisitor {
 266   
 
 267   
     private Stack myStack = new Stack();
 268   
     private StringBuffer myBuffer = new StringBuffer();
 269  387
     public TextCollector() {
 270  387
       myStack.push(new StringBuffer());
 271   
     }
 272  341
     public void visitTextImpl(ITextImpl anImpl) throws XmlException {
 273  341
       ((StringBuffer)myStack.peek()).append(anImpl.getTextContent());
 274   
     }
 275  394
     public void visitElementImplStart(IElementImpl anImpl) throws XmlException {
 276  394
       myStack.push(new StringBuffer());
 277   
     }
 278  394
     public void visitElementImplEnd(IElementImpl anImpl) throws XmlException {
 279  394
       StringBuffer sb = (StringBuffer)myStack.pop();
 280  394
       if (sb.length() == 0) {
 281  58
         ElementImpl e = (ElementImpl)anImpl;
 282  58
         if (e.getDefaultTextContent() != null) {
 283  30
           sb.append(e.getDefaultTextContent());
 284   
         }
 285   
       }
 286  394
       ((StringBuffer)myStack.peek()).append(sb.toString());
 287   
     }
 288  387
     public String getTextContent() {
 289  387
       return myStack.peek().toString();
 290   
     }
 291   
   }
 292   
 }
 293