001    /*
002     * Copyright (c) 2008-2009 Mozilla Foundation
003     *
004     * Permission is hereby granted, free of charge, to any person obtaining a 
005     * copy of this software and associated documentation files (the "Software"), 
006     * to deal in the Software without restriction, including without limitation 
007     * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
008     * and/or sell copies of the Software, and to permit persons to whom the 
009     * Software is furnished to do so, subject to the following conditions:
010     *
011     * The above copyright notice and this permission notice shall be included in 
012     * all copies or substantial portions of the Software.
013     *
014     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
015     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
016     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
017     * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
018     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
019     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
020     * DEALINGS IN THE SOFTWARE.
021     */
022    
023    package nu.validator.htmlparser.impl;
024    
025    import nu.validator.htmlparser.annotation.Literal;
026    import nu.validator.htmlparser.annotation.Local;
027    import nu.validator.htmlparser.annotation.NoLength;
028    import nu.validator.htmlparser.common.Interner;
029    
030    public final class Portability {
031    
032        // Allocating methods
033    
034        /**
035         * Allocates a new local name object. In C++, the refcount must be set up in such a way that 
036         * calling <code>releaseLocal</code> on the return value balances the refcount set by this method.
037         */
038        public static @Local String newLocalNameFromBuffer(@NoLength char[] buf, int offset, int length, Interner interner) {
039            return new String(buf, offset, length).intern();
040        }
041    
042        public static String newStringFromBuffer(@NoLength char[] buf, int offset, int length) {
043            return new String(buf, offset, length);
044        }
045    
046        public static String newEmptyString() {
047            return "";
048        }
049    
050        public static String newStringFromLiteral(@Literal String literal) {
051            return literal;
052        }
053        
054        public static String newStringFromString(String string) {
055            return string;
056        }
057        
058        // XXX get rid of this
059        public static char[] newCharArrayFromLocal(@Local String local) {
060            return local.toCharArray();
061        }
062    
063        public static char[] newCharArrayFromString(String string) {
064            return string.toCharArray();
065        }
066        
067        public static @Local String newLocalFromLocal(@Local String local, Interner interner) {
068            return local;
069        }
070        
071        // Deallocation methods
072        
073        public static void releaseString(String str) {
074            // No-op in Java
075        }
076        
077        // Comparison methods
078        
079        public static boolean localEqualsBuffer(@Local String local, @NoLength char[] buf, int offset, int length) {
080            if (local.length() != length) {
081                return false;
082            }
083            for (int i = 0; i < length; i++) {
084                if (local.charAt(i) != buf[offset + i]) {
085                    return false;
086                }
087            }
088            return true;
089        }
090    
091        public static boolean lowerCaseLiteralIsPrefixOfIgnoreAsciiCaseString(@Literal String lowerCaseLiteral,
092                String string) {
093            if (string == null) {
094                return false;
095            }
096            if (lowerCaseLiteral.length() > string.length()) {
097                return false;
098            }
099            for (int i = 0; i < lowerCaseLiteral.length(); i++) {
100                char c0 = lowerCaseLiteral.charAt(i);
101                char c1 = string.charAt(i);
102                if (c1 >= 'A' && c1 <= 'Z') {
103                    c1 += 0x20;
104                }
105                if (c0 != c1) {
106                    return false;
107                }
108            }
109            return true;
110        }
111        
112        public static boolean lowerCaseLiteralEqualsIgnoreAsciiCaseString(@Literal String lowerCaseLiteral,
113                String string) {
114            if (string == null) {
115                return false;
116            }
117            if (lowerCaseLiteral.length() != string.length()) {
118                return false;
119            }
120            for (int i = 0; i < lowerCaseLiteral.length(); i++) {
121                char c0 = lowerCaseLiteral.charAt(i);
122                char c1 = string.charAt(i);
123                if (c1 >= 'A' && c1 <= 'Z') {
124                    c1 += 0x20;
125                }
126                if (c0 != c1) {
127                    return false;
128                }
129            }
130            return true;
131        }
132        
133        public static boolean literalEqualsString(@Literal String literal, String string) {
134            return literal.equals(string);
135        }
136    
137        public static boolean stringEqualsString(String one, String other) {
138            return one.equals(other);
139        }
140        
141        public static void delete(Object o) {
142            
143        }
144    
145        public static void deleteArray(Object o) {
146            
147        }
148    }