ImageCodec.jvm.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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. ici.CodecName = rw.getDescription (java.util.Locale.getDefault ());
  181. //ici.DllName = null;
  182. foreach (string suffix in rw.getFileSuffixes ()) {
  183. if (ici.FilenameExtension != null)
  184. ici.FilenameExtension += ";";
  185. ici.FilenameExtension += "*."+suffix;
  186. }
  187. ici.Flags = ImageCodecFlags.Builtin|ImageCodecFlags.SupportBitmap;
  188. if (rw is spi.ImageReaderSpi)
  189. ici.Flags |= ImageCodecFlags.Decoder;
  190. if (rw is spi.ImageWriterSpi)
  191. ici.Flags |= ImageCodecFlags.Encoder;
  192. ici.FormatDescription = string.Join(";",
  193. rw.getFormatNames());
  194. try {
  195. ici.Version = (int)Convert.ToDouble(rw.getVersion ());
  196. }
  197. catch (Exception) {
  198. ici.Version = 1;
  199. }
  200. break;
  201. }
  202. return ici;
  203. }
  204. #endregion
  205. internal Hashtable Iterate () {
  206. // TBD: Insert Exception handling here
  207. NameValueCollection nvc = (NameValueCollection) System.Configuration.ConfigurationSettings
  208. .GetConfig ("system.drawing/codecs");
  209. Hashtable codecs = new Hashtable (10);
  210. for (int i=0; i<nvc.Count; i++) {
  211. Guid clsid = new Guid (nvc.GetKey (i));
  212. ImageFormat format = ClsidToImageFormat (clsid);
  213. ImageCodecInfo codec = ProcessOneCodec (clsid, format.Guid, nvc[i]);
  214. if ((codec != null) && (codec.FilenameExtension != null))
  215. codecs [clsid] = codec;
  216. }
  217. return codecs;
  218. }
  219. }
  220. class ReaderSpiIterator: BaseSpiIterator {
  221. protected override java.util.Iterator GetIterator(string mimeType) {
  222. return imageio.ImageIO.getImageReadersByMIMEType (mimeType);
  223. }
  224. protected override javax.imageio.spi.ImageReaderWriterSpi GetNext(java.util.Iterator iter) {
  225. imageio.ImageReader r = (imageio.ImageReader) iter.next ();
  226. return r.getOriginatingProvider ();
  227. }
  228. }
  229. class WriterSpiIterator: BaseSpiIterator {
  230. protected override java.util.Iterator GetIterator(string mimeType) {
  231. return imageio.ImageIO.getImageWritersByMIMEType (mimeType);
  232. }
  233. protected override javax.imageio.spi.ImageReaderWriterSpi GetNext(java.util.Iterator iter) {
  234. imageio.ImageWriter w = (imageio.ImageWriter) iter.next ();
  235. return w.getOriginatingProvider ();
  236. }
  237. }
  238. #endregion
  239. #region Clsid and FormatID
  240. static Guid BmpClsid = new Guid ("557cf400-1a04-11d3-9a73-0000f81ef32e");
  241. static Guid JpegClsid = new Guid ("557cf401-1a04-11d3-9a73-0000f81ef32e");
  242. static Guid GifClsid = new Guid ("557cf402-1a04-11d3-9a73-0000f81ef32e");
  243. static Guid EmfClsid = new Guid ("557cf403-1a04-11d3-9a73-0000f81ef32e");
  244. static Guid WmfClsid = new Guid ("557cf404-1a04-11d3-9a73-0000f81ef32e");
  245. static Guid TiffClsid = new Guid ("557cf405-1a04-11d3-9a73-0000f81ef32e");
  246. static Guid PngClsid = new Guid ("557cf406-1a04-11d3-9a73-0000f81ef32e");
  247. static Guid IconClsid = new Guid ("557cf407-1a04-11d3-9a73-0000f81ef32e");
  248. private static ImageFormat MimeTypesToImageFormat (string [] mimeTypes) {
  249. foreach (ImageCodecInfo codec in Decoders.Values)
  250. for (int i=0; i<mimeTypes.Length; i++)
  251. if (codec.MimeType == mimeTypes [i])
  252. return new ImageFormat (codec.FormatID);
  253. return null;
  254. }
  255. internal static ImageFormat ClsidToImageFormat (Guid clsid) {
  256. if (clsid.Equals (BmpClsid))
  257. return ImageFormat.Bmp;
  258. else if (clsid.Equals (JpegClsid))
  259. return ImageFormat.Jpeg;
  260. else if (clsid.Equals (GifClsid))
  261. return ImageFormat.Gif;
  262. else if (clsid.Equals (EmfClsid))
  263. return ImageFormat.Emf;
  264. else if (clsid.Equals (WmfClsid))
  265. return ImageFormat.Wmf;
  266. else if (clsid.Equals (TiffClsid))
  267. return ImageFormat.Tiff;
  268. else if (clsid.Equals (PngClsid))
  269. return ImageFormat.Png;
  270. else if (clsid.Equals (IconClsid))
  271. return ImageFormat.Icon;
  272. else
  273. return null;
  274. }
  275. internal static Guid ImageFormatToClsid (ImageFormat format) {
  276. if (format == null)
  277. return Guid.Empty;
  278. if (format.Guid.Equals (ImageFormat.Bmp.Guid))
  279. return BmpClsid;
  280. else if (format.Guid.Equals (ImageFormat.Jpeg.Guid))
  281. return JpegClsid;
  282. else if (format.Guid.Equals (ImageFormat.Gif))
  283. return GifClsid;
  284. else if (format.Guid.Equals (ImageFormat.Emf.Guid))
  285. return EmfClsid;
  286. else if (format.Guid.Equals (ImageFormat.Wmf.Guid))
  287. return WmfClsid;
  288. else if (format.Guid.Equals (ImageFormat.Tiff.Guid))
  289. return TiffClsid;
  290. else if (format.Guid.Equals (ImageFormat.Png.Guid))
  291. return PngClsid;
  292. else if (format.Guid.Equals (ImageFormat.Icon.Guid))
  293. return IconClsid;
  294. else
  295. return Guid.Empty;
  296. }
  297. private FrameDimension FormatFrameDimesion {
  298. get {
  299. if (ImageFormat == null)
  300. return FrameDimension.Page;
  301. if (ImageFormat.Guid.Equals (ImageFormat.Bmp.Guid))
  302. return FrameDimension.Page;
  303. else if (ImageFormat.Guid.Equals (ImageFormat.Jpeg.Guid))
  304. return FrameDimension.Page;
  305. else if (ImageFormat.Guid.Equals (ImageFormat.Gif))
  306. return FrameDimension.Time;
  307. else if (ImageFormat.Guid.Equals (ImageFormat.Emf.Guid))
  308. return FrameDimension.Page;
  309. else if (ImageFormat.Guid.Equals (ImageFormat.Wmf.Guid))
  310. return FrameDimension.Page;
  311. else if (ImageFormat.Guid.Equals (ImageFormat.Tiff.Guid))
  312. return FrameDimension.Page;
  313. else if (ImageFormat.Guid.Equals (ImageFormat.Png.Guid))
  314. return FrameDimension.Page;
  315. else if (ImageFormat.Guid.Equals (ImageFormat.Icon.Guid))
  316. return FrameDimension.Resolution;
  317. else
  318. return FrameDimension.Page;
  319. }
  320. }
  321. #endregion
  322. #region Image read/write methods
  323. internal PlainImage ReadPlainImage() {
  324. awt.Image img = ReadImage( _currentFrame );
  325. if (img == null)
  326. return null;
  327. // its possible to fail to load thumbnails and metadata, but image is ok.
  328. awt.Image [] th = null;
  329. #if THUMBNAIL_SUPPORTED
  330. try {
  331. th = ReadThumbnails( _currentFrame );
  332. }
  333. catch (Exception) {}
  334. #endif
  335. XmlDocument md = null;
  336. imageio.metadata.IIOMetadata nativeMd = null;
  337. try {
  338. nativeMd = ReadImageMetadata( _currentFrame );
  339. md = ConvertImageMetadata( nativeMd );
  340. }
  341. catch (Exception) {}
  342. float [] resolution = GetResolution( md );
  343. PlainImage pi = new PlainImage( img, th, ImageFormat, resolution[0], resolution[1], FormatFrameDimesion );
  344. pi.NativeMetadata = nativeMd;
  345. return pi;
  346. }
  347. internal PlainImage ReadNextPlainImage() {
  348. _currentFrame++;
  349. return ReadPlainImage();
  350. }
  351. private awt.Image ReadImage(int frame) {
  352. if (NativeStream == null)
  353. throw new Exception("Input stream not specified");
  354. try {
  355. return NativeReader.read (frame);
  356. }
  357. catch (java.lang.IndexOutOfBoundsException) {
  358. return null;
  359. }
  360. catch (java.io.IOException ex) {
  361. throw new System.IO.IOException(ex.Message, ex);
  362. }
  363. }
  364. #if THUMBNAIL_SUPPORTED
  365. private awt.Image [] ReadThumbnails(int frameIndex) {
  366. awt.Image [] thArray = null;
  367. try {
  368. if (NativeReader.readerSupportsThumbnails()) {
  369. int tmbNumber = NativeReader.getNumThumbnails(frameIndex);
  370. if (tmbNumber > 0) {
  371. thArray = new awt.Image[ tmbNumber ];
  372. for (int i = 0; i < tmbNumber; i++) {
  373. thArray[i] = NativeReader.readThumbnail(frameIndex, i);
  374. }
  375. }
  376. }
  377. return thArray;
  378. }
  379. catch (java.io.IOException ex) {
  380. throw new System.IO.IOException(ex.Message, ex);
  381. }
  382. }
  383. #endif
  384. internal void WritePlainImage(PlainImageCollection pic) {
  385. if ((pic == null) || (pic.Count == 0))
  386. return;
  387. if (pic.Count == 1) {
  388. WritePlainImage( pic[0] );
  389. return;
  390. }
  391. try {
  392. if (NativeWriter.canWriteSequence ()) {
  393. NativeWriter.prepareWriteSequence (null);
  394. for (int i=0; i < pic.Count; i++) {
  395. imageio.IIOImage iio = GetIIOImageContainer( pic[i] );
  396. NativeWriter.writeToSequence (iio, null);
  397. }
  398. NativeWriter.endWriteSequence ();
  399. }
  400. else
  401. WritePlainImage( pic[0] );
  402. }
  403. catch (java.io.IOException ex) {
  404. throw new System.IO.IOException(ex.Message, ex);
  405. }
  406. }
  407. internal void WritePlainImage(PlainImage pi) {
  408. try {
  409. imageio.IIOImage iio = GetIIOImageContainer( pi );
  410. WriteImage( iio );
  411. }
  412. catch (java.io.IOException ex) {
  413. throw new System.IO.IOException(ex.Message, ex);
  414. }
  415. }
  416. private void WriteImage(imageio.IIOImage iio) {
  417. if (NativeStream == null)
  418. throw new Exception("Output stream not specified");
  419. NativeWriter.write( iio );
  420. }
  421. private imageio.IIOImage GetIIOImageContainer(PlainImage pi) {
  422. java.util.ArrayList al = null;
  423. // prepare thumbnails list
  424. if (pi.Thumbnails != null) {
  425. al = new java.util.ArrayList( pi.Thumbnails.Length );
  426. for (int i=0; i < pi.Thumbnails.Length; i++)
  427. al.add(pi.Thumbnails[i]);
  428. }
  429. // prepare IIOImage container
  430. if (pi.NativeImage is image.BufferedImage) {
  431. imageio.IIOImage iio = new javax.imageio.IIOImage(
  432. (image.BufferedImage)pi.NativeImage, al, null /*pi.NativeMetadata*/);
  433. return iio;
  434. }
  435. else
  436. // TBD: This codec is for raster formats only
  437. throw new NotSupportedException("Only raster formats are supported");
  438. }
  439. private imageio.metadata.IIOMetadata ReadImageMetadata(int frameIndex) {
  440. if (NativeStream == null)
  441. throw new Exception("Input stream not specified");
  442. try {
  443. imageio.metadata.IIOMetadata md = NativeReader.getImageMetadata( frameIndex );
  444. return md;
  445. }
  446. catch (java.io.IOException ex) {
  447. throw new System.IO.IOException(ex.Message, ex);
  448. }
  449. }
  450. #endregion
  451. #region Extra properties
  452. public ImageFormat ImageFormat {
  453. get { return _imageFormat; }
  454. }
  455. #endregion
  456. #region Metadata parse
  457. private float [] GetResolution(XmlDocument metaData) {
  458. if (metaData == null)
  459. return new float[]{0, 0};
  460. ResolutionConfigurationCollection rcc =
  461. (ResolutionConfigurationCollection)
  462. ConfigurationSettings.GetConfig("system.drawing/codecsmetadata");
  463. if (rcc == null)
  464. throw new ConfigurationException("Configuration section codecsmetadata not found");
  465. ResolutionConfiguration rc = rcc[ ImageFormat.ToString() ];
  466. if (rc == null)
  467. return new float[]{0, 0};
  468. // Horizontal resolution
  469. string xResPath = rc.XResPath;
  470. string xRes;
  471. if (xResPath == string.Empty)
  472. xRes = rc.XResDefault;
  473. else
  474. xRes = GetValueFromMetadata(metaData, xResPath);
  475. if ((xRes == null) || (xRes == string.Empty))
  476. xRes = rc.XResDefault;
  477. // Vertical resolution
  478. string yResPath = rc.YResPath;
  479. string yRes;
  480. if (yResPath == string.Empty)
  481. yRes = rc.YResDefault;
  482. else
  483. yRes = GetValueFromMetadata(metaData, yResPath);
  484. if ((yRes == null) || (yRes == string.Empty))
  485. yRes = rc.YResDefault;
  486. // Resolution units
  487. string resUnitsPath = rc.UnitsTypePath;
  488. string resUnitsType;
  489. if (resUnitsPath == string.Empty)
  490. resUnitsType = rc.UnitsTypeDefault;
  491. else
  492. resUnitsType = GetValueFromMetadata(metaData, resUnitsPath);
  493. if (resUnitsType == null)
  494. resUnitsType = rc.UnitsTypeDefault;
  495. // Unit scale
  496. string unitScale = rc.UnitsScale[resUnitsType].ToString();
  497. // Adjust resolution to its units
  498. float [] res = new float[2];
  499. res[0] = ParseFloatValue(xRes) * ParseFloatValue(unitScale);
  500. res[1] = ParseFloatValue(yRes) * ParseFloatValue(unitScale);
  501. return res;
  502. }
  503. private string GetValueFromMetadata(XmlDocument metaData, string path) {
  504. XmlNode n = metaData.SelectSingleNode(path);
  505. if (n == null)
  506. return null;
  507. return n.InnerText;
  508. }
  509. private XmlDocument ConvertImageMetadata(imageio.metadata.IIOMetadata metaData) {
  510. string [] formatNames = metaData.getMetadataFormatNames();
  511. dom.Element rootNode = (dom.Element) metaData.getAsTree(formatNames[0]);
  512. XmlDocument _metadataDocument = new XmlDocument();
  513. XmlConvert(rootNode, _metadataDocument);
  514. return _metadataDocument;
  515. }
  516. private void XmlConvert(dom.Node jNode, XmlNode nNode) {
  517. XmlDocument document = nNode.OwnerDocument;
  518. if (document == null)
  519. document = (XmlDocument)nNode;
  520. XmlNode n = null;
  521. switch (jNode.getNodeType()) {
  522. case 1 :
  523. n = document.CreateNode(XmlNodeType.Element, jNode.getNodeName(), jNode.getNamespaceURI());
  524. break;
  525. case 4 :
  526. n = document.CreateNode(XmlNodeType.CDATA, jNode.getNodeName(), jNode.getNamespaceURI());
  527. break;
  528. default:
  529. return;
  530. }
  531. //set value
  532. n.InnerText = jNode.getNodeValue();
  533. nNode.AppendChild( n );
  534. //copy attributes
  535. org.w3c.dom.NamedNodeMap nm = jNode.getAttributes();
  536. for (int i=0; i<nm.getLength(); i++) {
  537. XmlAttribute a = document.CreateAttribute( nm.item(i).getNodeName() );
  538. a.Value = nm.item(i).getNodeValue();
  539. n.Attributes.Append( a );
  540. }
  541. //copy childs
  542. org.w3c.dom.NodeList nl = jNode.getChildNodes();
  543. for (int i=0; i<nl.getLength(); i++) {
  544. XmlConvert(nl.item(i), n);
  545. }
  546. }
  547. protected virtual float ParseFloatValue(string strValue) {
  548. try {
  549. if ((strValue != null) && (strValue != "")) {
  550. int dividerPos = strValue.IndexOf("/");
  551. if (dividerPos < 0) {
  552. return float.Parse(strValue);
  553. }
  554. else {
  555. return float.Parse(strValue.Substring( 0, dividerPos )) /
  556. float.Parse(strValue.Substring( dividerPos + 1 ));
  557. }
  558. }
  559. return float.NaN;
  560. }
  561. catch (Exception) {
  562. return float.NaN;
  563. }
  564. }
  565. #endregion
  566. #region IDisposable members
  567. public void Dispose() {
  568. if (NativeReader != null) {
  569. NativeReader.dispose();
  570. NativeReader = null;
  571. }
  572. if (NativeWriter != null) {
  573. NativeWriter.dispose();
  574. NativeWriter = null;
  575. }
  576. }
  577. #endregion
  578. }
  579. }