| 1 |
|
/* |
| 2 |
|
* Copyright 2005-2008 The Portal Application Laboratory Team. |
| 3 |
|
* |
| 4 |
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
|
* you may not use this file except in compliance with the License. |
| 6 |
|
* You may obtain a copy of the License at |
| 7 |
|
* |
| 8 |
|
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
* |
| 10 |
|
* Unless required by applicable law or agreed to in writing, software |
| 11 |
|
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
|
* See the License for the specific language governing permissions and |
| 14 |
|
* limitations under the License. |
| 15 |
|
*/ |
| 16 |
package jp.sf.pal.blog.converter; |
package jp.sf.pal.blog.converter; |
| 17 |
|
|
| 18 |
|
import java.io.BufferedReader; |
| 19 |
|
import java.io.ByteArrayInputStream; |
| 20 |
|
import java.io.IOException; |
| 21 |
|
import java.io.InputStreamReader; |
| 22 |
|
import java.io.UnsupportedEncodingException; |
| 23 |
import java.util.Map; |
import java.util.Map; |
| 24 |
|
|
| 25 |
import jp.sf.grizzly.pipeline.GrizzlyPipeline; |
import jp.sf.grizzly.pipeline.GrizzlyPipeline; |
| 26 |
|
import jp.sf.grizzly.pipeline.PipelineException; |
| 27 |
|
import jp.sf.grizzly.storage.StreamStorage; |
| 28 |
|
import jp.sf.grizzly.storage.StreamStorageException; |
| 29 |
|
import jp.sf.grizzly.storage.impl.ByteArrayStreamStorageImpl; |
| 30 |
|
import jp.sf.pal.blog.BlogConstants; |
| 31 |
|
|
| 32 |
|
import org.apache.commons.logging.Log; |
| 33 |
|
import org.apache.commons.logging.LogFactory; |
| 34 |
|
|
| 35 |
public class Converter |
public class Converter |
| 36 |
{ |
{ |
| 37 |
|
/** |
| 38 |
|
* Logger for this class |
| 39 |
|
*/ |
| 40 |
|
private static final Log log = LogFactory.getLog(Converter.class); |
| 41 |
|
|
| 42 |
private String formatType; |
private String formatType; |
| 43 |
|
|
| 44 |
private Map localizedName; |
private Map localizedName; |
| 100 |
this.formatType = name; |
this.formatType = name; |
| 101 |
} |
} |
| 102 |
|
|
| 103 |
|
/** |
| 104 |
|
* @param sourceText The sourceText to set. |
| 105 |
|
*/ |
| 106 |
|
public String convert(String sourceText) |
| 107 |
|
{ |
| 108 |
|
StringBuffer buffer = new StringBuffer(); |
| 109 |
|
try |
| 110 |
|
{ |
| 111 |
|
StreamStorage storage; |
| 112 |
|
try |
| 113 |
|
{ |
| 114 |
|
storage = new ByteArrayStreamStorageImpl( |
| 115 |
|
new ByteArrayInputStream(sourceText |
| 116 |
|
.getBytes(BlogConstants.UTF_8)), |
| 117 |
|
BlogConstants.UTF_8); |
| 118 |
|
} |
| 119 |
|
catch (UnsupportedEncodingException e1) |
| 120 |
|
{ |
| 121 |
|
log.warn("Unsupported encoding. ", e1); |
| 122 |
|
storage = new ByteArrayStreamStorageImpl( |
| 123 |
|
new ByteArrayInputStream(sourceText.getBytes()), |
| 124 |
|
BlogConstants.UTF_8); |
| 125 |
|
} |
| 126 |
|
getConverter().invoke(storage); |
| 127 |
|
|
| 128 |
|
BufferedReader r = null; |
| 129 |
|
try |
| 130 |
|
{ |
| 131 |
|
r = new BufferedReader(new InputStreamReader(storage |
| 132 |
|
.getResultInputStream(), storage.getEncoding())); |
| 133 |
|
String l = null; |
| 134 |
|
while ((l = r.readLine()) != null) |
| 135 |
|
{ |
| 136 |
|
buffer.append(l); |
| 137 |
|
buffer.append("\n"); |
| 138 |
|
} |
| 139 |
|
} |
| 140 |
|
catch (UnsupportedEncodingException e) |
| 141 |
|
{ |
| 142 |
|
log.warn("Unsupported Encoding. ", e); |
| 143 |
|
} |
| 144 |
|
catch (StreamStorageException e) |
| 145 |
|
{ |
| 146 |
|
log.warn("Stream Storage Exception. ", e); |
| 147 |
|
} |
| 148 |
|
catch (IOException e) |
| 149 |
|
{ |
| 150 |
|
log.error("I/O Exception. ", e); |
| 151 |
|
} |
| 152 |
|
finally |
| 153 |
|
{ |
| 154 |
|
if (r != null) |
| 155 |
|
{ |
| 156 |
|
try |
| 157 |
|
{ |
| 158 |
|
r.close(); |
| 159 |
|
} |
| 160 |
|
catch (IOException e) |
| 161 |
|
{ |
| 162 |
|
} |
| 163 |
|
} |
| 164 |
|
} |
| 165 |
|
storage.destroy(); |
| 166 |
|
return buffer.toString(); |
| 167 |
|
} |
| 168 |
|
catch (StreamStorageException e) |
| 169 |
|
{ |
| 170 |
|
log.error("StreamStorage Exception.", e); |
| 171 |
|
return null; |
| 172 |
|
} |
| 173 |
|
catch (PipelineException e) |
| 174 |
|
{ |
| 175 |
|
log.error("Pipeline Exception.", e); |
| 176 |
|
return null; |
| 177 |
|
} |
| 178 |
|
} |
| 179 |
} |
} |