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

org.dmfs.android.xmlmagic.builder.BundleObjectBuilder 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 java.util.ArrayList;

import org.dmfs.android.xmlmagic.Model;
import org.dmfs.xmlobjects.ElementDescriptor;
import org.dmfs.xmlobjects.QualifiedName;
import org.dmfs.xmlobjects.builder.AbstractObjectBuilder;
import org.dmfs.xmlobjects.pull.ParserContext;
import org.dmfs.xmlobjects.pull.XmlObjectPullParserException;

import android.os.Bundle;
import android.os.Parcelable;


/**
 * A builder that builds {@link Bundle}s from XML.
 * 

* The builder accepts bundle-value elements. These elements must have a key attribute that becomes the key of the value in the * bundle. The first child element of value becomes the value. value must not have more than one child element. *

* Example: * *

 * {@code
 * 
 *     
 *         This string will be stored in the bundle
 *     
 *     
 *         This is a special case: If bundle-value contains a text node it becomes the string value of this bundle-value.
 *     
 *     
 *         true
 *     
 *     
 *         
 *             
 *                 This string is in the nested bundle
 *             
 *         
 *     
 * 
 * }
 * 
*

* TODO: Add support for missing types * * @author Marten Gajda */ public class BundleObjectBuilder extends AbstractObjectBuilder { public final static BundleObjectBuilder INSTANCE = new BundleObjectBuilder(); /** * Private {@link org.dmfs.xmlobjects.ElementDescriptor} for bundle values. */ public final static ElementDescriptor VALUE = ElementDescriptor.register(QualifiedName.get(Model.NAMESPACE, "bundle-value"), new BaseAndroidObjectBuilder() { @Override public ValueHolder get(ElementDescriptor descriptor, ValueHolder recycle, ParserContext context) throws XmlObjectPullParserException { if (recycle != null) { recycle.key = null; recycle.value = null; return recycle; } return new ValueHolder(); } @Override public ValueHolder update(ElementDescriptor descriptor, ValueHolder object, QualifiedName attribute, String value, ParserContext context) throws XmlObjectPullParserException { if (attribute == Model.ATTR_KEY) { object.key = value; } return object; } @Override public ValueHolder update(ElementDescriptor descriptor, ValueHolder object, ElementDescriptor childDescriptor, V child, ParserContext context) throws XmlObjectPullParserException { if (object.value != null) { throw new IllegalStateException("'bundle-value' can not have more than one child element"); } object.value = child; return object; } public ValueHolder update(ElementDescriptor descriptor, ValueHolder object, String text, ParserContext context) throws XmlObjectPullParserException { if (object.value != null) { throw new IllegalStateException("'bundle-value' can not have more than one child element"); } object.value = format(text, context); return object; }; @Override public ValueHolder finish(ElementDescriptor descriptor, ValueHolder object, ParserContext context) throws XmlObjectPullParserException { if (object.key == null) { throw new IllegalArgumentException("'bundle-value' must have a key"); } return object; } }); @Override public Bundle get(ElementDescriptor descriptor, Bundle recycle, ParserContext context) throws XmlObjectPullParserException { if (recycle != null) { recycle.clear(); return recycle; } return new Bundle(); } @Override public Bundle update(ElementDescriptor descriptor, Bundle object, ElementDescriptor childDescriptor, V child, ParserContext context) throws XmlObjectPullParserException { if (child instanceof ValueHolder) { ValueHolder valueHolder = (ValueHolder) child; if (valueHolder.value instanceof String) { object.putString(valueHolder.key, (String) valueHolder.value); } if (valueHolder.value instanceof CharSequence) { object.putCharSequence(valueHolder.key, (CharSequence) valueHolder.value); } else if (valueHolder.value instanceof Integer) { object.putInt(valueHolder.key, (Integer) valueHolder.value); } else if (valueHolder.value instanceof Boolean) { object.putBoolean(valueHolder.key, (Boolean) valueHolder.value); } else if (valueHolder.value instanceof Bundle) { object.putBundle(valueHolder.key, (Bundle) valueHolder.value); } else if (valueHolder.value instanceof Parcelable) { object.putParcelable(valueHolder.key, (Parcelable) valueHolder.value); } else if (valueHolder.value instanceof String[]) { object.putStringArray(valueHolder.key, (String[]) valueHolder.value); } if (valueHolder.value instanceof CharSequence[]) { object.putCharSequenceArray(valueHolder.key, (CharSequence[]) valueHolder.value); } else if (valueHolder.value instanceof int[]) { object.putIntArray(valueHolder.key, (int[]) valueHolder.value); } else if (valueHolder.value instanceof boolean[]) { object.putBooleanArray(valueHolder.key, (boolean[]) valueHolder.value); } else if (valueHolder.value instanceof Parcelable[]) { object.putParcelableArray(valueHolder.key, (Parcelable[]) valueHolder.value); } else if (valueHolder.value instanceof ArrayList) { // FIXME: we can't trust that the generic type is String. We have to check the type of the first element, if there is any // object.putStringArrayList(valueHolder.key, (ArrayList) valueHolder.value); // object.putStringArrayList // object.putIntegerArrayList // object.putCharSequenceArrayList // object.putParcelableArrayList } // recycle the ValueHolder context.recycle(childDescriptor, child); } return object; } /** * Private holder for bundle values. */ private static class ValueHolder { /** * The key of the value. */ private String key; /** * The value itself. */ private Object value; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy