All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.tool.hbm2x.pojo.AnnotationBuilder Maven / Gradle / Ivy

There is a newer version: 5.6.15.Final
Show newest version
package org.hibernate.tool.hbm2x.pojo;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;

public class AnnotationBuilder {

	String annotation;
	LinkedHashMap attributes = new LinkedHashMap(); 
	
	public static AnnotationBuilder createAnnotation(String annotation) {
		return new AnnotationBuilder(annotation);
	}
	
	protected AnnotationBuilder(String annotation) {
		this.annotation = annotation;
	}

	public AnnotationBuilder addAttribute(String name, String[] values) {
		if(values!=null && values.length > 0) {				
			attributes.put(name, values);
		}
		return this;
	}

	public AnnotationBuilder addAttribute(String name, String value) {
		if(value!=null) {
			addAttribute(name, new String[] { value });
		}
		return this;
	}
	
	
	public AnnotationBuilder resetAnnotation(String annotationName) {
		this.annotation = annotationName;
		clearAttributes();
		return this;
	}
	
	private AnnotationBuilder clearAttributes() {
		attributes.clear();
		return this;
	}
	
	public String getResult() {
		StringBuffer b = new StringBuffer("@");
		b.append(annotation);
		if(attributes.isEmpty()) {
			return b.toString();
		} else {
			b.append("(");
			Iterator> elements = attributes.entrySet().iterator();
			boolean addedBefore = false;
			while ( elements.hasNext() ) {
				Entry element = elements.next();

				String[] s = element.getValue();
				if(s.length==0) {
					addedBefore = false;
					continue;
				} else {
					if(addedBefore) {
						b.append(", ");
					}
					String key = element.getKey();
					b.append(key).append("=");
					attributeToString( b, s );
					
					addedBefore=true;
				}
			}
			b.append( ")" );
		}
		return b.toString();
	}

	private void attributeToString(StringBuffer buffer, String[] values) {
		if(values.length>1) {
			buffer.append( "{" );
		}
		
		for (int i = 0; i < values.length; i++) {
			buffer.append(values[i]);
			if(i1) {
			buffer.append( "}" );
		}
		
	}

	public void addQuotedAttributes(String name, Iterator iterator) {
		List values = new ArrayList();
		while ( iterator.hasNext() ) {
			values.add(quote( iterator.next().toString() ));
		}
		addAttribute(name, values.toArray( new String[values.size()] ));
	}

	public void addAttributes(String name, Iterator iterator) {
		List values = new ArrayList();
		while ( iterator.hasNext() ) {
			String element = iterator.next().toString();
			values.add( element );
		}
		addAttribute(name, values.toArray( new String[values.size()] ));		
	}
	private String quote(String element) {
		return "\"" + element + "\"";
	}

	public AnnotationBuilder addQuotedAttribute(String name, String value) {
		if(value!=null) {
			addAttribute(name, quote(value));
		}
		return this;
	}
	

	public String toString() {
		return getResult();
	}

	public String getAttributeAsString(String name) {
		StringBuffer buffer = new StringBuffer();
		String[] object = (String[]) attributes.get( name );
		if(object==null) {
			return null;
		} else {
			attributeToString( buffer, object );
			return buffer.toString();
		}
	}

	
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy