
java.fedora.server.validation.DOValidatorSchematronResult Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fcrepo-client Show documentation
Show all versions of fcrepo-client Show documentation
The Fedora Client is a Java Library that allows API access to a Fedora Repository. The client is typically one part of a full Fedora installation.
The newest version!
/*
* -----------------------------------------------------------------------------
*
* License and Copyright: The contents of this file are subject to the
* Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
* http://www.fedora-commons.org/licenses.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The entire file consists of original code.
* Copyright © 2008 Fedora Commons, Inc.
*
Copyright © 2002-2007 The Rector and Visitors of the University of
* Virginia and Cornell University
* All rights reserved.
*
* -----------------------------------------------------------------------------
*/
package fedora.server.validation;
import java.io.Writer;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.util.Properties;
// DOM classes
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.apache.log4j.Logger;
// TrAX classes
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.*;
import javax.xml.parsers.ParserConfigurationException;
/**
* Schematron validation with FedoraRules schema as default.
*
* @author [email protected]
* @version $Id: DOValidatorSchematronResult.java 5218 2006-11-20 05:10:11Z cwilper $
*/
public class DOValidatorSchematronResult
{
/** Logger for this class. */
private static final Logger LOG = Logger.getLogger(
DOValidatorSchematronResult.class.getName());
private StringBuffer string = new StringBuffer();
private Element rootElement;
public DOValidatorSchematronResult(DOMResult result)
{
rootElement = (Element)result.getNode().getFirstChild();
}
public String getXMLResult()
throws TransformerException,
TransformerConfigurationException,
ParserConfigurationException
{
Writer w = new StringWriter();
PrintWriter out = new PrintWriter(w);
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer();
Properties transProps = new Properties();
transProps.put("method", "xml");
transProps.put("indent", "yes");
transformer.setOutputProperties(transProps);
transformer.transform(new DOMSource(rootElement), new StreamResult(out));
out.close();
return w.toString();
}
/**
* Check if the object passes Schematron validation
*
* @return true
, object is valid,
* false
object had errors.
*/
public boolean isValid()
{
if ((rootElement.getElementsByTagName("ASSERT").getLength() == 0) &&
(rootElement.getElementsByTagName("REPORT").getLength() == 0) )
return true;
else
return false;
}
/** Serializes the specified node, recursively, to a Writer
* and returns it as a String too.
*/
public String serializeResult(Writer out)
{
return(serializeNode(rootElement, out));
}
private String serializeNode(Node node, Writer out)
{
try
{
if ( node == null )
{
return null;
}
int type = node.getNodeType();
switch ( type )
{
case Node.DOCUMENT_NODE:
out.write("");
string.append(" \n");
serializeNode(((Document)node).getDocumentElement(), out);
break;
case Node.ELEMENT_NODE:
string.append("<");
string.append(node.getNodeName());
out.write("<");
out.write(node.getNodeName());
// do attributes
NamedNodeMap attrs = node.getAttributes();
for ( int i = 0; i < attrs.getLength(); i++ )
{
string.append(" ");
string.append(attrs.item(i).getNodeName());
string.append("=\"");
string.append(attrs.item(i).getNodeValue());
string.append("\"");
out.write(" ");
out.write(attrs.item(i).getNodeName());
out.write("=\"");
out.write(attrs.item(i).getNodeValue());
out.write("\"");
}
// close up the current element
string.append(">");
out.write(">");
// recursive call to process this node's children
NodeList children = node.getChildNodes();
if ( children != null )
{
int len = children.getLength();
for ( int i = 0; i < len; i++ )
{
serializeNode(children.item(i), out);
}
}
break;
case Node.TEXT_NODE:
string.append(node.getNodeValue());
out.write(node.getNodeValue());
break;
}
if ( type == Node.ELEMENT_NODE )
{
string.append("");
string.append(node.getNodeName());
string.append(">");
out.write("");
out.write(node.getNodeName());
out.write(">");
}
out.flush();
}
catch ( Exception e )
{
LOG.error("Error serializing node", e);
}
return(string.toString());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy