Java Code Examples for com.codename1.io.Util#split()

The following examples show how to use com.codename1.io.Util#split() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UIFragment.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
private void decorate(Element el, Component cmp) {
    String classAttr = el.getAttribute("class");
    if (classAttr != null && classAttr.length() > 0) {
        String[] tags = Util.split(classAttr, " ");
        $(cmp).addTags(tags);
    }
    String uiid = el.getAttribute("uiid");
    if (uiid != null && uiid.length() > 0){
        cmp.setUIID(uiid);
    }
    String id = el.getAttribute("id");
    if (id != null && id.length() > 0) {
        index.put(id, cmp);
    }
    
    String name = el.getAttribute("name");
    if (name != null && name.length() > 0) {
        cmp.setName(name);
    }
    String flags = el.getAttribute("flags");
    
    if (flags != null && flags.indexOf("safeArea") >= 0 && (cmp instanceof Container)) {
        ((Container)cmp).setSafeArea(true);
    }
}
 
Example 2
Source File: L10NManager.java    From CodenameOne with GNU General Public License v2.0 6 votes vote down vote up
private String extractMonthName(String dateStr) throws ParseException {
    String[] parts = Util.split(dateStr, " ");
    for (String part : parts) {
        if (part.length() == 0) {
            continue;
        }
        if (part.toLowerCase().equals("de")) {
            continue;
        }
        String firstChar = part.substring(0, 1);
        if (!firstChar.toLowerCase().equals(firstChar.toUpperCase())) {
            return part;
        }
    }
    throw new ParseException("Cannot extract month from string", 0);

}
 
Example 3
Source File: StyleParser.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
static Map<String,String> parseString(Map<String,String> out, String str) {
    String[] rules = Util.split(str, ";");
    for (String rule : rules) {
        rule = rule.trim();
        if (rule.length() == 0) {
            continue;
        }
        int pos = rule.indexOf(":");
        if (pos == -1) {
            continue;
        }
        String key = rule.substring(0, pos);
        if ("font".equals(key) && out.containsKey("font")) {
            // We may need to merge font rules
            FontInfo newFinfo = StyleParser.parseFont(new FontInfo(), rule.substring(pos+1));
            FontInfo origFinfo = StyleParser.parseFont(new FontInfo(), out.get("font"));
            Float newSize = newFinfo.getSize();
            if (newSize != null && newFinfo.getSizeUnit() != StyleParser.UNIT_INHERIT) {
                origFinfo.setSize(newSize);
                origFinfo.setSizeUnit(newFinfo.getSizeUnit());
            }
            if (newFinfo.getName() != null) {
                origFinfo.setName(newFinfo.getName());
            }
            if (newFinfo.getFile() != null) {
                origFinfo.setFile(newFinfo.getFile());
            }
            out.put(key, origFinfo.toString());
        } else {
            out.put(key, rule.substring(pos+1));
        }

    }
    return out;
}
 
Example 4
Source File: StyleParser.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
private static String parseStroke(BorderInfo out, String rem) {
    // parse the stroke
    int p1 = rem.indexOf("(");
    int p2 = rem.indexOf(")");
    if (p1 != -1 && p2 != -1) {
        String strokeStr = rem.substring(p1+1, p2).trim();
        String[] strokeArgs = Util.split(strokeStr, " ");
        for (String strokeArg : strokeArgs) {
            strokeArg = strokeArg.trim();
            if (strokeArg.endsWith("mm") || strokeArg.endsWith("px")) {
                ScalarValue sv = parseScalarValue(strokeArg);
                out.width = (float)sv.value;
                out.widthUnit = sv.unit;
            } else if (strokeArg.length() > 0) {
                //int strokeColor = Integer.parseInt(strokeArg, 16);
                if (strokeArg.length() == 8) {
                    // there is an alpha bit
                    out.setStrokeOpacity(Integer.parseInt(strokeArg.substring(0, 2), 16) & 0xff);
                    out.setStrokeColor(Integer.parseInt(strokeArg.substring(2), 16) & 0xffffff);
                } else {
                    out.setStrokeOpacity(0xff);
                    out.setStrokeColor(Integer.parseInt(strokeArg, 16) & 0xffffff);
                }
            }
        }
        rem = rem.substring(p2+1);
    } else {

        rem = rem.substring(6); // at least get past stroke
    }
    return rem;
}
 
Example 5
Source File: StyleParser.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * For a splicedImage border, this gets the spliced insets as a 4-element array of double values.
 * @param out An out parameter.  4-element array of insets.  Indices {@link Component#TOP}, {@link Component#BOTTOM}, {@link Component#LEFT}, and {@link Component#RIGHT}.
 * @return 4-element array of insets.  Indices {@link Component#TOP}, {@link Component#BOTTOM}, {@link Component#LEFT}, and {@link Component#RIGHT}.
 */
public double[] getSpliceInsets(double[] out) {
    String[] parts = Util.split(BorderInfo.this.getSpliceInsets(), " ");
    
    out[Component.TOP] = Double.parseDouble(parts[0]);
    out[Component.RIGHT] = Double.parseDouble(parts[1]);
    out[Component.BOTTOM] = Double.parseDouble(parts[2]);
    out[Component.LEFT] = Double.parseDouble(parts[3]);
    return out;
}
 
Example 6
Source File: CSSBorder.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
private CSSBorder borderImage(String cssProperty) {
    String[] parts = Util.split(cssProperty, " ");
    parts[0] = Util.decode(parts[0], "UTF-8", false);
    
    int len = parts.length;
    double[] splices = new double[len-1];
    for (int i=1; i<len; i++) {
        splices[i-1] = Double.parseDouble(parts[i]);
    }
    
    return borderImageWithName(parts[0], splices);
}
 
Example 7
Source File: CSSBorder.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds one or more background images from a CSS background-image property.
 * @param cssDirective The value of the background-image property.
 * @return Self for chaining.
 */
public CSSBorder backgroundImage(String cssDirective) {
    String[] parts = Util.split(cssDirective, ",");
    List<Image> imgs = new ArrayList<Image>();
    for (String part : parts) {
        part = part.trim();
        if (part.indexOf("url(") == 0) {
            part = part.substring(4, part.length()-1);
        }
        if (part.charAt(0) == '"' || part.charAt(0) == '"') {
            part = part.substring(1, part.length()-1);
        }
        if (part.indexOf("/") != -1) {
            part = part.substring(part.lastIndexOf("/")+1);
        }
        Image im = res.getImage(part);
        if (im == null) {
            try {
                im = EncodedImage.create("/"+part);
                im.setImageName(part);
                
            } catch (IOException ex) {
                Log.e(ex);
                throw new IllegalArgumentException("Failed to parse image: "+part);
            }
        }
        imgs.add(im);
        
    }
    return backgroundImage(imgs.toArray(new Image[imgs.size()]));
    
}
 
Example 8
Source File: GoogleImpl.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
private GoogleApiClient getClient(SuccessCallback<GoogleApiClient> onConnected) {
    if (mGoogleApiClient == null) {
        Context ctx = AndroidNativeUtil.getContext();
        if (mGoogleApiClient == null) {
            GoogleSignInOptions gso;

            if (clientId != null && clientSecret != null) {
                System.out.println("Generating GoogleSignIn for clientID="+clientId);
                List<Scope> includeScopes = new ArrayList<Scope>();
                Scope firstScope = new Scope(Scopes.PROFILE);
                if (scope != null) {
                    for (String str : Util.split(scope, " ")) {
                        if ("profile".equals(str)) {
                            //str = Scopes.PROFILE;
                            continue;
                        } else if ("email".equals(str)) {
                            str = Scopes.EMAIL;
                        } else if (Scopes.PROFILE.equals(str)) {
                            continue;
                        }
                        if (str.trim().isEmpty()) {
                            continue;
                        }
                        includeScopes.add(new Scope(str.trim()));
                    }
                }
                gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                        //.requestIdToken("555462747934-iujpd5saj4pjpibo7c6r9tbjfef22rh1.apps.googleusercontent.com")
                        .requestIdToken(clientId)
                        .requestScopes(firstScope, includeScopes.toArray(new Scope[includeScopes.size()]))
                        //.requestScopes(Plus.SCOPE_PLUS_PROFILE)
                        //.requestServerAuthCode("555462747934-iujpd5saj4pjpibo7c6r9tbjfef22rh1.apps.googleusercontent.com")
                        .requestServerAuthCode(clientId)
                        .build();
            } else {
                System.out.println("Generating GoogleSignIn without ID token");
                gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).build();
            }
            mGoogleApiClient = new GoogleApiClient.Builder(ctx)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    .build();
        }
    }
    if (mGoogleApiClient.isConnected()) {
        if (onConnected != null) {
            onConnected.onSucess(mGoogleApiClient);
        }
    } else {
        synchronized(onConnectedCallbacks) {
            if (onConnected != null) {
                onConnectedCallbacks.add(onConnected);
            }
            if (!mGoogleApiClient.isConnecting()) {
                mGoogleApiClient.connect(GoogleApiClient.SIGN_IN_MODE_OPTIONAL);
            }
        }
    }
    return mGoogleApiClient;
}
 
Example 9
Source File: JavascriptContext.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Calls the appropriate callback method given a URL that was received 
 * from the NavigationCallback.  It is set up to accept URLs of the 
 * form cn1command:object.method?type1=value1&type2=value2&...&typen=valuen
 * 
 * <p>This method parses the URL and converts all arguments (including the 
 * object and method) into their associated Java representations, then 
 * generates a JavascriptEvent to fire on the scriptMessageReceived
 * browser event.</p>
 * 
 * <p>This method will usually be called on the native platform's GUI
 * thread, but it dispatches the resulting JavascriptEvent on the EDT
 * using Display.callSerially()</p>
 * @param request The URL representing the command that is being called.
 */
private void dispatchCallback(final String request){
    Runnable r = new Runnable(){
        public void run(){
            String command = request.substring(request.indexOf("/!cn1command/")+"/!cn1command/".length());
            // Get the callback id
            String objMethod = command.substring(0, command.indexOf("?"));
            command = command.substring(command.indexOf("?")+1);

            final String self = objMethod.substring(0, objMethod.indexOf("."));
            String method = objMethod.substring(objMethod.indexOf(".")+1);

            // Now let's get the parameters
            String[] keyValuePairs = Util.split(command, "&");
            //Vector params = new Vector();

            int len = keyValuePairs.length;
            Object[] params = new Object[len];
            for ( int i=0; i<len; i++){
                String[] parts = Util.split(keyValuePairs[i], "=");
                if ( parts.length < 2 ){
                    continue;
                }
                String ptype = Util.decode(parts[0], null, true);
                String pval = Util.decode(parts[1], null, true);
                if ( "object".equals(ptype) || "function".equals(ptype) ){
                    params[i] = new JSObject(JavascriptContext.this, pval);
                } else if ( "number".equals(ptype) ){
                    params[i] = Double.valueOf(pval);
                } else if ( "string".equals(ptype)){
                    params[i] = pval;
                } else if ( "boolean".equals(ptype)){
                    params[i] = "true".equals(pval)?Boolean.TRUE:Boolean.FALSE;
                } else {
                    params[i] = null;
                }
            }
            JSObject selfObj = new JSObject(JavascriptContext.this, self);

            JavascriptEvent evt = new JavascriptEvent(selfObj, method, params);
            browser.fireWebEvent("scriptMessageReceived", evt);
        }
    };
    
    Display.getInstance().callSerially(r);
    
    
    
}
 
Example 10
Source File: StyleParser.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
private static String parseShadow(BorderInfo out, String rem) {
    int p1 = rem.indexOf("(");
    int p2 = rem.indexOf(")");
    if (p1 != -1 && p2 != -1) {
        String shadowStr = rem.substring(p1+1, p2);
        String[] shadowArgs = Util.split(shadowStr, " ");
        for (String shadowArg : shadowArgs) {
            shadowArg = shadowArg.trim();
            if (shadowArg.startsWith("shadowSpread:") || shadowArg.endsWith("mm") || shadowArg.endsWith("px")) {
                int colonPos = shadowArg.indexOf(":");
                if (colonPos != -1) {
                    shadowArg = shadowArg.substring(colonPos+1);
                }
                out.setShadowSpread(parseScalarValue(shadowArg));
            } else if (shadowArg.startsWith("x:")) {
                out.setShadowX((Float) Float.parseFloat(shadowArg.substring(shadowArg.indexOf(":")+1).trim()));
            } else if (shadowArg.startsWith("y:")) {
                out.setShadowY((Float) Float.parseFloat(shadowArg.substring(shadowArg.indexOf(":")+1).trim()));
            } else if (shadowArg.startsWith("blur:")) {
                out.setShadowBlur((Float) Float.parseFloat(shadowArg.substring(shadowArg.indexOf(":")+1).trim()));
            } else if (shadowArg.startsWith("opacity:")) {
                out.setShadowOpacity((Integer) Integer.parseInt(shadowArg.substring(shadowArg.indexOf(":")+1).trim()));
            }
        }

        if (out.getShadowSpread() == null) {
            out.setShadowSpread(parseScalarValue("0.5mm"));
        }
        if (out.getShadowX() == null) {
            out.setShadowX((Float) (float)0.5);
        }
        if (out.getShadowY() == null) {
            out.setShadowY((Float) (float)0.5);
        }
        if (out.getShadowBlur() == null) {
            out.setShadowBlur((Float) (float)0.1);
        }
        if (out.getShadowOpacity() == null) {
            out.setShadowOpacity((Integer) 128);
        }
        rem = rem.substring(p2+1);
    } else {
        rem = rem.substring(6); // at least get past the shadow param
    }
    return rem;
}
 
Example 11
Source File: StyleParser.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
static BorderInfo parseBorder(BorderInfo out, String args) {
    if (args == null) {
        out.setType("empty");
        return out;
    }
    args = args.trim();
    if ("none".equals(args)) {
        out.setType("empty");
        return out;
    }
    String[] parts1 = Util.split(args, " ");
    int plen = parts1.length;
    if (plen == 0) {
        out.setType("empty");
        return out;
    }
    
    if (plen > 3 && ("image".equals(parts1[0]) || "horizontalImage".equals(parts1[0]) || "verticalImage".equals(parts1[0]))) {
        out.setType(parts1[0]);

        out.setImages(new String[plen-1]);
        for (int i=1; i<plen; i++) {
            out.getImages()[i-1] = parts1[i];
        }
        return out;
    }
    
    if ("splicedImage".equals(parts1[0]) && plen == 6) {
        out.setType(parts1[0]);
        out.setSpliceImage(parts1[1]);
        out.setSpliceInsets(parts1[2]+" "+parts1[3]+" "+parts1[4]+" "+parts1[5]);
        return out;
    }
    
    if ("round".equals(parts1[0])) {
        return parseRoundBorder(out, args, parts1);
    }
    
    if ("roundRect".equals(parts1[0])) {
        return parseRoundRectBorder(out, args, parts1);
    }
    
    if (plen == 3) {
        String type = parts1[1];
        out.setColor((Integer) Integer.parseInt(parts1[2], 16));
        ScalarValue thicknessVal = parseSingleTRBLValue(parts1[0]);
        out.setWidth((Float) (float)thicknessVal.getValue());
        out.setWidthUnit(thicknessVal.getUnit());
        if (("solid".equals(type) || "line".equals(type))) {
            out.setType("line");
        } else if ("dashed".equals(type)) {
            out.setType("dashed");
        } else if ("dotted".equals(type)) {
            out.setType("dotted");
        } else if ("underline".equals(type)) {
            out.setType("underline");
        }
        return out;
    }
    
    out.setType("empty");
    return out;
    
}
 
Example 12
Source File: StyleParser.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
static FontInfo parseFont(FontInfo out, String font) {
    if (font == null || font.trim().length() == 0) {
        return null;
    }
    font = font.trim();
    String[] args = Util.split(font, " ");
    int len = args.length;
    if (len == 1) {
        String arg = args[0].trim();
        if (arg.length() == 0) {
            out.setSize(null);
            out.setSizeUnit(UNIT_INHERIT);
            out.setFile(null);
            out.setName(null);
            return out;
        }
        if (isFontSizeArg(arg)) {
            // This is a size
            parseFontSize(out, arg);
            out.setName(null);
            out.setFile(null);
            return out;
            
        } else {
            out.setSizeUnit(UNIT_INHERIT);
            out.setSize(null);
            parseFontName(out, arg);
            parseFontFile(out, arg);
            
        }
    } else if (len == 2) {
        if (isFontSizeArg(args[0])) {
            parseFontSize(out, args[0]);
            parseFontName(out, args[1]);
            parseFontFile(out, args[1]);
            return out;
        } else {
            out.setSize(null);
            out.setSizeUnit(UNIT_INHERIT);
            parseFontName(out, args[0]);
            parseFontFile(out, args[1]);
            return out;
            
        }

    } else if (len == 3) {
        parseFontSize(out, args[0]);
        parseFontName(out, args[1]);
        parseFontFile(out, args[2]);
        return out;
    } else {
        throw new IllegalArgumentException("Failed to parse font");
    }
    return out;
}
 
Example 13
Source File: CSSBorder.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Creates a new CSS border with the provided CSS styles.  This currenlty only supports a subset of CSS.  The following
 * properties are currently supported:
 * 
 * <p>
 * <ul>
 * <li>background-color</li>
 * <li>background-image</li>
 * <li>background-position</li>
 * <li>background-repeat</li>
 * <li>border-color</li>
 * <li>border-radius</li>
 * <li>border-stroke</li>
 * <li>border-style</li>
 * <li>border-width</li>
 * <li>border-image</li>
 * </ul>
 * </p>
 * @param res Theme resource file from which images can be loaded.
 * @param css CSS to parse.
 * @throws IllegalArgumentException If it fails to parse the style.
 */
public CSSBorder(Resources res, String css) {
    this.res = res;
    String[] parts = Util.split(css, ";");
    for (String part : parts) {
        int colonPos = part.indexOf(":");
        if (colonPos == -1) {
            continue;
        }
        String key = part.substring(0, colonPos).trim().toLowerCase();
        String value = part.substring(colonPos+1).trim();
        Decorator decorator = decorators.get(key);
        if (decorator == null) {
            throw new IllegalArgumentException("Unsupported CSS property: "+key);
        }
        decorator.decorate(this, key, value);
        
    }
}
 
Example 14
Source File: CSSBorder.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
BorderStroke(String value) {
    String[] parts = Util.split(value, " ");
    if (parts.length == 3) {
        thickness = parseThickness(parts[0]);
        type = getBorderStyle(parts[1]);
        color = Color.parse(parts[2]);
    } else if (parts.length == 2) {
        int index = 0;
        if (validateThickness(parts[index])) {
            thickness = parseThickness(parts[index]);
            index++;
        } else {
            thickness = parseThickness("medium");
        }
        
        if (validateBorderStyle(parts[index])) {
            type = getBorderStyle(parts[index]);
            index++;
        } else {
            type = STYLE_NONE;
        }
        
        if (index < 2) {
            color = Color.parse(parts[index]);
            index++;
        } else {
            color = Color.parse("transparent");
        }
        
        if (index < 2) {
            throw new IllegalArgumentException("Illegal border stroke parameter "+value);
        }
        
    } else if (parts.length == 1) {
        boolean used = false;
        if (validateThickness(value)) {
            thickness = parseThickness(value);
            used = true;
        } else {
            thickness = parseThickness("medium");
        }
        if (!used && validateBorderStyle(value)) {
            type = getBorderStyle(value);
            used = true;
        } else {
            type = STYLE_NONE;
        }
        if (!used && Color.validate(value)) {
            color = Color.parse(value);
            used = true;
        } else {
            color = Color.parse("transparent");
        }
        if (!used) {
            throw new IllegalArgumentException("Illegal border stroke parameter "+value);
        }
    } else {
        throw new IllegalArgumentException("Illegal border stroke parameter "+value);
    }
    
    
}