Coverage Report - org.kuali.rice.krad.util.documentserializer.PropertySerializerTrie
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertySerializerTrie
0%
0/47
0%
0/24
4.4
 
 1  
 /*
 2  
  * Copyright 2007-2008 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.krad.util.documentserializer;
 17  
 
 18  
 import org.apache.commons.lang.StringUtils;
 19  
 import org.kuali.rice.krad.util.KRADConstants;
 20  
 
 21  
 import java.util.StringTokenizer;
 22  
 
 23  
 /**
 24  
  * This is a implementation of a trie/prefix tree of that contains metadata about property serializability
 25  
  * during the document serialization process.
 26  
  *
 27  
  */
 28  
 public class PropertySerializerTrie {  
 29  
     private static final String PROPERTY_NAME_COMPONENT_SEPARATOR = ".";
 30  
     private PropertySerializerTrieNode rootNode;
 31  
     
 32  0
     public PropertySerializerTrie() {
 33  0
         rootNode = new PropertySerializerTrieNode(KRADConstants.EMPTY_STRING, KRADConstants.EMPTY_STRING);
 34  0
     }
 35  
     
 36  
     /**
 37  
      * Registers a new serializable property so that all of its primitives are serialized.  All nesting properties
 38  
      * will be serialized only to render open/close tags to maintain consistency with the document structure, unless
 39  
      * they are registered as well.
 40  
      * 
 41  
      * For example, if only property "document.a.b" is registered, then the XML will look like the following:
 42  
      * 
 43  
      * <document>
 44  
      *     <a>
 45  
      *         <b>
 46  
      *             <primitiveOfB>valueOfPrimitive</primitiveOfB>
 47  
      *         </b>
 48  
      *     </a>
 49  
      * </document>
 50  
      * 
 51  
      * That is, primitives of "document" and "document.a" will not be serialized unless those property strings are registered.
 52  
      * 
 53  
      * @param propertyName
 54  
      * @param setPropertySerializabilityToObjectAndAllPrimitivesForAll
 55  
      */
 56  
     public void addSerializablePropertyName(String propertyName, boolean setPropertySerializabilityToObjectAndAllPrimitivesForAll) {
 57  0
         if (propertyName == null) {
 58  0
             throw new IllegalArgumentException("Null attribute name specified");
 59  
         }
 60  0
         if (StringUtils.isBlank(propertyName)) {
 61  0
             rootNode.setPropertySerializabilityToObjectAndAllPrimitives();
 62  
         }
 63  
         else {
 64  0
             StringTokenizer tok = new StringTokenizer(propertyName, PROPERTY_NAME_COMPONENT_SEPARATOR, false);
 65  0
             StringBuilder buf = new StringBuilder();
 66  
 
 67  0
             if(setPropertySerializabilityToObjectAndAllPrimitivesForAll)
 68  0
                     rootNode.setPropertySerializabilityToObjectAndAllPrimitives();
 69  
 
 70  0
             PropertySerializerTrieNode currentNode = rootNode;
 71  0
             while (tok.hasMoreTokens()) {
 72  0
                 String attributeNameComponent = tok.nextToken();
 73  0
                 validateAttributeNameComponent(attributeNameComponent);
 74  
 
 75  0
                 buf.append(attributeNameComponent);
 76  
 
 77  
                 // create a new node or retrieve existing node for this name component
 78  0
                 PropertySerializerTrieNode childNode = currentNode.getChildNode(attributeNameComponent);
 79  0
                 if (childNode == null) {
 80  0
                     childNode = new PropertySerializerTrieNode(buf.toString(), attributeNameComponent);
 81  0
                     currentNode.addChildNode(childNode);
 82  
                 }
 83  
                 
 84  0
                 if (tok.hasMoreTokens()) {
 85  0
                     buf.append(PROPERTY_NAME_COMPONENT_SEPARATOR);
 86  
                 }
 87  0
                 currentNode = childNode;
 88  0
                 if(setPropertySerializabilityToObjectAndAllPrimitivesForAll)
 89  0
                         currentNode.setPropertySerializabilityToObjectAndAllPrimitives();
 90  0
             }
 91  
             
 92  0
             currentNode.setPropertySerializabilityToObjectAndAllPrimitives();
 93  
         }
 94  0
     }
 95  
     
 96  
     /**
 97  
      * Retrieves the metadata about the given property name
 98  
      * 
 99  
      * @param propertyName
 100  
      * @return
 101  
      */
 102  
     public PropertySerializabilityMetadata getPropertySerializabilityMetadata(String propertyName) {
 103  0
         if (propertyName == null) {
 104  0
             throw new IllegalArgumentException("Null attribute name specified");
 105  
         }
 106  0
         if (StringUtils.isBlank(propertyName)) {
 107  0
             return rootNode;
 108  
         }
 109  
         else {
 110  0
             StringTokenizer tok = new StringTokenizer(propertyName, PROPERTY_NAME_COMPONENT_SEPARATOR, false);
 111  
             
 112  0
             PropertySerializerTrieNode currentNode = rootNode;
 113  0
             while (tok.hasMoreTokens()) {
 114  0
                 String attributeNameComponent = tok.nextToken();
 115  0
                 validateAttributeNameComponent(attributeNameComponent);
 116  
 
 117  
                 // retrieve the child node for this name component
 118  0
                 PropertySerializerTrieNode childNode = currentNode.getChildNode(attributeNameComponent);
 119  0
                 if (childNode == null) {
 120  
                     // we didn't find a child node, so we know that something wasn't added with the prefix we're processing
 121  0
                     return null;
 122  
                 }
 123  
                 else {
 124  
                     // keep going until we hit the last token, at which case we'll get out of this loop
 125  0
                     currentNode = childNode;
 126  
                 }
 127  0
             }
 128  0
             return currentNode;
 129  
         }
 130  
     }
 131  
     
 132  
     /**
 133  
      * Returns the root node of the trie
 134  
      * 
 135  
      * @return
 136  
      */
 137  
     public PropertySerializabilityMetadata getRootPropertySerializibilityMetadata() {
 138  0
         return rootNode;
 139  
     }
 140  
     
 141  
     protected void validateAttributeNameComponent(String attributeNameComponent) {
 142  0
         if (StringUtils.isBlank(attributeNameComponent)) {
 143  0
             throw new IllegalArgumentException("Blank attribute name component specified");
 144  
         }
 145  0
     }
 146  
 }