Document: Add getLayoutItemByPath().
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / server / Utils.java
1 /*
2  * Copyright (C) 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
6  * GWT-Glom is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package org.glom.web.server;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.File;
25 import java.io.IOException;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28
29 import org.apache.commons.lang3.StringUtils;
30
31 /**
32  *
33  */
34 public class Utils {
35
36         /*
37          * This method safely converts longs from libglom into ints. This method was taken from stackoverflow:
38          * 
39          * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
40          */
41         public static int safeLongToInt(final long value) {
42                 if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
43                         throw new IllegalArgumentException(value + " cannot be cast to int without changing its value.");
44                 }
45                 return (int) value;
46         }
47
48         public static String getFileName(final String fileURI) {
49                 final String[] splitURI = fileURI.split(File.separator);
50                 return splitURI[splitURI.length - 1];
51         }
52
53         static public Object deepCopy(final Object oldObj) {
54                 ObjectOutputStream oos = null;
55                 ObjectInputStream ois = null;
56
57                 try {
58                         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
59                         oos = new ObjectOutputStream(bos);
60                         // serialize and pass the object
61                         oos.writeObject(oldObj);
62                         oos.flush();
63                         final ByteArrayInputStream bin = new ByteArrayInputStream(bos.toByteArray());
64                         ois = new ObjectInputStream(bin);
65
66                         // return the new object
67                         return ois.readObject();
68                 } catch (final Exception e) {
69                         System.out.println("Exception in deepCopy:" + e);
70                         return null;
71                 } finally {
72                         try {
73                                 oos.close();
74                                 ois.close();
75                         } catch (final IOException e) {
76                                 System.out.println("Exception in deepCopy during finally: " + e);
77                                 return null;
78                         }
79                 }
80         }
81         
82         /** Build a :-separated string to represent the path as a string.
83          * @param path
84          * @return
85          */
86         public static String buildLayoutPath(int[] path) {
87                 if((path == null) || (path.length == 0)) {
88                         return null;
89                 }
90
91                 String result = new String();
92                 for(int i:path) {
93                         if(!result.isEmpty()) {
94                                 result += ":";
95                         }
96                         
97                         final String strIndex = Integer.toString(i);
98                         result += strIndex;
99                 }
100                 
101                 return result;
102         }
103
104         /** Get an array of int indices from the :-separated string.
105          * See buildLayoutPath().
106          * 
107          * @param attrLayoutPath
108          * @return The array of indices of the layout items.
109          */
110         public static int[] parseLayoutPath(final String attrLayoutPath) {
111                 if(StringUtils.isEmpty(attrLayoutPath)) {
112                         return null;
113                 }
114                 
115                 final String[] strIndices = attrLayoutPath.split(":");
116                 final int[] indices = new int[strIndices.length];
117                 for (int i = 0; i < strIndices.length; ++i) {
118                         final String str = strIndices[i];
119         
120                         try
121                         {
122                                 indices[i] = Integer.parseInt(str);
123                         }
124                         catch (final NumberFormatException nfe)
125                         {
126                                 //TODO: Log the error.
127                                 return null;
128                         }
129                 }
130         
131                 return indices;
132         }
133
134 }