Project

General

Profile

« Previous | Next » 

Revision 7860

use StreamSource instead of StringReader for method signature -- can be used with different sources this way. https://projects.ecoinformatics.org/ecoinfo/issues/6019

View differences:

src/edu/ucsb/nceas/metacat/DBTransform.java
115 115
		  if (xslSystemId != null) {
116 116
			// Create a stylesheet from the system id that was found
117 117
			doc = removeDOCTYPE(doc);
118
			StringReader xml = new StringReader(doc);
118
			StreamSource xml = new StreamSource(new StringReader(doc));
119 119
			StreamResult result = new StreamResult(w);
120 120
			doTransform(xml, result, xslSystemId, param, qformat, sessionid);
121 121
		  }
......
149 149
   * It then adds the parameters passed to it via Hashtable param to the Transformer.
150 150
   * It then calls the Transformer.transform method.
151 151
   */
152
  private void doTransform(StringReader docContent, 
152
  private void doTransform(StreamSource xml, 
153 153
          StreamResult resultOutput,
154 154
          String xslSystemId, 
155 155
          Hashtable<String, String[]> param,
......
161 161
      TransformerFactory tFactory;
162 162
      Transformer transformer;
163 163
      String key, value;
164
      StreamSource xml;
165 164
      Enumeration<String> en;
166 165
      Iterator<Map.Entry<String, String>> iterIt;
167 166
      Map.Entry<String, String> entry;
......
212 211
            transformer.setParameter(key, value);
213 212
          }
214 213
        }
215
        xml = new StreamSource(docContent);
216
        transformer.transform(xml,resultOutput);
214
        transformer.transform(xml, resultOutput);
217 215
    }
218 216
  }//doTransform
219 217

  
src/edu/ucsb/nceas/metacat/dataone/MNodeService.java
24 24
package edu.ucsb.nceas.metacat.dataone;
25 25

  
26 26
import java.io.ByteArrayInputStream;
27
import java.io.ByteArrayOutputStream;
27 28
import java.io.File;
28 29
import java.io.FileInputStream;
29 30
import java.io.FileOutputStream;
30 31
import java.io.IOException;
31 32
import java.io.InputStream;
33
import java.io.OutputStreamWriter;
32 34
import java.io.UnsupportedEncodingException;
35
import java.io.Writer;
33 36
import java.math.BigInteger;
34 37
import java.net.URISyntaxException;
35 38
import java.security.NoSuchAlgorithmException;
39
import java.sql.SQLException;
36 40
import java.util.ArrayList;
37 41
import java.util.Calendar;
38 42
import java.util.Date;
39 43
import java.util.HashSet;
44
import java.util.Hashtable;
40 45
import java.util.List;
41 46
import java.util.Map;
42 47
import java.util.Set;
......
86 91
import org.dataone.service.types.v1.NodeReference;
87 92
import org.dataone.service.types.v1.NodeState;
88 93
import org.dataone.service.types.v1.NodeType;
94
import org.dataone.service.types.v1.ObjectFormat;
89 95
import org.dataone.service.types.v1.ObjectFormatIdentifier;
90 96
import org.dataone.service.types.v1.ObjectList;
91 97
import org.dataone.service.types.v1.Permission;
......
111 117

  
112 118
import edu.ucsb.nceas.ezid.EZIDException;
113 119
import edu.ucsb.nceas.metacat.DBQuery;
120
import edu.ucsb.nceas.metacat.DBTransform;
114 121
import edu.ucsb.nceas.metacat.EventLog;
115 122
import edu.ucsb.nceas.metacat.IdentifierManager;
116 123
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
......
129 136
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
130 137
import gov.loc.repository.bagit.Bag;
131 138
import gov.loc.repository.bagit.BagFactory;
132
import gov.loc.repository.bagit.writer.Writer;
133 139
import gov.loc.repository.bagit.writer.impl.ZipWriter;
134 140

  
135 141
/**
......
1699 1705
			// TODO: delete more confidently
1700 1706
			bagFile.deleteOnExit();
1701 1707
			bag.setFile(bagFile);
1702
			Writer zipWriter = new ZipWriter(bagFactory);
1708
			ZipWriter zipWriter = new ZipWriter(bagFactory);
1703 1709
			bag.write(zipWriter, bagFile);
1704 1710
			bagFile = bag.getFile();
1705 1711
			bagInputStream = new FileInputStream(bagFile);
......
1733 1739
		return bagInputStream;
1734 1740

  
1735 1741
	}
1742
	
1743
	/**
1744
	 * Get a rendered view of the object identified by pid.
1745
	 * Uses the registered format given by the format parameter.
1746
	 * Typically, this is structured HTML that can be styled with CSS.
1747
	 * @param session
1748
	 * @param pid
1749
	 * @param format
1750
	 * @return
1751
	 * @throws InvalidToken
1752
	 * @throws ServiceFailure
1753
	 * @throws NotAuthorized
1754
	 * @throws NotFound
1755
	 * @throws NotImplemented
1756
	 */
1757
	public InputStream getPackage(Session session, Identifier pid, String format) throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, NotImplemented {
1758
		InputStream resultInputStream = null;
1759
		
1760
		SystemMetadata sysMeta = this.getSystemMetadata(session, pid);
1761
		InputStream object = this.get(session, pid);
1762

  
1763
		try {
1764
			// can only transform metadata, really
1765
			ObjectFormat objectFormat = ObjectFormatService.getInstance().getFormat(sysMeta.getFormatId());
1766
			if (objectFormat.getFormatType().equals("METADATA")) {
1767
				// transform
1768
				DBTransform transformer = new DBTransform();
1769
	            String documentContent = IOUtils.toString(object, "UTF-8");
1770
	            String sourceType = objectFormat.getFormatId().getValue();
1771
	            String targetType = "-//W3C//HTML//EN";
1772
	            ByteArrayOutputStream baos = new ByteArrayOutputStream();
1773
	            Writer writer = new OutputStreamWriter(baos , "UTF-8");
1774
	            // TODO: include more params?
1775
	            Hashtable<String, String[]> params = new Hashtable<String, String[]>();
1776
	            params.put("qformat", new String[] {format});
1777
	            transformer.transformXMLDocument(
1778
	                    documentContent , 
1779
	                    sourceType, 
1780
	                    targetType , 
1781
	                    format, 
1782
	                    writer, 
1783
	                    params, 
1784
	                    null //sessionid
1785
	                    );
1786
	            
1787
	            // finally, get the HTML back
1788
	            resultInputStream = new ContentTypeByteArrayInputStream(baos.toByteArray());
1789
	            ((ContentTypeByteArrayInputStream) resultInputStream).setContentType("text/html");
1790
	
1791
			} else {
1792
				// just return the raw bytes
1793
				resultInputStream = object;
1794
			}
1795
		} catch (IOException e) {
1796
			// report as service failure
1797
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
1798
			sf.initCause(e);
1799
			throw sf;
1800
		} catch (PropertyNotFoundException e) {
1801
			// report as service failure
1802
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
1803
			sf.initCause(e);
1804
			throw sf;
1805
		} catch (SQLException e) {
1806
			// report as service failure
1807
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
1808
			sf.initCause(e);
1809
			throw sf;
1810
		} catch (ClassNotFoundException e) {
1811
			// report as service failure
1812
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
1813
			sf.initCause(e);
1814
			throw sf;
1815
		}
1816
		
1817
		return resultInputStream;
1818
		
1819
	}
1736 1820
    
1737 1821
}

Also available in: Unified diff