From: Sergey Vladimirov Date: Tue, 9 Aug 2011 07:48:13 +0000 (+0000) Subject: restore (merge, cleanup and move to dev. package) XSLT utilities classes for model... X-Git-Tag: REL_3_8_BETA4~28 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0a8b8570ad7eebae9d6feb6141b402991a29fc23;p=poi.git restore (merge, cleanup and move to dev. package) XSLT utilities classes for model classes autogeneration. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1155242 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/scratchpad/src/org/apache/poi/hwpf/dev/FieldIterator.java b/src/scratchpad/src/org/apache/poi/hwpf/dev/FieldIterator.java new file mode 100644 index 0000000000..a535fc4439 --- /dev/null +++ b/src/scratchpad/src/org/apache/poi/hwpf/dev/FieldIterator.java @@ -0,0 +1,156 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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.apache.poi.hwpf.dev; + +import org.apache.poi.util.Internal; + +/** + *

+ * For iterating through our fields. Used during model classes autogeneration. + *

+ * + * @author Glen Stampoultzis (glens at apache.org) + */ +@Internal +public class FieldIterator +{ + + protected int offset; + + public FieldIterator() + { + } + + public String calcSize( int fieldNumber, String fieldName, String size, + String type ) + { + String result = " + "; + if ( type.startsWith( "custom:" ) ) + { + String javaFieldName = RecordUtil.getFieldName( fieldNumber, + fieldName, 0 ); + return result + javaFieldName + ".getSize()"; + } + else if ( "var".equals( size ) ) + { + String javaFieldName = RecordUtil.getFieldName( fieldNumber, + fieldName, 0 ); + return result + " (" + javaFieldName + ".length() *2)"; + } + else if ( "varword".equals( size ) ) + { + String javaFieldName = RecordUtil.getFieldName( fieldNumber, + fieldName, 0 ); + return result + javaFieldName + ".length * 2 + 2"; + } + else + { + return result + size; + } + } + + public String fillDecoder( String size, String type ) + { + + String result = ""; + + if ( type.equals( "short[]" ) ) + result = "LittleEndian.getSimpleShortArray(data, 0x" + + Integer.toHexString( offset ) + " + offset," + size + ")"; + else if ( type.equals( "byte[]" ) ) + result = "LittleEndian.getByteArray(data, 0x" + + Integer.toHexString( offset ) + " + offset," + size + ")"; + else if ( type.equals( "BorderCode" ) ) + result = "new BorderCode(data, 0x" + Integer.toHexString( offset ) + + " + offset)"; + else if ( type.equals( "DateAndTime" ) ) + result = "new DateAndTime(data, 0x" + Integer.toHexString( offset ) + + " + offset)"; + else if ( size.equals( "2" ) ) + result = "LittleEndian.getShort(data, 0x" + + Integer.toHexString( offset ) + " + offset)"; + else if ( size.equals( "4" ) ) + result = "LittleEndian.getInt(data, 0x" + + Integer.toHexString( offset ) + " + offset)"; + else if ( size.equals( "1" ) ) + result = "data[ 0x" + Integer.toHexString( offset ) + " + offset ]"; + else if ( type.equals( "double" ) ) + result = "LittleEndian.getDouble(data, 0x" + + Integer.toHexString( offset ) + " + offset)"; + + try + { + offset += Integer.parseInt( size ); + } + catch ( NumberFormatException ignore ) + { + } + return result; + } + + public String serialiseEncoder( int fieldNumber, String fieldName, + String size, String type ) + { + // String javaType = RecordUtil.getType(size, type, 0); + String javaFieldName = RecordUtil.getFieldName( fieldNumber, fieldName, + 0 ); + + String result = ""; + + if ( type.equals( "short[]" ) ) + result = "LittleEndian.putShortArray(data, 0x" + + Integer.toHexString( offset ) + " + offset, " + + javaFieldName + ");"; + else if ( type.equals( "byte[]" ) ) + result = "System.arraycopy(" + javaFieldName + ", 0, data, 0x" + + Integer.toHexString( offset ) + " + offset, " + + javaFieldName + ".length);"; + else if ( type.equals( "BorderCode" ) ) + result = javaFieldName + ".serialize(data, 0x" + + Integer.toHexString( offset ) + " + offset);"; + else if ( type.equals( "DateAndTime" ) ) + result = javaFieldName + ".serialize(data, 0x" + + Integer.toHexString( offset ) + " + offset);"; + else if ( size.equals( "2" ) ) + result = "LittleEndian.putShort(data, 0x" + + Integer.toHexString( offset ) + " + offset, (short)" + + javaFieldName + ");"; + else if ( size.equals( "4" ) ) + result = "LittleEndian.putInt(data, 0x" + + Integer.toHexString( offset ) + " + offset, " + + javaFieldName + ");"; + else if ( size.equals( "1" ) ) + result = "data[ 0x" + Integer.toHexString( offset ) + + " + offset] = " + javaFieldName + ";"; + else if ( type.equals( "double" ) ) + result = "LittleEndian.putDouble(data, 0x" + + Integer.toHexString( offset ) + " + offset, " + + javaFieldName + ");"; + + try + { + offset += Integer.parseInt( size ); + } + catch ( NumberFormatException ignore ) + { + } + return result; + + } + +} diff --git a/src/scratchpad/src/org/apache/poi/hwpf/dev/RecordUtil.java b/src/scratchpad/src/org/apache/poi/hwpf/dev/RecordUtil.java new file mode 100644 index 0000000000..cc912785dc --- /dev/null +++ b/src/scratchpad/src/org/apache/poi/hwpf/dev/RecordUtil.java @@ -0,0 +1,208 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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.apache.poi.hwpf.dev; + +import org.apache.poi.util.Internal; + +/** + * Helper functions for the record transformations. Used during model classes + * autogeneration. + * + * @author Glen Stampoultzis (glens at apache.org) + * @author Andrew C. Oliver (acoliver at apache dot org) + */ +@Internal +public class RecordUtil +{ + + public static String getBitFieldFunction( String name, String bitMask, + String parentType, String withType ) + { + String type = getBitFieldType( name, bitMask, parentType ); + + String retVal = new String(); + if ( withType.equals( "true" ) ) + { + retVal = type + " "; + } + if ( type.equals( "boolean" ) ) + { + retVal += "is" + getFieldName1stCap( name, 0 ); + } + else + { + retVal += "get" + getFieldName1stCap( name, 0 ); + } + return retVal; + + } + + public static String getBitFieldGet( String name, String bitMask, + String parentType, String parentField ) + { + String type = getBitFieldType( name, bitMask, parentType ); + + String retVal = null; + + if ( type.equals( "boolean" ) ) + retVal = name + ".isSet(" + parentField + ");"; + else + retVal = "( " + type + " )" + name + ".getValue(" + parentField + + ");"; + + return retVal; + + } + + public static String getBitFieldSet( String name, String bitMask, + String parentType, String parentField ) + { + String type = getBitFieldType( name, bitMask, parentType ); + + String retVal = null; + + if ( type.equals( "boolean" ) ) + retVal = "(" + parentType + ")" + getFieldName( name, 0 ) + + ".setBoolean(" + parentField + ", value)"; + else + retVal = "(" + parentType + ")" + getFieldName( name, 0 ) + + ".setValue(" + parentField + ", value)"; + return retVal; + } + + public static String getBitFieldType( String name, String bitMask, + String parentType ) + { + byte parentSize = 0; + byte numBits = 0; + int mask = (int) Long.parseLong( bitMask.substring( 2 ), 16 ); + + if ( parentType.equals( "byte" ) ) + parentSize = 8; + else if ( parentType.equals( "short" ) ) + parentSize = 16; + else if ( parentType.equals( "int" ) ) + parentSize = 32; + + for ( int x = 0; x < parentSize; x++ ) + { + int temp = mask; + numBits += ( temp >> x ) & 0x1; + } + + if ( numBits == 1 ) + { + return "boolean"; + } + else if ( numBits < 8 ) + { + return "byte"; + } + else if ( numBits < 16 ) + { + return "short"; + } + else + { + return "int"; + } + } + + public static String getConstName( String parentName, String constName, + int padTo ) + { + StringBuffer fieldName = new StringBuffer(); + toConstIdentifier( parentName, fieldName ); + fieldName.append( '_' ); + toConstIdentifier( constName, fieldName ); + pad( fieldName, padTo ); + return fieldName.toString(); + } + + public static String getFieldName( int position, String name, int padTo ) + { + StringBuffer fieldName = new StringBuffer( "field_" + position + "_" ); + toIdentifier( name, fieldName ); + pad( fieldName, padTo ); + + return fieldName.toString(); + } + + public static String getFieldName( String name, int padTo ) + { + StringBuffer fieldName = new StringBuffer(); + toIdentifier( name, fieldName ); + pad( fieldName, padTo ); + + return fieldName.toString(); + } + + public static String getFieldName1stCap( String name, int padTo ) + { + StringBuffer fieldName = new StringBuffer(); + toIdentifier( name, fieldName ); + fieldName.setCharAt( 0, Character.toUpperCase( fieldName.charAt( 0 ) ) ); + pad( fieldName, padTo ); + + return fieldName.toString(); + } + + public static String getType1stCap( String size, String type, int padTo ) + { + StringBuffer result = new StringBuffer(); + result.append( type ); + result = pad( result, padTo ); + result.setCharAt( 0, Character.toUpperCase( result.charAt( 0 ) ) ); + + return result.toString(); + } + + protected static StringBuffer pad( StringBuffer fieldName, int padTo ) + { + for ( int i = fieldName.length(); i < padTo; i++ ) + fieldName.append( ' ' ); + return fieldName; + } + + private static void toConstIdentifier( String name, StringBuffer fieldName ) + { + for ( int i = 0; i < name.length(); i++ ) + { + if ( name.charAt( i ) == ' ' ) + fieldName.append( '_' ); + else + fieldName.append( Character.toUpperCase( name.charAt( i ) ) ); + } + } + + private static void toIdentifier( String name, StringBuffer fieldName ) + { + for ( int i = 0; i < name.length(); i++ ) + { + if ( name.charAt( i ) == ' ' ) + fieldName.append( Character.toUpperCase( name.charAt( ++i ) ) ); + else + fieldName.append( name.charAt( i ) ); + } + } + + public RecordUtil() + { + } + +} diff --git a/src/types/styles/hdftype.xsl b/src/types/styles/hdftype.xsl index dd87d1d1ff..85b51f2054 100644 --- a/src/types/styles/hdftype.xsl +++ b/src/types/styles/hdftype.xsl @@ -19,8 +19,8 @@ --> @@ -31,12 +31,8 @@ package ; -import org.apache.poi.util.BitField; -import org.apache.poi.util.Internal; -import org.apache.poi.util.LittleEndian; -import org.apache.poi.util.StringUtil; -import org.apache.poi.util.HexDump; import org.apache.poi.hwpf.usermodel.*; +import org.apache.poi.util.*; /** *