473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Is there an EntityResolver equivalent for xsd imports?

I'm using java 1.4 and xerces and I'm having problems validating XML.
I want to be able to override XSD retrieval (eventually, they will be
in a JAR file). This works fine when the XSD I'm validating doesn't
"import" other XSDs - I just assign a custom entity resolver. But if
those XSDs then import other XSDs, the entity resolver isn't called
for the imported XSD. Xerces tries to resolve the schemaLocation as a
URL, without letting my code redirect the reference.

Is there a solution to this problem?

This is the code I'm using:
================================================== =
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;

import java.util.LinkedList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import java.util.Iterator;

public class validate
{
static public class ResolverImpl implements EntityResolver
{
public InputSource resolveEntity(String publicId, String
systemId)
throws SAXException, IOException
{
System.out.println("resolving " + systemId + " : " +
publicId);
System.out.flush();
return new InputSource(new FileInputStream(
"c:\\somedirectory\\" + systemId));
}
}
static class ErrorHandlerImpl implements ErrorHandler
{
List errors = new LinkedList();

public void fatalError(SAXParseException e)
{
errors.add(e);
}

public void warning(SAXParseException e)
{
}

public void error(SAXParseException e)
{
errors.add(e);
}

boolean hasErrors()
{
if (errors.size() > 0)
return true;
else
return false;
}

void printErrors()
{
Iterator it = errors.iterator();
while (it.hasNext())
System.out.println(it.next());
}
}

static void validateContents(byte[] contents)
{
try
{
ErrorHandlerImpl handler = new ErrorHandlerImpl();

ByteArrayInputStream stream = new
ByteArrayInputStream(contents);
DocumentBuilderFactory docBuilderFactory
= DocumentBuilderFactory.newInstance();
docBuilderFactory.setValidating(true);
docBuilderFactory.setNamespaceAware(true);

DocumentBuilder docBuilder =
docBuilderFactory.newDocumentBuilder();
docBuilder.setErrorHandler(handler);
docBuilder.setEntityResolver(new ResolverImpl());
Document document = docBuilder.parse(stream);
Element rootElement = document.getDocumentElement();

System.out.println("errors = " + handler.hasErrors());
handler.printErrors();
}
catch (ParserConfigurationException e)
{
System.out.println("parse error:" + e);
}
catch (SAXException e)
{
System.out.println("parse error:" + e);
}
catch (IOException e)
{
System.out.println("parse error:" + e);
}
}

public static void main (String[] args)
{
try
{
validateContents(Util.readFile(args[0]));
}
catch (Exception e)
{
System.out.println("exception:" + e);
}
}
}
Jul 20 '05 #1
0 4267

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Thomas Scheffler | last post by:
Hi, I use the Xerces SAXParser to read my documents in Java. For that I wrote an EntityResolver so that the parse can use a schema to validate them. Now that is the problem. The parser does...
1
by: Steven K | last post by:
Hello, Is there a way to declare imports so that they don't have to be declared both in Master Page.vb and the Content Page.vb? For example, I have the following in the Master Page.vb Imports...
14
by: John | last post by:
Hi What is the vb.net equivalent of; y = Right(x, Len(x) - x.LastIndexOf("\")) Thanks Regards
12
by: vvv | last post by:
Hi All, Do we have anything in .NET which is equivalent to C++'s Typedef . Regards, Vasanth
3
by: chris | last post by:
Hello Group, All I wanna do is to get the contents of a web page and display it in my application. VB 6.0 has the INet control. How do I do it using VB.NET/ Thanks for the help, Chris.
4
by: Jim | last post by:
I am writing a Windows Service in VB.Net, and right upfront I need to pass the path to my config.xml file to the constructor of the class which reads it. In the old days, App.Path & "\config.xml"...
1
by: Thomas Wittek | last post by:
Hi! Is there any possibility/tool to automatically organize the imports at the beginning of a module? I don't mean automatic imports like autoimp does as I like seeing where my...
5
by: kimiraikkonen | last post by:
Hello, I want to ask about "imports" statement. Some projects must be inserted with "imports xxxx" statements before beginning coding. But how do i know when to use or do i have to use "imports"...
3
by: Mohamed Yousef | last post by:
Hello , The problem I'm asking about is how can imported modules be aware of other imported modules so they don't have to re-import them (avoiding importing problems and Consicing code and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.