001    /**
002     * ===========================================================
003     * LibRepository : a free Java content repository access layer
004     * ===========================================================
005     *
006     * Project Info:  http://jfreereport.pentaho.org/librepository/
007     *
008     * (C) Copyright 2006, by Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * RepositoryUtilities.java
027     * ------------
028     * (C) Copyright 2006, by Pentaho Corporation.
029     */
030    
031    package org.jfree.repository;
032    
033    import java.util.StringTokenizer;
034    import java.util.ArrayList;
035    import java.util.LinkedList;
036    
037    /**
038     * Creation-Date: 02.12.2006, 13:38:01
039     *
040     * @author Thomas Morgner
041     */
042    public class RepositoryUtilities
043    {
044      private RepositoryUtilities()
045      {
046    
047      }
048    
049      public static ContentEntity getEntity (final Repository repository, final String[] name)
050          throws ContentIOException
051      {
052        if (name.length == 0)
053        {
054          return repository.getRoot();
055        }
056    
057        ContentLocation node = repository.getRoot();
058        for (int i = 0; i < name.length - 1; i++)
059        {
060          final String nameItem = name[i];
061          final ContentEntity entry = node.getEntry(nameItem);
062          if (entry instanceof ContentLocation == false)
063          {
064            // its ok, if we hit the last item
065            throw new ContentIOException("No such item.");
066          }
067          node = (ContentLocation) entry;
068        }
069        return node.getEntry(name[name.length - 1]);
070      }
071    
072      public static ContentItem createItem (final Repository repository, final String[] name)
073          throws ContentIOException
074      {
075        if (name.length == 0)
076        {
077          throw new IllegalArgumentException("Empty name not permitted.");
078        }
079    
080        ContentLocation node = repository.getRoot();
081        for (int i = 0; i < name.length - 1; i++)
082        {
083          final String nameItem = name[i];
084          if (node.exists(nameItem) == false)
085          {
086            // create it
087            node = node.createLocation(nameItem);
088          }
089          else
090          {
091            final ContentEntity entry = node.getEntry(nameItem);
092            if (entry instanceof ContentLocation == false)
093            {
094              // its ok, if we hit the last item
095              throw new ContentIOException("No such item.");
096            }
097            node = (ContentLocation) entry;
098          }
099        }
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    }