📄 viewmetadatahelper.java
字号:
package com.esri.solutions.jitk.common.csw;
import com.esri.solutions.jitk.common.metadata.BasicMetadataExporter;
import com.esri.solutions.jitk.common.metadata.IMetadataAware;
import com.esri.solutions.jitk.common.metadata.IMetadataCatalog;
import com.esri.solutions.jitk.common.metadata.IMetadataContext;
import com.esri.solutions.jitk.common.metadata.IMetadataDocument;
import com.esri.solutions.jitk.common.metadata.InvalidMetadataOperationException;
import com.esri.solutions.jitk.common.metadata.MetadataID;
import com.esri.solutions.jitk.common.metadata.csw.CSWMetadataProfile;
import org.apache.log4j.Logger;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
/**
* Helper class that implements the {@link IMetadataAware} interface. This class
* is responsible for taking a CS/W metadata document and transforming it into
* a more readable version.
*/
public class ViewMetadataHelper implements IMetadataAware {
/**
* Flag to indicate that HTML output is requested.
*/
public static final String OUTPUT_TYPE_HTML = "html";
/**
* Flag to indicate that XML output is requested. This flag
* will leave the metadata document in it's original state. No
* transformation will take place.
*/
public static final String OUTPUT_TYPE_XML = "xml";
/**
* {@link Logger} used to log messages for this class.
*/
private static final Logger _logger = Logger.getLogger(ViewMetadataHelper.class);
/**
* Reference to the {@link IMetadataContext} object.
*/
private IMetadataContext m_context = null;
/**
* Default no-args constructor.
*/
public ViewMetadataHelper() {
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.common.metadata.IMetadataAware#setMetadataContext(com.esri.solutions.jitk.common.metadata.IMetadataContext)
*/
public void setMetadataContext(IMetadataContext ctx) {
if (ctx == null) {
throw new NullPointerException("metadata context cannot be null");
}
m_context = ctx;
}
/**
* Using the {@link IMetadataContext}, requests the metadata document from the
* configured CS/W service. Depending on the <code>outputType</code>, the returned
* value can be either an HTML transformation of the metadata document or the actual XML
* metadata document.
*
* @param guid ID of the metadata document hosted on the CS/W service.
* @param catalogId ID of the configured CS/W service that hosts the metadata document.
* @param outputType {@link #OUTPUT_TYPE_HTML} or {@link #OUTPUT_TYPE_XML}.
* @throws NullPointerException Thrown if <code>guid</code>, <code>catalogId</code>, or
* <code>outputType</code> arguments are <code>null</code>.
*/
public String generateHtmlForMetadataDoc(String guid, String catalogId,
String outputType) {
String html = null;
IMetadataCatalog catalog = null;
CSWMetadataProfile profile = null;
IMetadataDocument document = null;
MetadataID metadataId = null;
BasicMetadataExporter basicExporter = null;
String htmlTransformationXsl = null;
if (guid == null) {
throw new NullPointerException();
}
if (catalogId == null) {
throw new NullPointerException();
}
if (m_context == null) {
throw new NullPointerException();
}
catalog = m_context.getCatalogById(catalogId);
if (catalog != null) {
metadataId = new MetadataID();
metadataId.setCatalog(catalog);
metadataId.setId(guid);
// first attempt to get the metadata document via the GetRepositoryItem
// request. If that is not supported an InvalidMetadataOperationException
// will be thrown. Catch that exception and then do a GetRecordById request.
try {
document = catalog.lookupRepositoryItem(metadataId);
} catch (InvalidMetadataOperationException ex) {
_logger.warn(
"An operation exception has occurred looking up repository item with ID: [" +
metadataId.getId() + "]", ex);
_logger.warn(
"Attempting to retrieve metadata via another lookup by ID method.");
document = catalog.lookup(metadataId);
}
if (!document.getMetadataProfile().getExporters()
.containsKey("basicMetadataExporter")) {
_logger.warn(
"Unable to get display fields for CS/W record - skipping.");
_logger.debug(
"BasicMetadataExporter is missing or not configured properly.");
return html;
} else if (!(document.getMetadataProfile().getExporters()
.get("basicMetadataExporter") instanceof BasicMetadataExporter)) {
_logger.warn(
"Unable to get display fields for CS/W record - skipping.");
_logger.debug(
"Basic metadata exporter is not an instanceof BasicMetadataExporter.");
return html;
}
basicExporter = (BasicMetadataExporter) catalog.getMetadataProfile()
.getExporters()
.get("basicMetadataExporter");
document.export(basicExporter);
if (catalog.getMetadataProfile() instanceof CSWMetadataProfile) {
profile = (CSWMetadataProfile) catalog.getMetadataProfile();
htmlTransformationXsl = profile.getMetadataHtmlTransformationXsl();
if ((htmlTransformationXsl != null) &&
outputType.equalsIgnoreCase(OUTPUT_TYPE_HTML)) {
html = transformToHtml(htmlTransformationXsl,
basicExporter.getFullMetadata(), guid, catalogId);
} else {
_logger.warn(
"No Html transformation Xsl configured - returning raw document");
html = basicExporter.getFullMetadata();
}
} else {
_logger.warn(
"No Html transformation Xsl configured - returning raw document");
html = basicExporter.getFullMetadata();
}
} else {
_logger.warn(
"Invalid Catalog, unable to generate HTML for metadata document ID: [" +
guid + "] on catalog service: [" + catalogId + "]");
}
return html;
}
/**
* Utility method used to apply the given XSLT file to the metdata document
* XML. If there are any problems such that the XSLT file cannot be applied,
* the original <code>xml</code> will be returned.
*
* @param xslFile The classpath of the XSLT file used to transform the metadata document.
* @param xml The XML metadata document.
* @param guid The ID of the metadata document that is stored on the configured CS/W service.
* @param catalogId The ID of the configured CS/W service.
*
* @throws NullPointerException Thrown if the <code>xml</code> argument is <code>null</code>.
*/
private String transformToHtml(String xslFile, String xml, String guid,
String catalogId) {
String html = null;
Transformer transformer = null;
StreamResult streamResult = null;
StringWriter writer = null;
StringReader reader = null;
if (xml == null) {
throw new NullPointerException("xml cannot be null");
}
if (xslFile == null) {
_logger.warn("Invalid Xsl transformation file - return raw xml");
return xml;
}
try {
writer = new StringWriter();
reader = new StringReader(xml);
transformer = TransformerFactory.newInstance()
.newTransformer(new StreamSource(
getClass().getClassLoader().getResourceAsStream(xslFile)));
streamResult = new StreamResult(writer);
catalogId = catalogId.replace("&", "*/");
transformer.setParameter("metaId", guid);
transformer.setParameter("catId", catalogId);
transformer.transform(new StreamSource(reader), streamResult);
html = streamResult.getWriter().toString();
} catch (TransformerConfigurationException ex) {
_logger.warn("TransformerConfigurationException occurred while transforming metadata document into Html - returning raw document",
ex);
html = xml;
} catch (TransformerException ex) {
_logger.warn("TransformerException occurred while transforming metadata document into Html - returning raw document",
ex);
html = xml;
}
return html;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -