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
This commit is contained in:
Sergey Vladimirov 2011-08-09 07:48:13 +00:00
parent efceea044f
commit 1c04bacc34
3 changed files with 367 additions and 7 deletions

View File

@ -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;
/**
* <p>
* For iterating through our fields. Used during model classes autogeneration.
* </p>
*
* @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;
}
}

View File

@ -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()
{
}
}

View File

@ -19,8 +19,8 @@
-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:recutil="org.apache.poi.hdf.generator.HDFRecordUtil"
xmlns:field="org.apache.poi.hdf.generator.HDFFieldIterator"
xmlns:recutil="org.apache.poi.hwpf.dev.RecordUtil"
xmlns:field="org.apache.poi.hwpf.dev.FieldIterator"
xmlns:java="java" >
<xsl:output method="text"/>
@ -31,12 +31,8 @@
package <xsl:value-of select="@package"/>;
</xsl:if>
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.*;
/**
* <xsl:value-of select="/record/description"/>