Source for org.jfree.repository.RepositoryUtilities

   1: /**
   2:  * ===========================================================
   3:  * LibRepository : a free Java content repository access layer
   4:  * ===========================================================
   5:  *
   6:  * Project Info:  http://jfreereport.pentaho.org/librepository/
   7:  *
   8:  * (C) Copyright 2006, by Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * RepositoryUtilities.java
  27:  * ------------
  28:  * (C) Copyright 2006, by Pentaho Corporation.
  29:  */
  30: 
  31: package org.jfree.repository;
  32: 
  33: import java.util.StringTokenizer;
  34: import java.util.ArrayList;
  35: import java.util.LinkedList;
  36: 
  37: /**
  38:  * Creation-Date: 02.12.2006, 13:38:01
  39:  *
  40:  * @author Thomas Morgner
  41:  */
  42: public class RepositoryUtilities
  43: {
  44:   private RepositoryUtilities()
  45:   {
  46: 
  47:   }
  48: 
  49:   public static ContentEntity getEntity (final Repository repository, final String[] name)
  50:       throws ContentIOException
  51:   {
  52:     if (name.length == 0)
  53:     {
  54:       return repository.getRoot();
  55:     }
  56: 
  57:     ContentLocation node = repository.getRoot();
  58:     for (int i = 0; i < name.length - 1; i++)
  59:     {
  60:       final String nameItem = name[i];
  61:       final ContentEntity entry = node.getEntry(nameItem);
  62:       if (entry instanceof ContentLocation == false)
  63:       {
  64:         // its ok, if we hit the last item
  65:         throw new ContentIOException("No such item.");
  66:       }
  67:       node = (ContentLocation) entry;
  68:     }
  69:     return node.getEntry(name[name.length - 1]);
  70:   }
  71: 
  72:   public static ContentItem createItem (final Repository repository, final String[] name)
  73:       throws ContentIOException
  74:   {
  75:     if (name.length == 0)
  76:     {
  77:       throw new IllegalArgumentException("Empty name not permitted.");
  78:     }
  79: 
  80:     ContentLocation node = repository.getRoot();
  81:     for (int i = 0; i < name.length - 1; i++)
  82:     {
  83:       final String nameItem = name[i];
  84:       if (node.exists(nameItem) == false)
  85:       {
  86:         // create it
  87:         node = node.createLocation(nameItem);
  88:       }
  89:       else
  90:       {
  91:         final ContentEntity entry = node.getEntry(nameItem);
  92:         if (entry instanceof ContentLocation == false)
  93:         {
  94:           // its ok, if we hit the last item
  95:           throw new ContentIOException("No such item.");
  96:         }
  97:         node = (ContentLocation) entry;
  98:       }
  99:     }
 100:     return node.createItem(name[name.length - 1]);
 101:   }
 102: 
 103:   public static ContentLocation createLocation (final Repository repository, final String[] name)
 104:       throws ContentIOException
 105:   {
 106:     if (name.length == 0)
 107:     {
 108:       throw new IllegalArgumentException("Empty name not permitted.");
 109:     }
 110: 
 111:     ContentLocation node = repository.getRoot();
 112:     for (int i = 0; i < name.length - 1; i++)
 113:     {
 114:       final String nameItem = name[i];
 115:       if (node.exists(nameItem) == false)
 116:       {
 117:         // create it
 118:         node = node.createLocation(nameItem);
 119:       }
 120:       else
 121:       {
 122:         final ContentEntity entry = node.getEntry(nameItem);
 123:         if (entry instanceof ContentLocation == false)
 124:         {
 125:           // its ok, if we hit the last item
 126:           throw new ContentIOException("No such item.");
 127:         }
 128:         node = (ContentLocation) entry;
 129:       }
 130:     }
 131:     return node.createLocation(name[name.length - 1]);
 132:   }
 133: 
 134:   public static String[] split (final String name, final String separator)
 135:   {
 136:     final StringTokenizer strtok = new StringTokenizer(name, separator, false);
 137:     final int tokenCount = strtok.countTokens();
 138:     final String[] retval = new String[tokenCount];
 139:     int i = 0;
 140:     while (strtok.hasMoreTokens())
 141:     {
 142:       retval[i] = strtok.nextToken();
 143:       i += 1;
 144:     }
 145:     return retval;
 146:   }
 147: 
 148:   public static String[] buildNameArray (ContentEntity entity)
 149:   {
 150:     final LinkedList collector = new LinkedList();
 151:     while (entity != null)
 152:     {
 153:       final ContentLocation parent = entity.getParent();
 154:       if (parent != null)
 155:       {
 156:         // this filters out the root ..
 157:         collector.add(0, entity.getName());
 158:       }
 159:       entity = parent;
 160:     }
 161:     return (String[]) collector.toArray(new String[collector.size()]);
 162:   }
 163: 
 164:   public static String buildName (ContentEntity entity, final String separator)
 165:   {
 166:     final ArrayList collector = new ArrayList();
 167:     while (entity != null)
 168:     {
 169:       final ContentLocation parent = entity.getParent();
 170:       if (parent != null)
 171:       {
 172:         // this filters out the root ..
 173:         collector.add(entity.getName());
 174:       }
 175:       entity = parent;
 176:     }
 177: 
 178:     final StringBuffer builder = new StringBuffer();
 179:     final int maxIdx = collector.size() - 1;
 180:     for (int i = maxIdx; i >= 0; i--)
 181:     {
 182:       final String s = (String) collector.get(i);
 183:       if (i != maxIdx)
 184:       {
 185:         builder.append(separator);
 186:       }
 187:       builder.append(s);
 188:     }
 189:     return builder.toString();
 190:   }
 191: }