Project

General

Profile

« Previous | Next » 

Revision 2088

Added by Jing Tao about 20 years ago

Add a new transform method.

View differences:

src/edu/ucsb/nceas/metacat/DBTransform.java
110 110
    transformXMLDocument(doc, sourceType, targetType, qformat, pw, param, null);
111 111
  }
112 112

  
113
   /**
114
   * @see transformXMLDocument(String doc, String sourceType,
115
   *            String targetType, String qFormat, StringWriter pw
116
   *            String sessionid)
117
   */
118
  public void transformXMLDocument(String doc, String sourceType,
119
                String targetType, String qFormat, StringWriter pw)
120
  {
121
    transformXMLDocument(doc, sourceType, targetType, qFormat, pw, null);
122
  }
123

  
124

  
113 125
  /**
114 126
   * Transform an XML document using the stylesheet reference from the db
115 127
   *
......
128 140

  
129 141
    // Look up the stylesheet for this type combination
130 142
    String xslSystemId = getStyleSystemId(qformat, sourceType, targetType);
143
    if (xslSystemId != null)
144
    {
145
      try
146
      {// Create a stylesheet from the system id that was found
147
        doc = removeDOCTYPE(doc);
148
        StringReader xml = new StringReader(doc);
149
        StreamResult result = new StreamResult(pw);
150
        doTransform(xml, result, xslSystemId, param, qformat, sessionid);
131 151

  
132
    if (xslSystemId != null) {
133
      // Create a stylesheet from the system id that was found
134
      try {
135
        TransformerFactory tFactory = TransformerFactory.newInstance();
136
        Transformer transformer = tFactory.newTransformer(
137
                                  new StreamSource(xslSystemId));
138
        transformer.setParameter("qformat", qformat);
139
        MetaCatUtil.debugMessage("qformat: "+qformat, 30);
140

  
141
        if(sessionid != null)
142
        {
143
          transformer.setParameter("sessid", sessionid);
144
        }
145

  
146
        // Set up parameter for transformation
147
        if ( param != null)
148
        {
149
          Enumeration en = param.keys();
150
          while (en.hasMoreElements())
151
          {
152
            String key =(String)en.nextElement();
153
            String value = ((String[])(param.get(key)))[0];
154
            MetaCatUtil.debugMessage(key+" : "+value, 30);
155
            transformer.setParameter(key, value);
156
          }
157
        }
158
        
159
        doc = removeDOCTYPE(doc);
160
        StreamSource xml = new StreamSource(new StringReader(doc));
161
        //modify the system id for dtd
162
        //modifiedXmlStreamSource(xml, sourceType);
163
        transformer.transform(xml, new StreamResult(pw));
164
      } catch (Exception e) {
152
      }
153
      catch (Exception e)
154
      {
165 155
        pw.println(xslSystemId + "Error transforming document in " +
166 156
                   "DBTransform.transformXMLDocument: " +
167 157
                   e.getMessage());
168 158

  
169 159
      }
170
    } else {
160
    }
161
    else
162
    {
171 163
      // No stylesheet registered form this document type, so just return the
172 164
      // XML stream we were passed
173 165
      pw.print(doc);
......
185 177
   */
186 178
  public void transformXMLDocument(String doc, String sourceType,
187 179
                String targetType, String qFormat, StringWriter pw,
188
                String sessionid) {
180
                String sessionid)
181
  {
189 182

  
190 183
    // Look up the stylesheet for this type combination
191 184
    String xslSystemId = getStyleSystemId(qFormat, sourceType, targetType);
192

  
193
    if (xslSystemId != null) {
185
    if (xslSystemId != null)
186
    {
194 187
      // Create a stylesheet from the system id that was found
195
      try {
196
        TransformerFactory tFactory = TransformerFactory.newInstance();
197
        Transformer transformer = tFactory.newTransformer(
198
                                  new StreamSource(xslSystemId));
199
        if(sessionid != null)
200
        {
201
          transformer.setParameter("sessid", sessionid);
202
        }
203
        transformer.setParameter("qFormat", qFormat);
188
      try
189
      {
204 190
        doc = removeDOCTYPE(doc);
205
        StreamSource xml = new StreamSource(new StringReader(doc));
206
        //modify the system id for dtd
207
        //modifiedXmlStreamSource(xml, sourceType);
208
        transformer.transform(xml, new StreamResult(pw));
209
      } catch (Exception e) {
191
        StringReader xml = new StringReader(doc);
192
        StreamResult result = new StreamResult(pw);
193
        doTransform(xml, result, xslSystemId, null, qFormat, sessionid);
194
      }
195
      catch (Exception e)
196
      {
210 197
        util.debugMessage(xslSystemId + "Error transforming document in " +
211 198
                   "DBTransform.transformXMLDocument: " +
212 199
                   e.getMessage(), 30);
213 200

  
214 201
      }
215
    } else {
202
    }
203
    else
204
    {
216 205
      // No stylesheet registered form this document type, so just return the
217 206
      // XML stream we were passed
218 207
      pw.write(doc);
......
220 209
  }
221 210

  
222 211
  /**
223
   * @see transformXMLDocument(String doc, String sourceType,
224
   *            String targetType, String qFormat, StringWriter pw
225
   *            String sessionid)
212
   * Method to do transform for a string reader
213
   * @param doc the document to be transformed
214
   * @param sourcetype the document type of the source
215
   * @param targettype the target document type
216
   * @param qformat the name of the style set to use
217
   * @param pw the PrintWriter to which output is printed
218
   * @param params some parameters for eml2 transformation
226 219
   */
227
  public void transformXMLDocument(String doc, String sourceType,
228
                String targetType, String qFormat, StringWriter pw)
220
   public void transformXMLDocument(StringReader docContent, String sourceType,
221
                                   String targetType, String qformat,
222
                                   PrintWriter pw, Hashtable param,
223
                                   String sessionid)
224
   {
225
     // Look up the stylesheet for this type combination
226
    String xslSystemId = getStyleSystemId(qformat, sourceType, targetType);
227
    if (xslSystemId != null)
228
    {
229
      try
230
      {// Create a stylesheet from the system id that was found
231
        StreamResult result = new StreamResult(pw);
232
        doTransform(docContent, result, xslSystemId, param, qformat, sessionid);
233

  
234
      }
235
      catch (Exception e)
236
      {
237
        pw.println(xslSystemId + "Error transforming document in " +
238
                   "DBTransform.transformXMLDocument: " +
239
                   e.getMessage());
240

  
241
      }
242
    }
243
    else
244
    {
245
      // No stylesheet registered form this document type, so just return the
246
      // XML stream we were passed
247
      pw.print(docContent);
248
    }
249
   }
250

  
251
  /*
252
   * Method to do transfer
253
   */
254
  private void doTransform(StringReader docContent, StreamResult resultOutput,
255
                           String xslSystemId, Hashtable param,
256
                           String qformat, String sessionid)
257
                          throws Exception
229 258
  {
230
    transformXMLDocument(doc, sourceType, targetType, qFormat, pw, null);
231
  }
259
    if (xslSystemId != null)
260
    {
261
        TransformerFactory tFactory = TransformerFactory.newInstance();
262
        Transformer transformer = tFactory.newTransformer(
263
                                  new StreamSource(xslSystemId));
264
        transformer.setParameter("qformat", qformat);
265
        MetaCatUtil.debugMessage("qformat: "+qformat, 30);
232 266

  
267
        if(sessionid != null)
268
        {
269
          transformer.setParameter("sessid", sessionid);
270
        }
271
        // Set up parameter for transformation
272
        if ( param != null)
273
        {
274
          Enumeration en = param.keys();
275
          while (en.hasMoreElements())
276
          {
277
            String key =(String)en.nextElement();
278
            String value = ((String[])(param.get(key)))[0];
279
            MetaCatUtil.debugMessage(key+" : "+value, 30);
280
            transformer.setParameter(key, value);
281
          }
282
        }
283
        StreamSource xml = new StreamSource(docContent);
284
        transformer.transform(xml,resultOutput);
285
    }
286
  }//doTransform
287

  
288

  
233 289
  /**
234 290
   * gets the content of a tag in a given xml file with the given path
235 291
   * @param f the file to parse
......
371 427
 /* Method to modified the system id of xml input -- make sure it
372 428
    points to system id in xml_catalog table
373 429
  */
374
  private void modifiedXmlStreamSource(StreamSource xml, String publicId)   
430
  private void modifiedXmlStreamSource(StreamSource xml, String publicId)
375 431
                                       throws Exception
376 432
  {
377 433
    // make sure the xml is not null
......
387 443
    //set system id to input stream
388 444
    xml.setSystemId(systemId);
389 445
  }
390
  
446

  
391 447
  /*
392 448
   * removes the DOCTYPE element and its contents from a Sting
393
   * used to avoid problems with incorrect SystemIDs 
449
   * used to avoid problems with incorrect SystemIDs
394 450
   */
395
  private String removeDOCTYPE(String in) {  
451
  private String removeDOCTYPE(String in) {
396 452
    String ret = "";
397 453
    int startpos = in.indexOf("<!DOCTYPE");
398 454
    if (startpos>-1) {
399
      int stoppos = in.indexOf(">", startpos + 8);   
455
      int stoppos = in.indexOf(">", startpos + 8);
400 456
      ret = in.substring(0,startpos) + in.substring(stoppos+1,in.length());
401 457
    } else {
402 458
      return in;
403 459
    }
404
    return ret;  
460
    return ret;
405 461
  }
406 462

  
407 463
  /**

Also available in: Unified diff