2018-12-31 20:04:05 +01:00
< ? php
require_once 'TlDocumentationGenerator.php' ;
class JavadocTlDocumentationGenerator extends TlDocumentationGenerator
{
private $nullable_type ;
private $nullable_annotation ;
private $java_version ;
protected function escapeDocumentation ( $doc )
{
2019-02-20 02:23:02 +01:00
$doc = preg_replace_callback ( '/(?<!["A-Za-z_\/])[A-Za-z]*_[A-Za-z_]*/' ,
2019-01-07 02:32:49 +01:00
function ( $word_matches )
{
return preg_replace_callback ( '/_([A-Za-z])/' , function ( $matches ) { return strtoupper ( $matches [ 1 ]);}, $word_matches [ 0 ]);
}, $doc );
2018-12-31 20:04:05 +01:00
$doc = htmlspecialchars ( $doc );
$doc = str_replace ( '*/' , '*/' , $doc );
return $doc ;
}
2018-03-16 22:26:27 +01:00
protected function getFieldName ( $name , $class_name )
2018-12-31 20:04:05 +01:00
{
if ( substr ( $name , 0 , 6 ) === 'param_' ) {
$name = substr ( $name , 6 );
}
return preg_replace_callback ( '/_([A-Za-z])/' , function ( $matches ) { return strtoupper ( $matches [ 1 ]);}, trim ( $name ));
}
protected function getClassName ( $type )
{
2018-03-16 22:26:27 +01:00
return implode ( array_map ( 'ucfirst' , explode ( '.' , trim ( $type , " \r \n ; " ))));
2018-12-31 20:04:05 +01:00
}
protected function getTypeName ( $type )
{
switch ( $type ) {
case 'Bool' :
return 'boolean' ;
case 'int32' :
return 'int' ;
case 'int53' :
case 'int64' :
return 'long' ;
case 'double' :
return $type ;
case 'string' :
return 'String' ;
case 'bytes' :
return 'byte[]' ;
case 'bool' :
case 'int' :
case 'long' :
case 'Int' :
case 'Long' :
case 'Int32' :
case 'Int53' :
case 'Int64' :
case 'Double' :
case 'String' :
case 'Bytes' :
$this -> printError ( " Wrong type $type " );
return '' ;
default :
if ( substr ( $type , 0 , 6 ) === 'vector' ) {
if ( $type [ 6 ] !== '<' || $type [ strlen ( $type ) - 1 ] !== '>' ) {
$this -> printError ( " Wrong vector subtype in $type " );
return '' ;
}
return $this -> getTypeName ( substr ( $type , 7 , - 1 )) . '[]' ;
}
if ( preg_match ( '/[^A-Za-z0-9.]/' , $type )) {
$this -> printError ( " Wrong type $type " );
return '' ;
}
return $this -> getClassName ( $type );
}
}
protected function getBaseClassName ( $is_function )
{
return $is_function ? 'Function' : 'Object' ;
}
protected function needRemoveLine ( $line )
{
return strpos ( trim ( $line ), '/**' ) === 0 || strpos ( trim ( $line ), '*' ) === 0 ||
( $this -> nullable_type && strpos ( $line , $this -> nullable_type ) > 0 );
}
protected function needSkipLine ( $line )
{
2019-01-07 02:32:49 +01:00
$line = $this -> fixLine ( trim ( $line ));
return ( strpos ( $line , 'public' ) !== 0 && ! $this -> isHeaderLine ( $line )) || $line === 'public @interface Constructors {}' ;
2018-12-31 20:04:05 +01:00
}
protected function isHeaderLine ( $line )
{
2019-01-07 02:32:49 +01:00
return trim ( $line ) === '@Override' || trim ( $line ) === '@Constructors' ;
2018-12-31 20:04:05 +01:00
}
protected function extractClassName ( $line )
{
if ( strpos ( $line , 'public static class ' ) > 0 ) {
2021-12-30 02:04:04 +01:00
return preg_split ( '/( |<|>)/' , trim ( $line ))[ 3 ];
2018-12-31 20:04:05 +01:00
}
return '' ;
}
protected function fixLine ( $line )
{
if ( strpos ( $line , 'CONSTRUCTOR = ' ) > 0 ) {
return substr ( $line , 0 , strpos ( $line , '=' ));
}
return $this -> nullable_annotation ? str_replace ( $this -> nullable_annotation . ' ' , '' , $line ) : $line ;
}
protected function addGlobalDocumentation ()
{
2018-01-23 16:33:12 +01:00
if ( $this -> nullable_type ) {
2018-03-16 22:26:27 +01:00
$nullable_type_import = " import $this->nullable_type ; " . PHP_EOL ;
2018-01-23 16:33:12 +01:00
} else {
$nullable_type_import = '' ;
}
2018-12-31 20:04:05 +01:00
$this -> addDocumentation ( 'public class TdApi {' , <<< EOT
2018-01-23 16:33:12 +01:00
$nullable_type_import /**
2018-12-31 20:04:05 +01:00
* This class contains as static nested classes all other TDLib interface
* type - classes and function - classes .
* < p >
* It has no inner classes , functions or public members .
*/
EOT
);
$this -> addDocumentation ( ' public abstract static class Object {' , <<< EOT
/**
* This class is a base class for all TDLib interface classes .
*/
EOT
);
$this -> addDocumentation ( ' public abstract int getConstructor();' , <<< EOT
/**
* @ return identifier uniquely determining type of the object .
*/
EOT
);
2018-01-22 06:24:47 +01:00
$this -> addDocumentation ( ' public native String toString();' , <<< EOT
2018-12-31 20:04:05 +01:00
/**
* @ return string representation of the object .
*/
EOT
);
$this -> addDocumentation ( ' public abstract static class Function extends Object {' , <<< EOT
/**
* This class is a base class for all TDLib interface function - classes .
*/
EOT
);
$this -> addDocumentation ( ' public static final int CONSTRUCTOR' , <<< EOT
/**
* Identifier uniquely determining type of the object .
*/
EOT
);
$this -> addDocumentation ( ' public int getConstructor() {' , <<< EOT
/**
* @ return this . CONSTRUCTOR
*/
EOT
);
}
protected function addAbstractClassDocumentation ( $class_name , $documentation )
{
$this -> addDocumentation ( " public abstract static class $class_name extends Object { " , <<< EOT
/**
* This class is an abstract base class .
* $documentation
*/
EOT
);
}
2019-03-25 00:07:31 +01:00
protected function getFunctionReturnTypeDescription ( $return_type , $for_constructor )
2018-12-31 20:04:05 +01:00
{
2019-03-25 00:07:31 +01:00
$shift = $for_constructor ? ' ' : ' ' ;
return PHP_EOL . $shift . '*' . PHP_EOL . $shift . " * <p> Returns { @link $return_type $return_type } </p> " ;
2019-03-24 23:18:25 +01:00
}
2018-12-31 20:04:05 +01:00
2021-12-30 02:04:04 +01:00
protected function addClassDocumentation ( $class_name , $base_class_name , $return_type , $description )
2019-03-24 23:18:25 +01:00
{
2021-12-30 02:04:04 +01:00
$this -> addDocumentation ( " public static class $class_name extends " . ( $base_class_name == " Function " ? " Function<R extends Object> " : $base_class_name ) . ( empty ( $return_type ) ? " " : " < " . $return_type . " > " ) . " { " , <<< EOT
2018-12-31 20:04:05 +01:00
/**
2019-03-24 23:18:25 +01:00
* $description
2018-12-31 20:04:05 +01:00
*/
EOT
);
}
protected function addFieldDocumentation ( $class_name , $field_name , $type_name , $field_info , $may_be_null )
{
$full_line = $class_name . " public $type_name $field_name ; " ;
$this -> addDocumentation ( $full_line , <<< EOT
/**
* $field_info
*/
EOT
);
if ( $may_be_null && $this -> nullable_annotation && ( $this -> java_version >= 8 || substr ( $type_name , - 1 ) != ']' )) {
2018-06-25 16:23:56 +02:00
$this -> addLineReplacement ( $full_line , " $this->nullable_annotation public $type_name $field_name ; " . PHP_EOL );
2018-12-31 20:04:05 +01:00
}
}
2019-03-25 00:07:31 +01:00
protected function addDefaultConstructorDocumentation ( $class_name , $class_description )
2018-12-31 20:04:05 +01:00
{
$this -> addDocumentation ( " public $class_name () { " , <<< EOT
/**
2019-03-25 00:07:31 +01:00
* $class_description
2018-12-31 20:04:05 +01:00
*/
EOT
);
}
2019-03-25 00:07:31 +01:00
protected function addFullConstructorDocumentation ( $class_name , $class_description , $known_fields , $info )
2018-12-31 20:04:05 +01:00
{
$full_constructor = " public $class_name ( " ;
$colon = '' ;
foreach ( $known_fields as $name => $type ) {
2018-03-16 22:26:27 +01:00
$full_constructor .= $colon . $this -> getTypeName ( $type ) . ' ' . $this -> getFieldName ( $name , $class_name );
2018-12-31 20:04:05 +01:00
$colon = ', ' ;
}
$full_constructor .= ') {' ;
$full_doc = <<< EOT
/**
2019-03-25 00:07:31 +01:00
* $class_description
2018-12-31 20:04:05 +01:00
*
EOT ;
foreach ( $known_fields as $name => $type ) {
2018-03-16 22:26:27 +01:00
$full_doc .= ' * @param ' . $this -> getFieldName ( $name , $class_name ) . ' ' . $info [ $name ] . PHP_EOL ;
2018-12-31 20:04:05 +01:00
}
$full_doc .= ' */' ;
$this -> addDocumentation ( $full_constructor , $full_doc );
}
public function __construct ( $nullable_type , $nullable_annotation , $java_version ) {
$this -> nullable_type = trim ( $nullable_type );
$this -> nullable_annotation = trim ( $nullable_annotation );
$this -> java_version = intval ( $java_version );
}
}
$nullable_type = isset ( $argv [ 3 ]) ? $argv [ 3 ] : '' ;
$nullable_annotation = isset ( $argv [ 4 ]) ? $argv [ 4 ] : '' ;
$java_version = isset ( $argv [ 5 ]) ? intval ( $argv [ 5 ]) : 7 ;
$generator = new JavadocTlDocumentationGenerator ( $nullable_type , $nullable_annotation , $java_version );
$generator -> generate ( $argv [ 1 ], $argv [ 2 ]);