String uri,
Map parameters,
Logger logger)
throws MalformedURLException {
Environment env = CocoonComponentManager.getCurrentEnvironment();
if ( env == null ) {
throw new MalformedURLException("The cocoon protocol can not be used outside an environment.");
}
this.manager = manager;
this.enableLogging(logger);
boolean rawMode = false;
// remove the protocol
int position = uri.indexOf(':') + 1;
if (position != 0) {
this.protocol = uri.substring(0, position-1);
// check for subprotocol
if (uri.startsWith("raw:", position)) {
position += 4;
rawMode = true;
}
} else {
throw new MalformedURLException("No protocol found for sitemap source in " + uri);
}
// does the uri point to this sitemap or to the root sitemap?
String prefix;
if (uri.startsWith("//", position)) {
position += 2;
this.processor = CocoonComponentManager.getActiveProcessor(env).getRootProcessor();
prefix = ""; // start at the root
} else if (uri.startsWith("/", position)) {
position ++;
prefix = null;
this.processor = CocoonComponentManager.getActiveProcessor(env);
} else {
throw new MalformedURLException("Malformed cocoon URI: " + uri);
}
// create the queryString (if available)
String queryString = null;
int queryStringPos = uri.indexOf('?', position);
if (queryStringPos != -1) {
queryString = uri.substring(queryStringPos + 1);
uri = uri.substring(position, queryStringPos);
} else if (position > 0) {
uri = uri.substring(position);
}
// determine if the queryString specifies a cocoon-view
String view = null;
if (queryString != null) {
int index = queryString.indexOf(Constants.VIEW_PARAM);
if (index != -1
&& (index == 0 || queryString.charAt(index-1) == '&')
&& queryString.length() > index + Constants.VIEW_PARAM.length()
&& queryString.charAt(index+Constants.VIEW_PARAM.length()) == '=') {
String tmp = queryString.substring(index+Constants.VIEW_PARAM.length()+1);
index = tmp.indexOf('&');
if (index != -1) {
view = tmp.substring(0,index);
} else {
view = tmp;
}
} else {
view = env.getView();
}
} else {
view = env.getView();
}
// build the request uri which is relative to the context
String requestURI = (prefix == null ? env.getURIPrefix() + uri : uri);
// create system ID
this.systemId = queryString == null ?
this.protocol + "://" + requestURI :
this.protocol + "://" + requestURI + "?" + queryString;