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

AtomParserUtils

public class AtomParserUtils extends Object
author
Simon Willnauer

Fields Summary
private static final Pattern
ATOM_MEDIA_TYPE_PATTERN
Constructors Summary
Methods Summary
public static booleancompareAlternateLinks(org.apache.lucene.gdata.gom.GOMLink left, org.apache.lucene.gdata.gom.GOMLink right)
Compares two links with rel attribute "alternate" Checks if href and type are equal

param
left - left link to compare
param
right - right link to compare
return
true if and only if href and type are equal, otherwise false

		if ((left.getType() == null) ^ right.getType() == null
				|| (left.getType() == null && right.getType() == null)) {
			return false;
		} else {
			if (!left.getType().equalsIgnoreCase(right.getType()))
				return false;
		}

		if (((left.getHrefLang() == null) ^ right.getHrefLang() == null)
				|| (left.getHrefLang() == null && right.getHrefLang() == null)) {
			return false;
		} else {
			if (!left.getHrefLang().equalsIgnoreCase(right.getHrefLang()))
				return false;
		}
		return true;

	
public static java.lang.StringescapeXMLCharacter(java.lang.String aString)
Replaces all xml character with the corresponding entity.
  • <!ENTITY lt "&#38;#60;">
  • <!ENTITY gt "&#62;">
  • <!ENTITY amp "&#38;#38;">
  • <!ENTITY apos "&#39;">
  • <!ENTITY quot "&#34;">
see W3C specification

param
aString - a string may container xml characters like '<'
return
the input string with escaped xml characters


	                                                              	 
	     
		StringBuilder builder = new StringBuilder();
		char[] cs = aString.toCharArray();
		for (int i = 0; i < cs.length; i++) {
			switch (cs[i]) {
			case '<":
				builder.append("<");
				break;
			case '>":
				builder.append(">");
				break;
			case '"":
				builder.append(""");
				break;
			case '\'":
				builder.append("'");
				break;
			case '&":
				builder.append("&");
				break;
			case '\0":
				// this breaks some xml serializer like soap serializer -->
				// remove it
				break;
			default:
				builder.append(cs[i]);
			}
		}

		return builder.toString();

	
public static java.lang.StringgetAbsolutAtomURI(java.lang.String xmlBase, java.lang.String atomUri)

param
xmlBase
param
atomUri
return
throws
URISyntaxException

		if (atomUri == null)
			throw new IllegalArgumentException("atomUri must not be null");
		if (atomUri.startsWith("www."))
			atomUri = "http://" + atomUri;
		URI aUri = new URI(atomUri);

		if (xmlBase == null || xmlBase.length() == 0) {
			if (!aUri.isAbsolute()) {
				throw new URISyntaxException(atomUri,
						" -- no xml:base specified atom uri must be an absolute url");
			}
		}

		return atomUri;
	
public static org.apache.lucene.gdata.gom.AtomMediaTypegetAtomMediaType(java.lang.String aMediaType)

param
aMediaType
return

		if (aMediaType == null || !isAtomMediaType(aMediaType))
			throw new IllegalArgumentException(
					"aMediaType must be a media type and  not be null ");
		if (aMediaType.endsWith("+xml") || aMediaType.endsWith("/xml"))
			return AtomMediaType.XML;
		if (aMediaType.startsWith("text/"))
			return AtomMediaType.TEXT;
		return AtomMediaType.BINARY;
	
public static booleanisAtomMediaType(java.lang.String aMediaType)

param
aMediaType
return

		return (aMediaType == null || aMediaType.length() < 3) ? false
				: ATOM_MEDIA_TYPE_PATTERN.matcher(aMediaType).matches();
	
public static voidmain(java.lang.String[] args)

		// String s = new String(
		// "<!ENTITY lt \"&#60;\"><!ENTITY gt \">\"><!ENTITY amp
		// \"&#38;\"><!ENTITY apos \"'\"><!ENTITY quot \""\">");
		// System.out.println(escapeXMLCharacter(s));
		//		
		System.out.println(isAtomMediaType("t/h"));