Ejemplo n.º 1
0
func (parser *Parser) Parse(data []byte) error {
	var word0 int64
	parser.Data = data

	for parser.ParserOffset < len(parser.Data) {
		word0 = int64(parser.getLEWord(parser.ParserOffset))
		switch word0 {
		case WORD_START_DOCUMENT:
			parser.parseStartDocument()
		case WORD_STRING_TABLE:
			parser.parseStringTable()
		case WORD_RES_TABLE:
			parser.parseResourceTable()
		case WORD_START_NS:
			parser.parseNamespace(true)
		case WORD_END_NS:
			parser.parseNamespace(false)
		case WORD_START_TAG:
			parser.parseStartTag()
		case WORD_END_TAG:
			parser.parseEndTag()
		case WORD_TEXT:
			parser.parseText()
		case WORD_EOS:
			parser.listener.EndDocument()
		default:
			parser.ParserOffset += WORD_SIZE
			log.Warnf(TAG+"Unknown word 0x%x @", word0, parser.ParserOffset)
		}
	}

	parser.listener.EndDocument()
	return nil
}
Ejemplo n.º 2
0
/**
 * @param word
 *            a word read in an attribute data
 * @return the typed value
 */
func (parser *Parser) getAttributeValue(tpe int, data int) string {
	var res string

	switch tpe {
	case TYPE_STRING:
		res = parser.getString(data)
	case TYPE_DIMEN:
		res = fmt.Sprintf("%v", data>>8) + DIMEN[data&0xFF]
	case TYPE_FRACTION:
		fracValue := (float64(data) / (float64(0x7FFFFFFF)))
		// res = String.format("%.2f%%", fracValue);
		//res = new DecimalFormat("#.##%").format(fracValue)
		res = fmt.Sprintf("%.2f%%", fracValue)
	case TYPE_FLOAT:
		buf := new(bytes.Buffer)
		err := binary.Write(buf, binary.LittleEndian, int32(data))
		if err != nil {
			panic(err)
		}
		var fl float32
		err = binary.Read(buf, binary.LittleEndian, &fl)
		if err != nil {
			panic(err)
		}
		//res = Float.toString(Float.intBitsToFloat(data))
		res = fmt.Sprintf("%f", fl)
	case TYPE_INT:
		fallthrough
	case TYPE_FLAGS:
		res = strconv.Itoa(data)
	case TYPE_BOOL:
		//res = Boolean.toString(data != 0)
		if data != 0 {
			res = "true"
		} else {
			res = "false"
		}
	case TYPE_COLOR:
		fallthrough
	case TYPE_COLOR2:
		res = fmt.Sprintf("%#08X", data)
	case TYPE_ID_REF:
		res = fmt.Sprintf("@id/0x%08X", data)
	case TYPE_ATTR_REF:
		res = fmt.Sprintf("?id/0x%08X", data)
	default:
		log.Warnf(TAG+"(type=%d) : %v (0x%08X) @%d", tpe, data,
			data, parser.ParserOffset)
		res = fmt.Sprintf("%08X/0x%08X", tpe, data)
	}

	return res
}