/**
 *  Copyright 2011 Ryszard Wiśniewski <brut.alll@gmail.com>
 *
 *  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 brut.androlib.res.data.value;

import brut.androlib.AndrolibException;
import brut.androlib.res.data.ResResSpec;
import brut.androlib.res.data.ResResource;
import brut.androlib.res.xml.ResValuesXmlSerializable;
import brut.util.Duo;
import java.io.IOException;
import org.xmlpull.v1.XmlSerializer;

/**
 * @author Ryszard Wiśniewski <brut.alll@gmail.com>
 */
public class ResStyleValue extends ResBagValue implements ResValuesXmlSerializable {
    ResStyleValue(ResReferenceValue parent,
            Duo<Integer, ResScalarValue>[] items, ResValueFactory factory) {
        super(parent);

        mItems = new Duo[items.length];
        for (int i = 0; i < items.length; i++) {
            mItems[i] = new Duo<ResReferenceValue, ResScalarValue>(
                factory.newReference(items[i].m1, null), items[i].m2);
        }
    }

    @Override
    public void serializeToResValuesXml(XmlSerializer serializer, ResResource res)
            throws IOException, AndrolibException {
        serializer.startTag(null, "style");
        serializer.attribute(null, "name", res.getResSpec().getName());
        if (! mParent.isNull()) {
            serializer.attribute(null, "parent", mParent.encodeAsResXmlAttr());
        }
        for (int i = 0; i < mItems.length; i++) {
            ResResSpec spec = mItems[i].m1.getReferent();
            
            // fix for ClassCastException by Alsan Wong <alsan.wong@gmail.com> at 2013/01/18 15:08
            ResAttr attr = null;
			String value = null;
			
			try {
				// hacky-fix remove bad ReferenceVars
				ResValue attrValue = spec.getDefaultResource().getValue();
				value = attrValue.toString();	// I know, this is not good, but I don't want another variable just for exception handling
				
				if(true == value.contains("ResReferenceValue@")) {
					continue;
				}
				
				attr = (ResAttr)attrValue;
				value = attr.convertToResXmlFormat(mItems[i].m2);
			} catch(ClassCastException e) {
				System.out.println("ClassCastException: " + e.getMessage());
				System.out.println(value);
			}

            if (value == null) {
                value = mItems[i].m2.encodeAsResXmlValue();
            }

            if (value == null) {
                continue;
            }

            serializer.startTag(null, "item");
            serializer.attribute(null, "name",
                spec.getFullName(res.getResSpec().getPackage(), true));
            serializer.text(value);
            serializer.endTag(null, "item");
        }
        serializer.endTag(null, "style");
    }


    private final Duo<ResReferenceValue, ResScalarValue>[] mItems;
}

@@ -2000,169 +2000,8 @@
   //
- fix for ClassCastException by Alsan Wong %3Calsan.wong@gmail.com%3E at 2013/01/18 15:08%0A            ResAttr attr = null;%0A%09%09%09String value = null;%0A%09%09%09%0A%09%09%09try %7B%0A%09%09%09%09//
  hac
@@ -2036,33 +2036,24 @@
 ars%0A
-%09%09%09%09ResValue attrValue = 
+            if (
 spec
@@ -2088,441 +2088,234 @@
 ue()
-;%0A%09%09%09%09value = attrValue.toString();%09// I know, this is not good, but I don't want another variable just for exception handling%0A%09%09%09%09%0A%09%09%09%09if(true == value.contains(%22ResReferenceValue@%22)) %7B%0A%09%09%09%09%09continue;%0A%09%09%09%09%7D%0A%09%09%09%09%0A%09%09%09%09attr = (ResAttr)attrValue;%0A%09%09%09%09value = attr.convertToResXmlFormat(mItems%5Bi%5D.m2);%0A%09%09%09%7D catch(ClassCastException e) %7B%0A%09%09%09%09System.out.println(%22ClassCastException: %22 + e.getMessage());%0A%09%09%09%09System.out.println(value);%0A%09%09%09%7D
+.toString().contains(%22ResReferenceValue@%22)) %7B%0A            %09continue;%0A            %7D%0A            ResAttr attr = (ResAttr) spec.getDefaultResource().getValue();%0A            String value = attr.convertToResXmlFormat(mItems%5Bi%5D.m2);
 %0A%0A  

Revert to c836995ead80bc13e10627e1939f153b2faefc6e