Java Code Examples for org.hibernate.internal.util.StringHelper#split()

The following examples show how to use org.hibernate.internal.util.StringHelper#split() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetadataBuilderImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private ArrayList<MetadataSourceType> resolveInitialSourceProcessOrdering(ConfigurationService configService) {
	final ArrayList<MetadataSourceType> initialSelections = new ArrayList<>();

	final String sourceProcessOrderingSetting = configService.getSetting(
			AvailableSettings.ARTIFACT_PROCESSING_ORDER,
			StandardConverters.STRING
	);
	if ( sourceProcessOrderingSetting != null ) {
		final String[] orderChoices = StringHelper.split( ",; ", sourceProcessOrderingSetting, false );
		initialSelections.addAll( CollectionHelper.arrayList( orderChoices.length ) );
		for ( String orderChoice : orderChoices ) {
			initialSelections.add( MetadataSourceType.parsePrecedence( orderChoice ) );
		}
	}
	if ( initialSelections.isEmpty() ) {
		initialSelections.add( MetadataSourceType.HBM );
		initialSelections.add( MetadataSourceType.CLASS );
	}

	return initialSelections;
}
 
Example 2
Source File: PathHelper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Turns a path into an AST.
 *
 * @param path    The path.
 * @param factory The AST factory to use.
 * @return An HQL AST representing the path.
 */
public static AST parsePath(String path, ASTFactory factory) {
	String[] identifiers = StringHelper.split( ".", path );
	AST lhs = null;
	for ( int i = 0; i < identifiers.length; i++ ) {
		String identifier = identifiers[i];
		AST child = ASTUtil.create( factory, HqlSqlTokenTypes.IDENT, identifier );
		if ( i == 0 ) {
			lhs = child;
		}
		else {
			lhs = ASTUtil.createBinarySubtree( factory, HqlSqlTokenTypes.DOT, ".", lhs, child );
		}
	}
	if ( LOG.isDebugEnabled() ) {
		LOG.debugf( "parsePath() : %s -> %s", path, ASTUtil.getDebugString( lhs ) );
	}
	return lhs;
}
 
Example 3
Source File: WhereParser.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void preprocess(String token, QueryTranslatorImpl q) throws QueryException {
	// ugly hack for cases like "elements(foo.bar.collection)"
	// (multi-part path expression ending in elements or indices)
	String[] tokens = StringHelper.split( ".", token, true );
	if (
			tokens.length > 5 &&
			( CollectionPropertyNames.COLLECTION_ELEMENTS.equals( tokens[tokens.length - 1] )
			|| CollectionPropertyNames.COLLECTION_INDICES.equals( tokens[tokens.length - 1] ) )
	) {
		pathExpressionParser.start( q );
		for ( int i = 0; i < tokens.length - 3; i++ ) {
			pathExpressionParser.token( tokens[i], q );
		}
		pathExpressionParser.token( null, q );
		pathExpressionParser.end( q );
		addJoin( pathExpressionParser.getWhereJoin(), q );
		pathExpressionParser.ignoreInitialJoin();
	}
}
 
Example 4
Source File: ConfigurationHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Convert a string to an array of strings.  The assumption is that
 * the individual array elements are delimited in the source stringForm
 * param by the delim param.
 *
 * @param stringForm The string form of the string array.
 * @param delim The delimiter used to separate individual array elements.
 * @return The array; never null, though may be empty.
 */
public static String[] toStringArray(String stringForm, String delim) {
	// todo : move to StringHelper?
	if ( stringForm != null ) {
		return StringHelper.split( delim, stringForm );
	}
	else {
		return ArrayHelper.EMPTY_STRING_ARRAY;
	}
}
 
Example 5
Source File: EntityManagerFactoryBuilderImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
	protected List<AttributeConverterDefinition> populate(
			MetadataSources metadataSources,
			MergedSettings mergedSettings,
			StandardServiceRegistry ssr) {
//		final ClassLoaderService classLoaderService = ssr.getService( ClassLoaderService.class );
//
//		// todo : make sure MetadataSources/Metadata are capable of handling duplicate sources
//
//		// explicit persistence unit mapping files listings
//		if ( persistenceUnit.getMappingFileNames() != null ) {
//			for ( String name : persistenceUnit.getMappingFileNames() ) {
//				metadataSources.addResource( name );
//			}
//		}
//
//		// explicit persistence unit managed class listings
//		//		IMPL NOTE : managed-classes can contain class or package names!!!
//		if ( persistenceUnit.getManagedClassNames() != null ) {
//			for ( String managedClassName : persistenceUnit.getManagedClassNames() ) {
//				// try it as a class name first...
//				final String classFileName = managedClassName.replace( '.', '/' ) + ".class";
//				final URL classFileUrl = classLoaderService.locateResource( classFileName );
//				if ( classFileUrl != null ) {
//					// it is a class
//					metadataSources.addAnnotatedClassName( managedClassName );
//					continue;
//				}
//
//				// otherwise, try it as a package name
//				final String packageInfoFileName = managedClassName.replace( '.', '/' ) + "/package-info.class";
//				final URL packageInfoFileUrl = classLoaderService.locateResource( packageInfoFileName );
//				if ( packageInfoFileUrl != null ) {
//					// it is a package
//					metadataSources.addPackage( managedClassName );
//					continue;
//				}
//
//				LOG.debugf(
//						"Unable to resolve class [%s] named in persistence unit [%s]",
//						managedClassName,
//						persistenceUnit.getName()
//				);
//			}
//		}

		List<AttributeConverterDefinition> attributeConverterDefinitions = null;

		// add any explicit Class references passed in
		final List<Class> loadedAnnotatedClasses = (List<Class>) configurationValues.remove( AvailableSettings.LOADED_CLASSES );
		if ( loadedAnnotatedClasses != null ) {
			for ( Class cls : loadedAnnotatedClasses ) {
				if ( AttributeConverter.class.isAssignableFrom( cls ) ) {
					if ( attributeConverterDefinitions == null ) {
						attributeConverterDefinitions = new ArrayList<>();
					}
					attributeConverterDefinitions.add( AttributeConverterDefinition.from( (Class<? extends AttributeConverter>) cls ) );
				}
				else {
					metadataSources.addAnnotatedClass( cls );
				}
			}
		}

		// add any explicit hbm.xml references passed in
		final String explicitHbmXmls = (String) configurationValues.remove( AvailableSettings.HBXML_FILES );
		if ( explicitHbmXmls != null ) {
			for ( String hbmXml : StringHelper.split( ", ", explicitHbmXmls ) ) {
				metadataSources.addResource( hbmXml );
			}
		}

		// add any explicit orm.xml references passed in
		final List<String> explicitOrmXmlList = (List<String>) configurationValues.remove( AvailableSettings.XML_FILE_NAMES );
		if ( explicitOrmXmlList != null ) {
			explicitOrmXmlList.forEach( metadataSources::addResource );
		}

		return attributeConverterDefinitions;
	}
 
Example 6
Source File: IncrementGenerator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
	returnClass = type.getReturnedClass();

	final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService( JdbcEnvironment.class );
	final ObjectNameNormalizer normalizer =
			(ObjectNameNormalizer) params.get( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER );

	String column = params.getProperty( "column" );
	if ( column == null ) {
		column = params.getProperty( PersistentIdentifierGenerator.PK );
	}
	column = normalizer.normalizeIdentifierQuoting( column ).render( jdbcEnvironment.getDialect() );

	String tableList = params.getProperty( "tables" );
	if ( tableList == null ) {
		tableList = params.getProperty( PersistentIdentifierGenerator.TABLES );
	}
	String[] tables = StringHelper.split( ", ", tableList );

	final String schema = normalizer.toDatabaseIdentifierText(
			params.getProperty( PersistentIdentifierGenerator.SCHEMA )
	);
	final String catalog = normalizer.toDatabaseIdentifierText(
			params.getProperty( PersistentIdentifierGenerator.CATALOG )
	);

	StringBuilder buf = new StringBuilder();
	for ( int i = 0; i < tables.length; i++ ) {
		final String tableName = normalizer.toDatabaseIdentifierText( tables[i] );
		if ( tables.length > 1 ) {
			buf.append( "select max(" ).append( column ).append( ") as mx from " );
		}
		buf.append( Table.qualify( catalog, schema, tableName ) );
		if ( i < tables.length - 1 ) {
			buf.append( " union " );
		}
	}
	if ( tables.length > 1 ) {
		buf.insert( 0, "( " ).append( " ) ids_" );
		column = "ids_.mx";
	}

	sql = "select max(" + column + ") from " + buf.toString();
}
 
Example 7
Source File: QuerySplitter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Handle Hibernate "implicit" polymorphism, by translating the query string into
 * several "concrete" queries against mapped classes.
 */
public static String[] concreteQueries(String query, SessionFactoryImplementor factory) throws MappingException {

	//scan the query string for class names appearing in the from clause and replace
	//with all persistent implementors of the class/interface, returning multiple
	//query strings (make sure we don't pick up a class in the select clause!)

	//TODO: this is one of the ugliest and most fragile pieces of code in Hibernate....

	String[] tokens = StringHelper.split( StringHelper.WHITESPACE + "(),", query, true );
	if ( tokens.length == 0 ) {
		// just especially for the trivial collection filter
		return new String[] { query };
	}
	ArrayList<String> placeholders = new ArrayList<String>();
	ArrayList<String[]> replacements = new ArrayList<String[]>();
	StringBuilder templateQuery = new StringBuilder( 40 );

	int start = getStartingPositionFor( tokens, templateQuery );
	int count = 0;
	String next;
	String last = tokens[start - 1].toLowerCase(Locale.ROOT);

	boolean inQuote = false;

	for ( int i = start; i < tokens.length; i++ ) {

		String token = tokens[i];

		if ( ParserHelper.isWhitespace( token ) ) {
			templateQuery.append( token );
			continue;
		}
		else if ( isQuoteCharacter( token) ) {
			inQuote = !inQuote;
			templateQuery.append( token );
			continue;
		}
		else if ( isTokenStartWithAQuoteCharacter( token ) ) {
			if ( !isTokenEndWithAQuoteCharacter( token ) ) {
				inQuote = true;
			}
			templateQuery.append( token );
			continue;
		}
		else if ( isTokenEndWithAQuoteCharacter( token ) ) {
			inQuote = false;
			templateQuery.append( token );
			continue;
		}
		else if ( inQuote ) {
			templateQuery.append( token );
			continue;
		}
		next = nextNonWhite( tokens, i ).toLowerCase(Locale.ROOT);

		boolean process = isJavaIdentifier( token )
				&& isPossiblyClassName( last, next );

		last = token.toLowerCase(Locale.ROOT);

		if ( process ) {
			String importedClassName = getImportedClass( token, factory );
			if ( importedClassName != null ) {
				String[] implementors = factory.getImplementors( importedClassName );
				token = "$clazz" + count++ + "$";
				if ( implementors != null ) {
					placeholders.add( token );
					replacements.add( implementors );
				}
			}
		}

		templateQuery.append( token );

	}
	String[] results = StringHelper.multiply(
			templateQuery.toString(),
			placeholders.iterator(),
			replacements.iterator()
	);
	if ( results.length == 0 ) {
		LOG.noPersistentClassesFound( query );
	}
	return results;
}