Here is a class I have been using to manage the mappings in my applications.
It has some convenience methods so you don't have to repeat your self all
the time when you need to get an InputSource. Thought I would share it with
you in light of the current thread. All the methods are static and if you
addMapping(String s) it will clear out your mappingPath so you should all go
and put in one for a default or set it each time.
/*
* Created on Feb 12, 2005
*
*/
package org.xoscript.mapping;
import java.io.IOException;
import java.util.Iterator;
import java.util.TreeSet;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.mapping.xml.Include;
import org.exolab.castor.mapping.xml.MappingRoot;
import org.xml.sax.InputSource;
/**
* @author [EMAIL PROTECTED]
*
*/
public class MappingFactory {
private static Mapping mapping = null;
private static String mappingPath = "/default/path/to/mapping.xml";
private static TreeSet mappings = new TreeSet();
public static Mapping getMapping(ClassLoader cl) throws MappingException,
IOException{
if(mapping != null){
return mapping;
}else{
if(mappingPath.equalsIgnoreCase("") || mappingPath.equals(null)){
throw new MappingException("The mappingPath may not be empty or null");
}
mapping = new Mapping();
mapping.loadMapping(new
InputSource(cl.getResourceAsStream(getMappingPath())));
if(mappings.size() > 0){
MappingRoot root = mapping.getRoot();
Iterator i = mappings.iterator();
while (i.hasNext()) {
String url = (String) i.next();
Include inc = new Include();
inc.setHref(url);
root.addInclude(inc);
}
}
return mapping;
}
}
public static Mapping getMapping(Object o) throws MappingException,
IOException{
return getMapping(o.getClass().getClassLoader());
}
public static Mapping getMapping(Class c) throws MappingException,
IOException{
return getMapping(c.getClassLoader());
}
public static String getMappingPath() {
return mappingPath;
}
public static void setMappingPath(String path) {
mappingPath = path;
mapping = null;
}
public static void addMapping(String url){
mappings.add(url);
mapping = null;
}
public static void removeMapping(String url){
mappings.remove(url);
mapping = null;
}
public static TreeSet getMappings() {
return mappings;
}
}
----- Original Message -----
From: "Stephen Bash" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Thursday, February 17, 2005 8:00 AM
Subject: Re: [castor-user] Resolving Mapping file location
>
> Keith and Werner-
>
> Okay, as of this morning, I'm not sure it does apply to 0.9.6... It
> appears my classpath was more confused than I thought... This morning I
> got an exception about the commons logging package which I hadn't had
> before (which I assume means the system was finally looking at 0.9.6
> instead of 0.9.5.4). I'll do some more playing to see if I can break it
> again now that the system is using the right jar. Thanks for the help.
>
> On a side note, I am running on XP, I just used forward slashes in my
> properties file to avoid problems with backlash substitution... But
> I'll test both forms today when I get a chance.
>
> Thanks again!
> Stephen
>
>
> Keith Visco wrote:
> >
> > Hi Stephen,
> >
> > I'm not sure I'd classify it as a feature! :-)
> >
> > This issue was actually reported a couple months back and I thought we
> > had it all cleared up for the 0.9.6 release. I was not able to reproduce
> > the problem on my Win XP box. So I'm guessing our logic for dealing with
> > paths still has a problem under Unix/Linux platforms.
> >
> > Can you open up a bug on this for tracking purposes. I currently don't
> > have access to a linux machine, but I'll try and track down the problem
> > or have Bruce look into it.
> >
> > Thanks,
> >
> > --Keith
> >
> > Stephen Bash wrote:
> >
> >>
> >>
> >> This may be a "bug or feature" question... I tried load an XML
> >> mapping file from a full pathname, and got a FileNotFoundException...
> >> If I wrap the filename inside of an InputSource, it works... The FNFE
> >> is correct in that the FNFE reports the filename as
> >> (full-path)/(full-path)/filename.
> >>
> >> I traced through the code, and the line throwing the exception is 257
> >> of DTDResolver if that helps at all.
> >>
> >> Is this a case where loadMapping( String ) should just be discouraged
> >> in favor of loadMapping( InputSource ), or is there some greater issue
> >> here?
> >>
> >> Thanks for any help!
> >>
> >> Stephen
> >>
> >>
> >>
> >> ----------------------------------------------------------- If you
> >> wish to unsubscribe from this mailing, send mail to
> >> [EMAIL PROTECTED] with a subject of:
> >> unsubscribe castor-user
> >>
> >>
> >
> >
> >
> > ----------------------------------------------------------- If you wish
> > to unsubscribe from this mailing, send mail to
> > [EMAIL PROTECTED] with a subject of:
> > unsubscribe castor-user
> >
> >
>
>
>
> -----------------------------------------------------------
> If you wish to unsubscribe from this mailing, send mail to
> [EMAIL PROTECTED] with a subject of:
> unsubscribe castor-user
-----------------------------------------------------------
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
unsubscribe castor-user