From bd7c3ec609f1527db42601c65c3990423300ceca Mon Sep 17 00:00:00 2001 From: mcentner Date: Tue, 21 Oct 2008 15:00:33 +0000 Subject: Simplified IdentityLinkTransformer. git-svn-id: https://joinup.ec.europa.eu/svn/mocca/trunk@124 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4 --- .../slcommands/impl/InfoboxReadCommandImpl.java | 20 +- .../at/gv/egiz/idlink/IdentityLinkTransformer.java | 223 +++++---------------- 2 files changed, 71 insertions(+), 172 deletions(-) diff --git a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxReadCommandImpl.java b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxReadCommandImpl.java index b6c89e5b..d23c0598 100644 --- a/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxReadCommandImpl.java +++ b/bkucommon/src/main/java/at/gv/egiz/bku/slcommands/impl/InfoboxReadCommandImpl.java @@ -23,6 +23,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.net.MalformedURLException; import java.security.cert.CertificateEncodingException; import java.security.cert.CertificateException; import java.security.cert.CertificateFactory; @@ -415,11 +416,22 @@ public class InfoboxReadCommandImpl extends SLCommandImpltrue if this transformer is in use, or false otherwise - */ - public boolean isInUse() { - return inUse; - } - - @Override - public String toString() { - StringBuffer str = new StringBuffer(); - str.append("Transformer ").append(stylesheetURL) - .append("\n created ").append(new Date(created)).append(" used ").append( - timesUsed).append(" times, (init ").append(initTime).append("ms / ") - .append(((float) time) / timesUsed).append("ms avg) last time ").append(new Date(lastTimeUsed)); - return str.toString(); - } - - } - /** - * The transfomer factory. + * The transformer factory. */ private static SAXTransformerFactory factory; @@ -232,70 +122,67 @@ public class IdentityLinkTransformer { } - /** - * The pool of Transformer. - */ - private Map> pool; + /** + * Mapping of issuer template URIs to transformation templates. + */ + private Map templates = new HashMap(); /** * Private constructor. */ private IdentityLinkTransformer() { - pool = new HashMap>(); - } - - private IdLTransformer getFreeTransfomer(String stylesheetURL) throws TransformerConfigurationException, IOException { - - IdLTransformer transformer = null; - - List transfomerList = pool.get(stylesheetURL); - if (transfomerList == null) { - transfomerList = new ArrayList(); - pool.put(stylesheetURL, transfomerList); - } - - for (IdLTransformer candTransformer : transfomerList) { - if (!candTransformer.inUse) { - transformer = candTransformer; - break; - } - } - - if (transformer == null) { - transformer = new IdLTransformer(stylesheetURL); - transfomerList.add(transformer); - } - - transformer.inUse = true; - return transformer; - - } - + } + + /** + * Transforms an identity link source to result with + * the given issuer template from the stylesheetURL. + * + * @param stylesheetURL + * the URL of the issuer template to be used for transformation + * @param source + * the compressed identity link source + * @param result + * the transformed identity link result + * + * @throws MalformedURLException + * if the given stylesheetURL is not a valid + * http or https URL. + * @throws IOException + * if dereferencing the stylesheetURL fails. + * @throws TransformerConfigurationException + * if creating a transformation template from the dereferenced + * stylesheet fails. + * @throws TransformerException + * if transforming the identity link fails. + */ public void transformIdLink(String stylesheetURL, Source source, Result result) throws IOException, TransformerException { - log.trace("Trying to get free IdentityLinkTransformer for issuer template '" + stylesheetURL + "'."); - IdLTransformer transformer = getFreeTransfomer(stylesheetURL); - log.trace("Trying to transform IdentityLink."); + + Templates templ = templates.get(stylesheetURL); + + if (templ == null) { + + // TODO: implement stylesheet cache + URL url = new URL(stylesheetURL); + + if (!"http".equalsIgnoreCase(url.getProtocol()) && !"https".equalsIgnoreCase(url.getProtocol())) { + throw new MalformedURLException("Protocol " + url.getProtocol() + " not supported for IssuerTemplate URL."); + } + + URLDereferencer dereferencer = URLDereferencer.getInstance(); + StreamData data = dereferencer.dereference(url.toExternalForm(), null); + + log.trace("Trying to create issuer template."); + templ = factory.newTemplates(new StreamSource(data.getStream())); + log.trace("Successfully created issuer template"); + + templates.put(stylesheetURL, templ); + + } + + Transformer transformer = templ.newTransformer(); + transformer.transform(source, result); - log.trace("IdentityLink transformed successfully. " + getStatistics()); - } - - public String getStatistics() { - - StringBuffer str = new StringBuffer(); - Iterator keys = pool.keySet().iterator(); - int count = 0; - while (keys.hasNext()) { - String stylesheetURL = (String) keys.next(); - str.append("Stylesheet URL: ").append(stylesheetURL); - Iterator transformer = pool.get(stylesheetURL).iterator(); - while (transformer.hasNext()) { - IdLTransformer idLTransformer = (IdLTransformer) transformer.next(); - str.append("\n ").append(idLTransformer); - count++; - } - } - str.append("\n(").append(count).append(" transformer)"); - return str.toString(); + } } -- cgit v1.2.3