XPATH - [Exception] java.lang.ClassCastException: java.lang.String - (Ajuda)

0 respostas
psampei

Olá galera do Guj, estou de cabelos em pé... mas chega uma hora que não consigo mais enxergar nada.... vou explicar o cenário e oq está acontecendo: estou fazendo uma classe que recebe (in-memory xml + xpath) para pegar a informação necessária do xml: em código fica assim:

CallFotMod10Factory:

protected void callFotMod10Factory(XQPart prt) throws IOException, ParserConfigurationException, SAXException, TransformerException{
		FotMod10Factory mod10 = new FotMod10Factory();
		ByteArrayInputStream xmlStream = new ByteArrayInputStream((byte[])prt.getContent());
		InputStreamDataSource isds  = new InputStreamDataSource("currentContent.xml",prt.getContentType(),xmlStream);		
		mod10.AppliesXPath(isds, m_xpath);
	}

Essa classe é responsável por gerar transformar o XML (prt.getContent()) que está em memória passando a um pouco da responsabilidade para outra class ByteArrayInputStream, mais o XPath (//book/title/text()) que pega as Informação do xml.

Class ByteArrayInputStream:

public class InputStreamDataSource implements DataSource {

	private String name;
	private String contentType;
	private ByteArrayOutputStream baos;

	public InputStreamDataSource(String name, String contentType,
			InputStream inputStream) throws IOException {
		int read;
		this.name = name;
		this.contentType = contentType;

		baos = new ByteArrayOutputStream();

		byte[] buff = new byte[256];
		while ((read = inputStream.read(buff)) != -1) {
			baos.write(buff, 0, read);
		}
	}

	public String getContentType() {
		// TODO Auto-generated method stub
		return contentType;
	}

	public InputStream getInputStream() throws IOException {
		// TODO Auto-generated method stub
		return new ByteArrayInputStream(baos.toByteArray());
	}

	public String getName() {
		// TODO Auto-generated method stub
		return name;
	}

	public OutputStream getOutputStream() throws IOException {
		// TODO Auto-generated method stub
		throw new IOException("Cannot write to this read-only resource");
	}

}

Chamado a AppliesXPath(isds, m_xpath), que é onde aplica o xpath passado:

public void AppliesXPath(InputStreamDataSource isds, String m_xpath) throws ParserConfigurationException, FileNotFoundException, SAXException, IOException, TransformerException{
	      DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
	      DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
	      Document doc = docBuilder.parse(isds.getInputStream());
	      doc.getDocumentElement().normalize();
	      Transformer serializer = TransformerFactory.newInstance().newTransformer();
	      serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
	      NodeIterator ni = XPathAPI.selectNodeIterator(doc, m_xpath);
	      Node n;
	      while ((n = ni.nextNode()) != null) {
	        // Serialize the found nodes to System.out
	        serializer.transform(
	            new DOMSource(n),
	            new StreamResult(System.out));
	      }
	}

Assim que processado esse method, então ele mostra os elements (/text()) do node pelo critério do xpath (m_xpath).

Acontece que quando executo essa interface, me gera uma exception no log, que está me confundindo todo:
log:

RuntimeParameter: //book/title/text()
java.lang.ClassCastException: java.lang.String
at br.com.customer.FotMod10.callFotMod10Factory(FotMod10.java:252)
at br.com.customer.FotMod10.service(FotMod10.java:112)
at com.sonicsw.xqimpl.service.debug.DebugServiceInterceptor.intercept(DebugServiceInterceptor.java:118)
at com.sonicsw.xqimpl.service.XQServiceChain$XQInterceptorServiceWrapper.intercept(XQServiceChain.java:481)
at com.sonicsw.xqimpl.service.XQServiceChain$XQInterceptorServiceWrapper.service(XQServiceChain.java:470)
at com.sonicsw.xqimpl.service.XQServiceChain.service(XQServiceChain.java:151)
at com.sonicsw.xqimpl.service.ServiceMessageHandler.callService(ServiceMessageHandler.java:429)
at com.sonicsw.xqimpl.service.ServiceMessageHandler.handleMessage(ServiceMessageHandler.java:190)
at com.sonicsw.xqimpl.service.ProcessMessageHandler.doHandleMessage(ProcessMessageHandler.java:317)
at com.sonicsw.xqimpl.service.ProcessMessageHandler.handleMessage(ProcessMessageHandler.java:94)
at com.sonicsw.xqimpl.service.XQDispatcher.onMessage(XQDispatcher.java:422)
at com.sonicsw.xqimpl.endpoint.container.EndpointContextContainer.onMessage(EndpointContextContainer.java:90)
at com.sonicsw.xq.connector.jms.JMSEndpoint$JMSEndpointListener.onMessage(JMSEndpoint.java:575)
at progress.message.jimpl.Session.deliver(Session.java:3006)
at progress.message.jimpl.Session.run(Session.java:2397)
at progress.message.jimpl.Session$SessionThread.run(Session.java:2782)

em detalhe com a informação:

at br.com.customer.FotMod10.callFotMod10Factory(FotMod10.java:252)
at br.com.customer.FotMod10.service(FotMod10.java:112)

onde no log aponta para 252: que é a chamada o CallFotMod10Factory, na linha:

ByteArrayInputStream xmlStream = new ByteArrayInputStream((byte[])prt.getContent());

e na 112: que é a chamada ao method callFotMod10Factory.

agora pq está dando este erro java.lang.ClassCastException: java.lang.String ???

Qualquer ajuda será bem vinda.

abs,

Sampei

Criado 15 de julho de 2009
Respostas 0
Participantes 1