All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dmfs.android.xmlmagic.builder.AndroidBooleanObjectBuilder Maven / Gradle / Ivy

There is a newer version: 0.1.2
Show newest version
/*
 * Copyright (C) 2015 Marten Gajda 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.dmfs.android.xmlmagic.builder;

import org.dmfs.android.xmlmagic.AndroidParserContext;
import org.dmfs.android.xmlmagic.StringFormatter;
import org.dmfs.android.xmlmagic.tokenresolvers.ITokenResolver;
import org.dmfs.xmlobjects.ElementDescriptor;
import org.dmfs.xmlobjects.QualifiedName;
import org.dmfs.xmlobjects.pull.ParserContext;
import org.dmfs.xmlobjects.pull.XmlObjectPullParserException;


/**
 * An object builder to build {@link Boolean} instances from XML.
 * 

* This builder accepts the value in an attribute called value or as text value. *

* Examples: * *

 * {@code
 * true 
 * false 
 * 1 
 * 0 
 * {@literal@bool:some_boolean_resource}  
 * {@literal@string:some_string_resource}  
 * some random text 
 *  
 *  
 *  
 *   
 * }
 * 
* * Note: if both, an attribute and a text value, are present, the attribute takes precedence over the text value. *

* A concrete {@link ElementDescriptor} using this builder is {@link org.dmfs.android.xmlmagic.Model#BOOLEAN}. *

* * @author Marten Gajda */ public class AndroidBooleanObjectBuilder extends BaseAndroidObjectBuilder { public final static AndroidBooleanObjectBuilder INSTANCE = new AndroidBooleanObjectBuilder(); @Override public Boolean update(ElementDescriptor descriptor, Boolean object, QualifiedName attribute, String value, ParserContext context) throws XmlObjectPullParserException { return getBooleanAttr(attribute, context); } @Override public Boolean update(ElementDescriptor descriptor, Boolean object, String text, ParserContext context) throws XmlObjectPullParserException { if (object != null) { // we already have a value return object; } if (text == null || text.length() == 0) { return false; } if (context instanceof AndroidParserContext) { // The context can provide a formatter. ITokenResolver resolver = ((AndroidParserContext) context).getResolver(); if (resolver != null) { // We have a formatter. Format the given text. text = StringFormatter.format(text, resolver, 5).toString(); } } return "true".equalsIgnoreCase(text) || "1".equals(text); } @Override public Boolean finish(ElementDescriptor descriptor, Boolean object, ParserContext context) throws XmlObjectPullParserException { if (object == null) { // no value was specified: default to false return false; } return object; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy