ImageCodec.jvm.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. using System;
  2. using System.Configuration;
  3. using System.Collections;
  4. using System.Collections.Specialized;
  5. using System.Drawing.Imaging;
  6. using System.Xml;
  7. using Mainsoft.Drawing.Configuration;
  8. using imageio = javax.imageio;
  9. using stream = javax.imageio.stream;
  10. using awt = java.awt;
  11. using image = java.awt.image;
  12. using spi = javax.imageio.spi;
  13. using dom = org.w3c.dom;
  14. namespace Mainsoft.Drawing.Imaging {
  15. /// <summary>
  16. /// Summary description for ImageCodec.
  17. /// </summary>
  18. public class ImageCodec : IDisposable {
  19. #region Members
  20. imageio.ImageReader _nativeReader = null;
  21. imageio.ImageWriter _nativeWriter = null;
  22. stream.ImageInputStream _nativeStream = null;
  23. ImageFormat _imageFormat = null;
  24. int _currentFrame = 0;
  25. #endregion
  26. #region Constructros
  27. protected ImageCodec() {
  28. }
  29. static ImageCodec() {
  30. }
  31. #endregion
  32. #region Internal properties
  33. internal imageio.ImageReader NativeReader {
  34. get { return _nativeReader; }
  35. set {
  36. _nativeReader = value;
  37. if (value == null)
  38. return;
  39. _imageFormat = MimeTypesToImageFormat( value.getOriginatingProvider().getMIMETypes() );
  40. }
  41. }
  42. internal imageio.ImageWriter NativeWriter {
  43. get { return _nativeWriter; }
  44. set {
  45. _nativeWriter = value;
  46. if (value == null)
  47. return;
  48. _imageFormat = MimeTypesToImageFormat( value.getOriginatingProvider().getMIMETypes() );
  49. }
  50. }
  51. internal stream.ImageInputStream NativeStream {
  52. get { return _nativeStream; }
  53. set {
  54. _nativeStream = value;
  55. if (value == null)
  56. return;
  57. if (NativeReader != null)
  58. NativeReader.setInput( value );
  59. if (NativeWriter != null)
  60. NativeWriter.setOutput( value );
  61. }
  62. }
  63. #endregion
  64. #region ImageCodec factory methods
  65. public static ImageCodec CreateReader(stream.ImageInputStream inputStream) {
  66. java.util.Iterator iter = imageio.ImageIO.getImageReaders( inputStream );
  67. return CreateReader(iter);
  68. }
  69. public static ImageCodec CreateReader(ImageFormat imageFormat) {
  70. return CreateReader( ImageFormatToClsid( imageFormat ) );
  71. }
  72. public static ImageCodec CreateReader(Guid clsid) {
  73. ImageCodec codec = null;
  74. try {
  75. ImageCodecInfo codecInfo = FindDecoder(clsid);
  76. java.util.Iterator iter = imageio.ImageIO.getImageReadersByMIMEType( codecInfo.MimeType );
  77. codec = CreateReader(iter);
  78. }
  79. catch {}
  80. if (codec == null) {
  81. ImageFormat format = ClsidToImageFormat(clsid);
  82. string name = (format != null) ? format.ToString() : clsid.ToString();
  83. throw new NotSupportedException(String.Format("The '{0}' format decoder is not installed.", name));
  84. }
  85. return codec;
  86. }
  87. private static ImageCodec CreateReader(java.util.Iterator iter) {
  88. if ( !iter.hasNext() )
  89. return null;
  90. ImageCodec imageCodec = new ImageCodec();
  91. imageCodec.NativeReader = (imageio.ImageReader) iter.next();
  92. return imageCodec;
  93. }
  94. public static ImageCodec CreateWriter(ImageFormat imageFormat) {
  95. return CreateWriter( ImageFormatToClsid( imageFormat ) );
  96. }
  97. public static ImageCodec CreateWriter(Guid clsid) {
  98. ImageCodec codec = null;
  99. try {
  100. ImageCodecInfo codecInfo = FindEncoder(clsid);
  101. java.util.Iterator iter = imageio.ImageIO.getImageWritersByMIMEType( codecInfo.MimeType );
  102. codec = CreateWriter(iter);
  103. }
  104. catch {}
  105. if (codec == null) {
  106. ImageFormat format = ClsidToImageFormat(clsid);
  107. string name = (format != null) ? format.ToString() : clsid.ToString();
  108. throw new NotSupportedException(String.Format("The '{0}' format encoder is not installed.", name));
  109. }
  110. return codec;
  111. }
  112. private static ImageCodec CreateWriter(java.util.Iterator iter) {
  113. if ( !iter.hasNext() )
  114. return null;
  115. ImageCodec imageCodec = new ImageCodec();
  116. imageCodec.NativeWriter = (imageio.ImageWriter) iter.next();
  117. return imageCodec;
  118. }
  119. #endregion
  120. #region Codec enumerations
  121. internal static Hashtable Decoders {
  122. get {
  123. const string MYNAME = "System.Drawing.Imaging.ImageCodecInfo.decoders";
  124. Hashtable o = (Hashtable) AppDomain.CurrentDomain.GetData (MYNAME);
  125. if (o != null)
  126. return o;
  127. o = new ReaderSpiIterator().Iterate();
  128. AppDomain.CurrentDomain.SetData(MYNAME, o);
  129. return o;
  130. }
  131. }
  132. internal static Hashtable Encoders {
  133. get {
  134. const string MYNAME = "System.Drawing.Imaging.ImageCodecInfo.encoders";
  135. Hashtable o = (Hashtable) AppDomain.CurrentDomain.GetData (MYNAME);
  136. if (o != null)
  137. return o;
  138. o = new WriterSpiIterator().Iterate();
  139. AppDomain.CurrentDomain.SetData(MYNAME, o);
  140. return o;
  141. }
  142. }
  143. internal static ImageCodecInfo FindEncoder (Guid clsid) {
  144. ImageCodecInfo codec = (ImageCodecInfo) Encoders[clsid];
  145. if (codec == null) {
  146. // .net saves in png if cannot find requested encoder. atc id 316563
  147. codec = (ImageCodecInfo) Encoders[ ImageCodec.PngClsid ];
  148. }
  149. return codec;
  150. }
  151. internal static ImageCodecInfo FindDecoder (Guid clsid) {
  152. ImageCodecInfo codec = (ImageCodecInfo) Decoders[clsid];
  153. if (codec == null) {
  154. ImageFormat format = ClsidToImageFormat(clsid);
  155. string name = (format != null) ? format.ToString() : clsid.ToString();
  156. throw new NotSupportedException(String.Format("The '{0}' format decoder is not installed.", name));
  157. }
  158. return codec;
  159. }
  160. #endregion
  161. #region SpiIterators
  162. abstract class BaseSpiIterator {
  163. protected abstract java.util.Iterator GetIterator (string mimeType);
  164. protected abstract spi.ImageReaderWriterSpi GetNext (java.util.Iterator iter);
  165. #region ProcessOneCodec
  166. private ImageCodecInfo ProcessOneCodec (Guid clsid, Guid formatID, string mimeType) {
  167. ImageCodecInfo ici = new ImageCodecInfo ();
  168. ici.Clsid = clsid;
  169. ici.FormatID = formatID;
  170. ici.MimeType = mimeType;
  171. java.util.Iterator iter = null;
  172. try {
  173. iter = GetIterator (mimeType);
  174. }
  175. catch(Exception) {
  176. return null;
  177. }
  178. while (iter.hasNext ()) {
  179. spi.ImageReaderWriterSpi rw = GetNext (iter);
  180. try {
  181. ici.CodecName = rw.getDescription (java.util.Locale.getDefault ());
  182. ici.DllName = null;
  183. foreach (string suffix in rw.getFileSuffixes ()) {
  184. if (ici.FilenameExtension != null)
  185. ici.FilenameExtension += ";";
  186. ici.FilenameExtension += "*."+suffix;
  187. }
  188. ici.Flags = ImageCodecFlags.Builtin|ImageCodecFlags.SupportBitmap;
  189. if (rw is spi.ImageReaderSpi) {
  190. ici.Flags |= ImageCodecFlags.Decoder;
  191. if ((rw as spi.ImageReaderSpi).getImageWriterSpiNames().Length != 0)
  192. ici.Flags |= ImageCodecFlags.Encoder;
  193. }
  194. if (rw is spi.ImageWriterSpi) {
  195. ici.Flags |= ImageCodecFlags.Encoder;
  196. if ((rw as spi.ImageWriterSpi).getImageReaderSpiNames().Length != 0)
  197. ici.Flags |= ImageCodecFlags.Decoder;
  198. }
  199. ici.FormatDescription = string.Join(";",
  200. rw.getFormatNames());
  201. ici.Version = (int)Convert.ToDouble(rw.getVersion ());
  202. break;
  203. }
  204. catch {
  205. }
  206. }
  207. return ici;
  208. }
  209. #endregion
  210. internal Hashtable Iterate () {
  211. // TBD: Insert Exception handling here
  212. NameValueCollection nvc = (NameValueCollection) System.Configuration.ConfigurationSettings
  213. .GetConfig ("system.drawing/codecs");
  214. Hashtable codecs = new Hashtable (10);
  215. for (int i=0; i<nvc.Count; i++) {
  216. Guid clsid = new Guid (nvc.GetKey (i));
  217. ImageFormat format = ClsidToImageFormat (clsid);
  218. ImageCodecInfo codec = ProcessOneCodec (clsid, format.Guid, nvc[i]);
  219. if ((codec != null) && (codec.FilenameExtension != null))
  220. codecs [clsid] = codec;
  221. }
  222. return codecs;
  223. }
  224. }
  225. class ReaderSpiIterator: BaseSpiIterator {
  226. protected override java.util.Iterator GetIterator(string mimeType) {
  227. return imageio.ImageIO.getImageReadersByMIMEType (mimeType);
  228. }
  229. protected override javax.imageio.spi.ImageReaderWriterSpi GetNext(java.util.Iterator iter) {
  230. imageio.ImageReader r = (imageio.ImageReader) iter.next ();
  231. return r.getOriginatingProvider ();
  232. }
  233. }
  234. class WriterSpiIterator: BaseSpiIterator {
  235. protected override java.util.Iterator GetIterator(string mimeType) {
  236. return imageio.ImageIO.getImageWritersByMIMEType (mimeType);
  237. }
  238. protected override javax.imageio.spi.ImageReaderWriterSpi GetNext(java.util.Iterator iter) {
  239. imageio.ImageWriter w = (imageio.ImageWriter) iter.next ();
  240. return w.getOriginatingProvider ();
  241. }
  242. }
  243. #endregion
  244. #region Clsid and FormatID
  245. static Guid BmpClsid = new Guid ("557cf400-1a04-11d3-9a73-0000f81ef32e");
  246. static Guid JpegClsid = new Guid ("557cf401-1a04-11d3-9a73-0000f81ef32e");
  247. static Guid GifClsid = new Guid ("557cf402-1a04-11d3-9a73-0000f81ef32e");
  248. static Guid EmfClsid = new Guid ("557cf403-1a04-11d3-9a73-0000f81ef32e");
  249. static Guid WmfClsid = new Guid ("557cf404-1a04-11d3-9a73-0000f81ef32e");
  250. static Guid TiffClsid = new Guid ("557cf405-1a04-11d3-9a73-0000f81ef32e");
  251. static Guid PngClsid = new Guid ("557cf406-1a04-11d3-9a73-0000f81ef32e");
  252. static Guid IconClsid = new Guid ("557cf407-1a04-11d3-9a73-0000f81ef32e");
  253. private static ImageFormat MimeTypesToImageFormat (string [] mimeTypes) {
  254. foreach (ImageCodecInfo codec in Decoders.Values)
  255. for (int i=0; i<mimeTypes.Length; i++)
  256. if (codec.MimeType == mimeTypes [i])
  257. return new ImageFormat (codec.FormatID);
  258. return null;
  259. }
  260. internal static ImageFormat ClsidToImageFormat (Guid clsid) {
  261. if (clsid.Equals (BmpClsid))
  262. return ImageFormat.Bmp;
  263. else if (clsid.Equals (JpegClsid))
  264. return ImageFormat.Jpeg;
  265. else if (clsid.Equals (GifClsid))
  266. return ImageFormat.Gif;
  267. else if (clsid.Equals (EmfClsid))
  268. return ImageFormat.Emf;
  269. else if (clsid.Equals (WmfClsid))
  270. return ImageFormat.Wmf;
  271. else if (clsid.Equals (TiffClsid))
  272. return ImageFormat.Tiff;
  273. else if (clsid.Equals (PngClsid))
  274. return ImageFormat.Png;
  275. else if (clsid.Equals (IconClsid))
  276. return ImageFormat.Icon;
  277. else
  278. return null;
  279. }
  280. internal static Guid ImageFormatToClsid (ImageFormat format) {
  281. if (format == null)
  282. return Guid.Empty;
  283. if (format.Guid.Equals (ImageFormat.Bmp.Guid))
  284. return BmpClsid;
  285. else if (format.Guid.Equals (ImageFormat.Jpeg.Guid))
  286. return JpegClsid;
  287. else if (format.Guid.Equals (ImageFormat.Gif))
  288. return GifClsid;
  289. else if (format.Guid.Equals (ImageFormat.Emf.Guid))
  290. return EmfClsid;
  291. else if (format.Guid.Equals (ImageFormat.Wmf.Guid))
  292. return WmfClsid;
  293. else if (format.Guid.Equals (ImageFormat.Tiff.Guid))
  294. return TiffClsid;
  295. else if (format.Guid.Equals (ImageFormat.Png.Guid))
  296. return PngClsid;
  297. else if (format.Guid.Equals (ImageFormat.Icon.Guid))
  298. return IconClsid;
  299. else
  300. return Guid.Empty;
  301. }
  302. private FrameDimension FormatFrameDimesion {
  303. get {
  304. if (ImageFormat == null)
  305. return FrameDimension.Page;
  306. if (ImageFormat.Guid.Equals (ImageFormat.Bmp.Guid))
  307. return FrameDimension.Page;
  308. else if (ImageFormat.Guid.Equals (ImageFormat.Jpeg.Guid))
  309. return FrameDimension.Page;
  310. else if (ImageFormat.Guid.Equals (ImageFormat.Gif))
  311. return FrameDimension.Time;
  312. else if (ImageFormat.Guid.Equals (ImageFormat.Emf.Guid))
  313. return FrameDimension.Page;
  314. else if (ImageFormat.Guid.Equals (ImageFormat.Wmf.Guid))
  315. return FrameDimension.Page;
  316. else if (ImageFormat.Guid.Equals (ImageFormat.Tiff.Guid))
  317. return FrameDimension.Page;
  318. else if (ImageFormat.Guid.Equals (ImageFormat.Png.Guid))
  319. return FrameDimension.Page;
  320. else if (ImageFormat.Guid.Equals (ImageFormat.Icon.Guid))
  321. return FrameDimension.Resolution;
  322. else
  323. return FrameDimension.Page;
  324. }
  325. }
  326. #endregion
  327. #region Image read/write methods
  328. public PlainImage ReadPlainImage() {
  329. awt.Image img = ReadImage( _currentFrame );
  330. if (img == null)
  331. return null;
  332. // its possible to fail to load thumbnails and metadata, but image is ok.
  333. awt.Image [] th = null;
  334. #if THUMBNAIL_SUPPORTED
  335. try {
  336. th = ReadThumbnails( _currentFrame );
  337. }
  338. catch (Exception) {}
  339. #endif
  340. XmlDocument md = null;
  341. imageio.metadata.IIOMetadata nativeMd = null;
  342. try {
  343. nativeMd = ReadImageMetadata( _currentFrame );
  344. md = ConvertImageMetadata( nativeMd );
  345. }
  346. catch (Exception) {}
  347. float [] resolution = GetResolution( md );
  348. PlainImage pi = new PlainImage( img, th, ImageFormat, resolution[0], resolution[1], FormatFrameDimesion );
  349. pi.NativeMetadata = nativeMd;
  350. return pi;
  351. }
  352. public PlainImage ReadNextPlainImage() {
  353. _currentFrame++;
  354. return ReadPlainImage();
  355. }
  356. private awt.Image ReadImage(int frame) {
  357. if (NativeStream == null)
  358. throw new Exception("Input stream not specified");
  359. try {
  360. return NativeReader.read (frame);
  361. }
  362. catch (java.lang.IndexOutOfBoundsException) {
  363. return null;
  364. }
  365. catch (java.io.IOException ex) {
  366. throw new System.IO.IOException(ex.Message, ex);
  367. }
  368. }
  369. #if THUMBNAIL_SUPPORTED
  370. private awt.Image [] ReadThumbnails(int frameIndex) {
  371. awt.Image [] thArray = null;
  372. try {
  373. if (NativeReader.readerSupportsThumbnails()) {
  374. int tmbNumber = NativeReader.getNumThumbnails(frameIndex);
  375. if (tmbNumber > 0) {
  376. thArray = new awt.Image[ tmbNumber ];
  377. for (int i = 0; i < tmbNumber; i++) {
  378. thArray[i] = NativeReader.readThumbnail(frameIndex, i);
  379. }
  380. }
  381. }
  382. return thArray;
  383. }
  384. catch (java.io.IOException ex) {
  385. throw new System.IO.IOException(ex.Message, ex);
  386. }
  387. }
  388. #endif
  389. public void WritePlainImage(PlainImageCollection pic) {
  390. if ((pic == null) || (pic.Count == 0))
  391. return;
  392. if (pic.Count == 1) {
  393. WritePlainImage( pic[0] );
  394. return;
  395. }
  396. try {
  397. if (NativeWriter.canWriteSequence ()) {
  398. NativeWriter.prepareWriteSequence (null);
  399. for (int i=0; i < pic.Count; i++) {
  400. imageio.IIOImage iio = GetIIOImageContainer( pic[i] );
  401. NativeWriter.writeToSequence (iio, null);
  402. }
  403. NativeWriter.endWriteSequence ();
  404. }
  405. else
  406. WritePlainImage( pic[0] );
  407. }
  408. catch (java.io.IOException ex) {
  409. throw new System.IO.IOException(ex.Message, ex);
  410. }
  411. }
  412. public void WritePlainImage(PlainImage pi) {
  413. try {
  414. imageio.IIOImage iio = GetIIOImageContainer( pi );
  415. WriteImage( iio );
  416. }
  417. catch (java.io.IOException ex) {
  418. throw new System.IO.IOException(ex.Message, ex);
  419. }
  420. }
  421. private void WriteImage(imageio.IIOImage iio) {
  422. if (NativeStream == null)
  423. throw new Exception("Output stream not specified");
  424. NativeWriter.write( iio );
  425. NativeStream.flush();
  426. }
  427. private imageio.IIOImage GetIIOImageContainer(PlainImage pi) {
  428. java.util.ArrayList al = null;
  429. // prepare thumbnails list
  430. if (pi.Thumbnails != null) {
  431. al = new java.util.ArrayList( pi.Thumbnails.Length );
  432. for (int i=0; i < pi.Thumbnails.Length; i++)
  433. al.add(pi.Thumbnails[i]);
  434. }
  435. // prepare IIOImage container
  436. if (pi.NativeImage is image.BufferedImage) {
  437. imageio.IIOImage iio = new javax.imageio.IIOImage(
  438. (image.BufferedImage)pi.NativeImage, al, null /*pi.NativeMetadata*/);
  439. return iio;
  440. }
  441. else
  442. // TBD: This codec is for raster formats only
  443. throw new NotSupportedException("Only raster formats are supported");
  444. }
  445. private imageio.metadata.IIOMetadata ReadImageMetadata(int frameIndex) {
  446. if (NativeStream == null)
  447. throw new Exception("Input stream not specified");
  448. try {
  449. imageio.metadata.IIOMetadata md = NativeReader.getImageMetadata( frameIndex );
  450. return md;
  451. }
  452. catch (java.io.IOException ex) {
  453. throw new System.IO.IOException(ex.Message, ex);
  454. }
  455. }
  456. #endregion
  457. #region Extra properties
  458. public ImageFormat ImageFormat {
  459. get { return _imageFormat; }
  460. }
  461. #endregion
  462. #region Metadata parse
  463. private float [] GetResolution(XmlDocument metaData) {
  464. if (metaData == null)
  465. return new float[]{0, 0};
  466. ResolutionConfigurationCollection rcc =
  467. (ResolutionConfigurationCollection)
  468. ConfigurationSettings.GetConfig("system.drawing/codecsmetadata");
  469. if (rcc == null)
  470. throw new ConfigurationException("Configuration section codecsmetadata not found");
  471. ResolutionConfiguration rc = rcc[ ImageFormat.ToString() ];
  472. if (rc == null)
  473. return new float[]{0, 0};
  474. // Horizontal resolution
  475. string xResPath = rc.XResPath;
  476. string xRes;
  477. if (xResPath == string.Empty)
  478. xRes = rc.XResDefault;
  479. else
  480. xRes = GetValueFromMetadata(metaData, xResPath);
  481. if ((xRes == null) || (xRes == string.Empty))
  482. xRes = rc.XResDefault;
  483. // Vertical resolution
  484. string yResPath = rc.YResPath;
  485. string yRes;
  486. if (yResPath == string.Empty)
  487. yRes = rc.YResDefault;
  488. else
  489. yRes = GetValueFromMetadata(metaData, yResPath);
  490. if ((yRes == null) || (yRes == string.Empty))
  491. yRes = rc.YResDefault;
  492. // Resolution units
  493. string resUnitsPath = rc.UnitsTypePath;
  494. string resUnitsType;
  495. if (resUnitsPath == string.Empty)
  496. resUnitsType = rc.UnitsTypeDefault;
  497. else
  498. resUnitsType = GetValueFromMetadata(metaData, resUnitsPath);
  499. if (resUnitsType == null)
  500. resUnitsType = rc.UnitsTypeDefault;
  501. // Unit scale
  502. string unitScale = rc.UnitsScale[resUnitsType].ToString();
  503. // Adjust resolution to its units
  504. float [] res = new float[2];
  505. res[0] = ParseFloatValue(xRes) * ParseFloatValue(unitScale);
  506. res[1] = ParseFloatValue(yRes) * ParseFloatValue(unitScale);
  507. return res;
  508. }
  509. private string GetValueFromMetadata(XmlDocument metaData, string path) {
  510. XmlNode n = metaData.SelectSingleNode(path);
  511. if (n == null)
  512. return null;
  513. return n.InnerText;
  514. }
  515. private XmlDocument ConvertImageMetadata(imageio.metadata.IIOMetadata metaData) {
  516. string [] formatNames = metaData.getMetadataFormatNames();
  517. dom.Element rootNode = (dom.Element) metaData.getAsTree(formatNames[0]);
  518. XmlDocument _metadataDocument = new XmlDocument();
  519. XmlConvert(rootNode, _metadataDocument);
  520. return _metadataDocument;
  521. }
  522. private void XmlConvert(dom.Node jNode, XmlNode nNode) {
  523. XmlDocument document = nNode.OwnerDocument;
  524. if (document == null)
  525. document = (XmlDocument)nNode;
  526. XmlNode n = null;
  527. switch (jNode.getNodeType()) {
  528. case 1 :
  529. n = document.CreateNode(XmlNodeType.Element, jNode.getNodeName(), jNode.getNamespaceURI());
  530. break;
  531. case 4 :
  532. n = document.CreateNode(XmlNodeType.CDATA, jNode.getNodeName(), jNode.getNamespaceURI());
  533. break;
  534. default:
  535. return;
  536. }
  537. //set value
  538. n.InnerText = jNode.getNodeValue();
  539. nNode.AppendChild( n );
  540. //copy attributes
  541. org.w3c.dom.NamedNodeMap nm = jNode.getAttributes();
  542. for (int i=0; i<nm.getLength(); i++) {
  543. XmlAttribute a = document.CreateAttribute( nm.item(i).getNodeName() );
  544. a.Value = nm.item(i).getNodeValue();
  545. n.Attributes.Append( a );
  546. }
  547. //copy childs
  548. org.w3c.dom.NodeList nl = jNode.getChildNodes();
  549. for (int i=0; i<nl.getLength(); i++) {
  550. XmlConvert(nl.item(i), n);
  551. }
  552. }
  553. protected virtual float ParseFloatValue(string strValue) {
  554. try {
  555. if ((strValue != null) && (strValue != "")) {
  556. int dividerPos = strValue.IndexOf("/");
  557. if (dividerPos < 0) {
  558. return float.Parse(strValue);
  559. }
  560. else {
  561. return float.Parse(strValue.Substring( 0, dividerPos )) /
  562. float.Parse(strValue.Substring( dividerPos + 1 ));
  563. }
  564. }
  565. return float.NaN;
  566. }
  567. catch (Exception) {
  568. return float.NaN;
  569. }
  570. }
  571. #endregion
  572. #region IDisposable members
  573. public void Dispose() {
  574. if (NativeReader != null) {
  575. NativeReader.dispose();
  576. NativeReader = null;
  577. }
  578. if (NativeWriter != null) {
  579. NativeWriter.dispose();
  580. NativeWriter = null;
  581. }
  582. }
  583. #endregion
  584. }
  585. }