001    /*
002     * Copyright 2010 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.vafer.jdeb.utils;
017    
018    import java.io.IOException;
019    import java.io.InputStream;
020    import java.io.OutputStream;
021    import java.text.ParseException;
022    
023    
024    /**
025     * Simple utils functions.
026     * 
027     * ATTENTION: don't use outside of jdeb
028     * 
029     * @author Torsten Curdt <tcurdt@vafer.org>
030     */
031    public final class Utils {
032    
033        public static int copy( final InputStream pInput, final OutputStream pOutput ) throws IOException {
034            final byte[] buffer = new byte[2048];
035            int count = 0;
036            int n = 0;
037            while (-1 != (n = pInput.read(buffer))) {
038                    pOutput.write(buffer, 0, n);
039                    count += n;
040            }
041            return count;
042         }
043    
044        public static String toHex( final byte[] pBytes ) {
045            final StringBuffer sb = new StringBuffer();
046    
047            for (int i = 0; i < pBytes.length; ++i) {
048                sb.append(Integer.toHexString((pBytes[i]>>4) & 0x0f));
049                sb.append(Integer.toHexString(pBytes[i] & 0x0f));
050            }
051    
052            return sb.toString();
053        }
054    
055        public static String stripPath( final int p, final String s ) {
056    
057            if (p <= 0) {
058                return s;
059            }
060    
061            int x = 0;
062            for (int i=0 ; i<p; i++) {
063                x = s.indexOf('/', x);
064                if (x < 0) {
065                    return s;
066                }
067            }
068    
069            return s.substring(x+1);
070        }
071    
072        public static String stripLeadingSlash( final String s ) {
073            if (s == null) {
074                return s;
075            }
076            if (s.length() == 0) {
077                return s;
078            }
079            if (s.charAt(0) == '/') {
080                return s.substring(1);
081            }
082            return s;
083        }
084    
085        
086        /**
087         * Substitute the variables in the given expression with the
088         * values from the resolver
089         * 
090         * @param pVariables
091         * @param pExpression
092         * @return
093         */
094        public static String replaceVariables(final VariableResolver pResolver, final String pExpression, final String pOpen, final String pClose) throws ParseException {
095    
096            final char[] s = pExpression.toCharArray();
097    
098            final char[] open = pOpen.toCharArray();
099            final char[] close = pClose.toCharArray();
100    
101            final StringBuffer out = new StringBuffer();
102            StringBuffer sb = new StringBuffer();
103            char[] watch = open;
104            int w = 0;
105            for (int i = 0; i < s.length; i++) {
106                char c = s[i];
107    
108                if (c == watch[w]) {
109                    w++;
110                    if (watch.length == w) {
111                        // found the full token to watch for
112                        
113                        if (watch == open) {
114                            // found open
115                            out.append(sb);
116                            sb = new StringBuffer();
117                            // search for close
118                            watch = close;
119                        } else if (watch == close) {
120                            // found close
121                            final String variable = (String) pResolver.get(sb.toString());
122                            if (variable != null) {
123                                out.append(variable);
124                            } else {
125                                throw new ParseException("Unknown variable " + sb, i);
126                            }
127                            sb = new StringBuffer();
128                            // search for open
129                            watch = open;
130                        }
131                        w = 0;
132                    }
133                } else {
134    
135                    if (w > 0) {
136                        sb.append(watch, 0, w);
137                    }
138    
139                    sb.append(c);
140    
141                    w = 0;
142                }
143            }
144    
145            if (watch == close) {
146                out.append(open);
147            }
148            out.append(sb);
149    
150            return out.toString();
151        }
152        
153    }