FileDocCategorySizeDatePackage
GOMUtils.javaAPI DocApache Lucene 2.1.011027Wed Feb 14 10:45:58 GMT 2007org.apache.lucene.gdata.gom.core.utils

GOMUtils

public class GOMUtils extends Object
author
Simon Willnauer

Fields Summary
private static final Pattern
RFC3339_DATE_PATTERN
static final String
ATTRIBUTE_TYPE
static final org.apache.lucene.gdata.gom.GOMAttribute
TEXT_TYPE
static final org.apache.lucene.gdata.gom.GOMAttribute
HTML_TYPE
static final org.apache.lucene.gdata.gom.GOMAttribute
XHTML_TYPE
static final org.apache.lucene.gdata.gom.GOMAttribute
TEXT_TYPE_DEFAULT_NS
static final org.apache.lucene.gdata.gom.GOMAttribute
HTML_TYPE_DEFAULT_NS
static final org.apache.lucene.gdata.gom.GOMAttribute
XHTML_TYPE_DEFAULT_NS
Constructors Summary
Methods Summary
public static org.apache.lucene.gdata.gom.GOMAttributebuildAtomAttribute(java.lang.String aValue, java.lang.String aName)
Builds a atom namespace attribute

param
aValue attribute value
param
aName attribute name
return
a GOMAttribute

		return new GOMAttributeImpl(GOMNamespace.ATOM_NS_URI,
				GOMNamespace.ATOM_NS_PREFIX, aName, aValue);
	
public static org.apache.lucene.gdata.gom.GOMAttributebuildDefaultNamespaceAttribute(java.lang.String aValue, java.lang.String aName)

param
aValue
param
aName
return

		return new GOMAttributeImpl(aName, aValue);
	
public static java.lang.StringbuildRfc3339DateFormat(long aMillisecondLong)

param
aMillisecondLong
return

		Calendar instance = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
		instance.setTimeInMillis(aMillisecondLong);

		StringBuilder builder = new StringBuilder();
		// 2003-12-13T18:30:02.25+01:00
		int time = 0;
		time = instance.get(Calendar.YEAR);
		if (time < 1000)
			builder.append("0");
		if (time < 100)
			builder.append("0");
		if (time < 10)
			builder.append("0");
		builder.append(time);
		builder.append('-");
		time = instance.get(Calendar.MONTH);
		if (time < 10)
			builder.append(0);
		builder.append(time);
		builder.append('-");
		time = instance.get(Calendar.DAY_OF_MONTH);
		if (time < 10)
			builder.append(0);
		builder.append(time);
		builder.append('T");
		time = instance.get(Calendar.HOUR_OF_DAY);
		if (time < 10)
			builder.append(0);
		builder.append(time);
		builder.append(':");
		time = instance.get(Calendar.MINUTE);
		if (time < 10)
			builder.append(0);
		builder.append(time);
		builder.append(':");
		time = instance.get(Calendar.SECOND);
		if (time < 10)
			builder.append(0);
		builder.append(time);
		builder.append('.");
		builder.append(instance.get(Calendar.MILLISECOND));
		// this is always GMT offset -> 0
		builder.append('Z");

		return builder.toString();
	
public static java.lang.StringbuildRfc822Date(long aMillisecondLong)

param
aMillisecondLong
return

		/*
		 * Rather implement it for a special case as use SDF. SDF is very
		 * expensive to create and not thread safe so it should be synchronized
		 * of pooled
		 */
		Calendar instance = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
		instance.setTimeInMillis(aMillisecondLong);

		StringBuilder builder = new StringBuilder();
		// Sun, 06 Aug 2006 00:53:49 +0000
		// EEE, dd MMM yyyy HH:mm:ss Z

		switch (instance.get(Calendar.DAY_OF_WEEK)) {
		case Calendar.SUNDAY:
			builder.append("Sun");
			break;
		case Calendar.MONDAY:
			builder.append("Mon");
			break;
		case Calendar.TUESDAY:
			builder.append("Tue");
			break;
		case Calendar.WEDNESDAY:
			builder.append("Wed");
			break;
		case Calendar.THURSDAY:
			builder.append("Thu");
			break;
		case Calendar.FRIDAY:
			builder.append("Fri");
			break;
		case Calendar.SATURDAY:
			builder.append("Sat");
			break;
		default:
			break;
		}
		builder.append(',");
		builder.append(' ");

		int time = 0;
		time = instance.get(Calendar.DAY_OF_MONTH);
		if (time < 10)
			builder.append(0);
		builder.append(time);
		builder.append(' ");
		switch (instance.get(Calendar.MONTH)) {
		case Calendar.JANUARY:
			builder.append("Jan");
			break;
		case Calendar.FEBRUARY:
			builder.append("Feb");
			break;
		case Calendar.MARCH:
			builder.append("Mar");
			break;
		case Calendar.APRIL:
			builder.append("Apr");
			break;
		case Calendar.MAY:
			builder.append("May");
			break;
		case Calendar.JUNE:
			builder.append("Jun");
			break;
		case Calendar.JULY:
			builder.append("Jul");
			break;
		case Calendar.AUGUST:
			builder.append("Aug");
			break;
		case Calendar.SEPTEMBER:
			builder.append("Sep");
			break;
		case Calendar.OCTOBER:
			builder.append("Oct");
			break;
		case Calendar.NOVEMBER:
			builder.append("Nov");
			break;
		case Calendar.DECEMBER:
			builder.append("Dec");
			break;

		default:
			break;
		}
		builder.append(' ");
		time = instance.get(Calendar.YEAR);
		if (time < 1000)
			builder.append("0");
		if (time < 100)
			builder.append("0");
		if (time < 10)
			builder.append("0");
		builder.append(time);
		builder.append(' ");

		time = instance.get(Calendar.HOUR_OF_DAY);
		if (time < 10)
			builder.append(0);
		builder.append(time);
		builder.append(':");
		time = instance.get(Calendar.MINUTE);
		if (time < 10)
			builder.append(0);
		builder.append(time);
		builder.append(':");
		time = instance.get(Calendar.SECOND);
		if (time < 10)
			builder.append(0);
		builder.append(time);

		// this is always GMT offset -> 0
		builder.append(" +0000");
		return builder.toString();
	
public static org.apache.lucene.gdata.gom.GOMAttributebuildXMLNamespaceAttribute(java.lang.String aValue, java.lang.String aName)

param
aValue
param
aName
return

		return new GOMAttributeImpl(GOMNamespace.XML_NS_URI,
				GOMNamespace.XML_NS_PREFIX, aName, aValue);
	
public static org.apache.lucene.gdata.gom.GOMAttributegetAttributeByContentType(org.apache.lucene.gdata.gom.ContentType type)

param
type
return


	 
		TEXT_TYPE = new GOMAttributeImpl(GOMNamespace.ATOM_NS_URI,
				GOMNamespace.ATOM_NS_PREFIX, ATTRIBUTE_TYPE, ContentType.TEXT
						.name().toLowerCase());
		HTML_TYPE = new GOMAttributeImpl(GOMNamespace.ATOM_NS_URI,
				GOMNamespace.ATOM_NS_PREFIX, ATTRIBUTE_TYPE, ContentType.HTML
						.name().toLowerCase());
		XHTML_TYPE = new GOMAttributeImpl(GOMNamespace.ATOM_NS_URI,
				GOMNamespace.ATOM_NS_PREFIX, ATTRIBUTE_TYPE, ContentType.XHTML
						.name().toLowerCase());

		TEXT_TYPE_DEFAULT_NS = new GOMAttributeImpl(ATTRIBUTE_TYPE,
				ContentType.TEXT.name().toLowerCase());
		HTML_TYPE_DEFAULT_NS = new GOMAttributeImpl(ATTRIBUTE_TYPE,
				ContentType.HTML.name().toLowerCase());
		XHTML_TYPE_DEFAULT_NS = new GOMAttributeImpl(ATTRIBUTE_TYPE,
				ContentType.XHTML.name().toLowerCase());

	
		switch (type) {
		case HTML:
			return HTML_TYPE;
		case XHTML:
			return XHTML_TYPE;

		default:
			return TEXT_TYPE;
		}

	
public static org.apache.lucene.gdata.gom.GOMAttributegetAttributeByContentTypeDefaultNs(org.apache.lucene.gdata.gom.ContentType type)

param
type
return

		if (type == null)
			return TEXT_TYPE_DEFAULT_NS;
		switch (type) {
		case HTML:
			return HTML_TYPE_DEFAULT_NS;
		case XHTML:
			return XHTML_TYPE_DEFAULT_NS;

		default:
			return TEXT_TYPE_DEFAULT_NS;
		}

	
public org.apache.lucene.gdata.gom.GOMLinkgetHtmlLink(java.util.List links)

		for (GOMLink link : links) {
			if ((link.getRel() == null || link.getRel().equals("alternate"))
					&& (link.getType() == null || link.getType()
							.equalsIgnoreCase("html")))
				return link;
		}
		return null;
	
public static booleanisRfc3339DateFormat(java.lang.String aString)

param
aString
return

		Matcher aMatcher = RFC3339_DATE_PATTERN.matcher(aString);
		return aMatcher.matches();
	
public static longparseRfc3339DateFormat(java.lang.String aString)

param
aString
return

		if (aString == null)
			throw new IllegalArgumentException(
					"Date-Time String must not be null");
		Matcher aMatcher = RFC3339_DATE_PATTERN.matcher(aString);

		if (!aMatcher.matches()) {
			throw new GDataParseException(
					"Invalid RFC3339 date / time pattern -- " + aString);
		}
		int grCount = aMatcher.groupCount();
		if (grCount > 13)
			throw new GDataParseException(
					"Invalid RFC3339 date / time pattern -- " + aString);

		Integer timeZoneShift = null;
		Calendar dateTime = null;
		try {

			if (aMatcher.group(9) == null) {
				// skip time zone
			} else if (aMatcher.group(9).equalsIgnoreCase("Z")) {
				timeZoneShift = new Integer(0);
			} else {
				timeZoneShift = new Integer((Integer
						.valueOf(aMatcher.group(12)) * 60 + Integer
						.valueOf(aMatcher.group(13))));
				if (aMatcher.group(11).equals("-")) {
					timeZoneShift = new Integer(-timeZoneShift.intValue());
				}
			}

			dateTime = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
			;
			dateTime.clear();
			dateTime.set(Integer.valueOf(aMatcher.group(1)), Integer
					.valueOf(aMatcher.group(2)) - 1, Integer.valueOf(aMatcher
					.group(3)), Integer.valueOf(aMatcher.group(4)), Integer
					.valueOf(aMatcher.group(5)), Integer.valueOf(aMatcher
					.group(6)));
			// seconds with milliseconds
			if (aMatcher.group(8) != null && aMatcher.group(8).length() > 0) {

				dateTime.set(Calendar.MILLISECOND, new BigDecimal("0."/*
																		 * use
																		 * big
																		 * dec
																		 * this
																		 * could
																		 * be
																		 * big!!
																		 */
						+ aMatcher.group(8)).movePointRight(3).intValue());
			}
		} catch (NumberFormatException e) {
			throw new GDataParseException(
					"Invalid RFC3339 date / time pattern -- " + aString, e);
		}

		long retVal = dateTime.getTimeInMillis();
		if (timeZoneShift != null) {
			retVal -= timeZoneShift.intValue() * 60000;
		}

		return retVal;