001package org.maltparser.core.helper;
002
003
004import java.io.BufferedInputStream;
005import java.io.BufferedOutputStream;
006import java.io.Closeable;
007import java.io.FileInputStream;
008import java.io.FileNotFoundException;
009import java.io.FileOutputStream;
010import java.io.IOException;
011
012import org.apache.log4j.Logger;
013import org.maltparser.core.exception.MaltChainedException;
014
015/**
016*
017*
018* @author Johan Hall
019*/
020public class Util {
021          private static final int BUFFER = 4096;
022          private static final char AMP_CHAR = '&';
023          private static final char LT_CHAR = '<';
024          private static final char GT_CHAR = '>';
025          private static final char QUOT_CHAR = '"';
026          private static final char APOS_CHAR = '\'';
027        
028          public static String xmlEscape(String str) {
029                  boolean needEscape = false;
030                  char c;
031                  for (int i = 0; i < str.length(); i++) {
032                          c = str.charAt(i);
033                          if (c == AMP_CHAR || c == LT_CHAR || c == GT_CHAR || c == QUOT_CHAR || c == APOS_CHAR) {
034                                  needEscape = true;
035                                  break;
036                          }
037                  }
038                  if (!needEscape) {
039                          return str;
040                  }
041                  final StringBuilder sb = new StringBuilder();
042                  for (int i = 0; i < str.length(); i++) {
043                          c = str.charAt(i);
044                          if (str.charAt(i) == AMP_CHAR) {
045                                  sb.append("&amp;");
046                          } else if ( str.charAt(i) == LT_CHAR) {
047                                  sb.append("&lt;");
048                          } else if (str.charAt(i) == GT_CHAR) {
049                                  sb.append("&gt;");
050                          } else if (str.charAt(i) == QUOT_CHAR) {
051                                  sb.append("&quot;");
052                          } else if (str.charAt(i) == APOS_CHAR) {
053                                  sb.append("&apos;");
054                          } else {
055                                  sb.append(c);
056                          }
057                  }
058                  return sb.toString();
059          }
060
061        public static int simpleTicer(Logger logger, long startTime, int nTicxRow, int inTic, int subject) {
062                logger.info(".");
063                int tic = inTic + 1;
064                if (tic >= nTicxRow) {
065                        ticInfo(logger, startTime, subject);
066                        tic = 0;
067                }
068                return tic;
069        }
070        
071        public static void startTicer(Logger logger, long startTime, int nTicxRow, int subject) {
072                logger.info(".");
073                for (int i = 1; i <= nTicxRow; i++) {
074                        logger.info(" ");
075                }
076                ticInfo(logger, startTime, subject);
077        }
078        
079        public static void endTicer(Logger logger, long startTime, int nTicxRow, int inTic, int subject) {
080                for (int i = inTic; i <= nTicxRow; i++) {
081                        logger.info(" ");
082                }
083                ticInfo(logger, startTime, subject);
084        }
085        
086        private static void ticInfo(Logger logger, long startTime, int subject) {
087                logger.info("\t");
088                int a = 1000000;
089                if (subject != 0) {
090                        while (subject/a == 0) {
091                                logger.info(" ");
092                                a /= 10;
093                        }
094                } else {
095                        logger.info("      ");
096                }
097                logger.info(subject);
098                logger.info("\t");
099                long time = (System.currentTimeMillis()-startTime)/1000;
100                a = 1000000;
101                if (time != 0) {
102                        while (time/a == 0 ) {
103                                logger.info(" ");
104                                a /= 10;
105                        }
106                        logger.info(time);
107                        logger.info("s");
108                } else {
109                        logger.info("      0s");
110                }
111                logger.info("\t");
112                long memory =  (Runtime.getRuntime().totalMemory() -  Runtime.getRuntime().freeMemory())/1000000;
113                a = 1000000;
114                if (memory != 0) {
115                        while (memory/a == 0 ) {
116                                logger.info(" ");
117                                a /= 10;
118                        }
119                        logger.info(memory);
120                        logger.info("MB\n");
121                } else {
122                        logger.info("      0MB\n");
123                }
124        }
125        
126        public static void copyfile(String source, String destination) throws MaltChainedException {
127        try {
128                byte[] readBuffer = new byte[BUFFER];
129                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
130                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destination), BUFFER);
131                int n = 0;
132                    while ((n = bis.read(readBuffer, 0, BUFFER)) != -1) {
133                        bos.write(readBuffer, 0, n);
134                    }
135                bos.flush();
136                bos.close();
137                bis.close();
138                } catch (FileNotFoundException e) {
139                        throw new MaltChainedException("The destination file '"+destination+"' cannot be created when coping the file. ", e);
140                } catch (IOException e) {
141                        throw new MaltChainedException("The source file '"+source+"' cannot be copied to destination '"+destination+"'. ", e);
142                }
143        }
144
145    /**
146        * @param s the string to parse for the double value
147        * @throws IllegalArgumentException if s is empty or represents NaN or Infinity
148        * @throws NumberFormatException see {@link Double#parseDouble(String)}
149        */
150        public static double atof(String s) {
151        if (s == null || s.length() < 1) throw new IllegalArgumentException("Can't convert empty string to integer");
152        double d = Double.parseDouble(s);
153        if (Double.isNaN(d) || Double.isInfinite(d)) {
154            throw new IllegalArgumentException("NaN or Infinity in input: " + s);
155        }
156        return (d);
157    }
158
159            /**
160        * @param s the string to parse for the integer value
161        * @throws IllegalArgumentException if s is empty
162        * @throws NumberFormatException see {@link Integer#parseInt(String)}
163        */
164        public static int atoi(String s) throws NumberFormatException {
165        if (s == null || s.length() < 1) throw new IllegalArgumentException("Can't convert empty string to integer");
166        // Integer.parseInt doesn't accept '+' prefixed strings
167        if (s.charAt(0) == '+') s = s.substring(1);
168        return Integer.parseInt(s);
169    }
170        
171        public static void closeQuietly(Closeable c) {
172        if (c == null) return;
173        try {
174            c.close();
175        } catch (Throwable t) {}
176    }
177    
178    public static double[] copyOf(double[] original, int newLength) {
179        double[] copy = new double[newLength];
180        System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
181        return copy;
182    }
183
184    public static int[] copyOf(int[] original, int newLength) {
185        int[] copy = new int[newLength];
186        System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
187        return copy;
188    }
189    
190    public static boolean equals(double[] a, double[] a2) {
191        if (a == a2) return true;
192        if (a == null || a2 == null) return false;
193
194        final int length = a.length;
195        if (a2.length != length) return false;
196
197        for (int i = 0; i < length; i++)
198            if (a[i] != a2[i]) return false;
199
200        return true;
201    }
202}