-
-
Notifications
You must be signed in to change notification settings - Fork 381
/
Copy pathHtmlNode.Encapsulator.cs
946 lines (768 loc) · 38.3 KB
/
HtmlNode.Encapsulator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
// Description: Html Agility Pack - HTML Parsers, selectors, traversors, manupulators.
// Website & Documentation: http://html-agility-pack.net
// Forum & Issues: https://github.com/zzzprojects/html-agility-pack
// License: https://github.com/zzzprojects/html-agility-pack/blob/master/LICENSE
// More projects: http://www.zzzprojects.com/
// Copyright © ZZZ Projects Inc. 2014 - 2017. All rights reserved.
#if !METRO && !NETSTANDARD1_3
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Xml.XPath;
namespace HtmlAgilityPack
{
public partial class HtmlNode
{
/// <summary>
/// Fill an object and go through it's properties and fill them too.
/// </summary>
/// <typeparam name="T">Type of object to want to fill. It should have atleast one property that defined XPath.</typeparam>
/// <returns>Returns an object of type T including Encapsulated data.</returns>
/// <exception cref="ArgumentException">Why it's thrown.</exception>
/// <exception cref="ArgumentNullException">Why it's thrown.</exception>
/// <exception cref="MissingMethodException">Why it's thrown.</exception>
/// <exception cref="MissingXPathException">Why it's thrown.</exception>
/// <exception cref="XPathException">Why it's thrown.</exception>
/// <exception cref="NodeNotFoundException">Why it's thrown.</exception>
/// <exception cref="NodeAttributeNotFoundException">Why it's thrown.</exception>
/// <exception cref="FormatException">Why it's thrown.</exception>
/// <exception cref="Exception">Why it's thrown.</exception>
public T GetEncapsulatedData<T>()
{
return (T)GetEncapsulatedData(typeof(T), null);
}
/// <summary>
/// Fill an object and go through it's properties and fill them too.
/// </summary>
/// <typeparam name="T">Type of object to want to fill. It should have atleast one property that defined XPath.</typeparam>
/// <param name="htmlDocument">If htmlDocument includes data , leave this parameter null. Else pass your specific htmldocument.</param>
/// <returns>Returns an object of type T including Encapsulated data.</returns>
/// <exception cref="ArgumentException">Why it's thrown.</exception>
/// <exception cref="ArgumentNullException">Why it's thrown.</exception>
/// <exception cref="MissingMethodException">Why it's thrown.</exception>
/// <exception cref="MissingXPathException">Why it's thrown.</exception>
/// <exception cref="XPathException">Why it's thrown.</exception>
/// <exception cref="NodeNotFoundException">Why it's thrown.</exception>
/// <exception cref="NodeAttributeNotFoundException">Why it's thrown.</exception>
/// <exception cref="FormatException">Why it's thrown.</exception>
/// <exception cref="Exception">Why it's thrown.</exception>
public T GetEncapsulatedData<T>(HtmlDocument htmlDocument)
{
return (T)GetEncapsulatedData(typeof(T), htmlDocument);
}
/// <summary>
/// Fill an object and go through it's properties and fill them too.
/// </summary>
/// <param name="targetType">Type of object to want to fill. It should have atleast one property that defined XPath.</param>
/// <param name="htmlDocument">If htmlDocument includes data , leave this parameter null. Else pass your specific htmldocument.</param>
/// <returns>Returns an object of type targetType including Encapsulated data.</returns>
/// <exception cref="ArgumentException">Why it's thrown.</exception>
/// <exception cref="ArgumentNullException">Why it's thrown.</exception>
/// <exception cref="MissingMethodException">Why it's thrown.</exception>
/// <exception cref="MissingXPathException">Why it's thrown.</exception>
/// <exception cref="XPathException">Why it's thrown.</exception>
/// <exception cref="NodeNotFoundException">Why it's thrown.</exception>
/// <exception cref="NodeAttributeNotFoundException">Why it's thrown.</exception>
/// <exception cref="FormatException">Why it's thrown.</exception>
/// <exception cref="Exception">Why it's thrown.</exception>
public object GetEncapsulatedData(Type targetType, HtmlDocument htmlDocument = null)
{
#region SettingPrerequisite
if (targetType == null)
{
throw new ArgumentNullException("Parameter targetType is null");
}
HtmlDocument source;
if (htmlDocument == null)
{
source = OwnerDocument;
}
else
{
source = htmlDocument;
}
object targetObject;
if (targetType.IsInstantiable() == false) // if it can not create instanse of T because of lack of constructor in type T.
{
throw new MissingMethodException("Parameterless Constructor excpected for " + targetType.FullName);
}
else
{
targetObject = Activator.CreateInstance(targetType);
}
#endregion SettingPrerequisite
#region targetObject_Defined_XPath
if (targetType.IsDefinedAttribute(typeof(HasXPathAttribute)) == true) // Object has xpath attribute (Defined HasXPath)
{
// Store list of properties that defined xpath attribute
IEnumerable<PropertyInfo> validProperties = targetType.GetPropertiesDefinedXPath();
if (validProperties.CountOfIEnumerable() == 0) // if no XPath property exist in type T while T defined HasXpath attribute.
{
throw new MissingXPathException("Type " + targetType.FullName +
" defined HasXPath Attribute but it does not have any property with XPath Attribte.");
}
else
{
// Fill targetObject variable Properties ( T targetObject )
foreach (PropertyInfo propertyInfo in validProperties)
{
// Get xpath attribute from valid properties
// for .Net old versions:
XPathAttribute xPathAttribute = (propertyInfo.GetCustomAttributes(typeof(XPathAttribute), false) as IList)[0] as XPathAttribute;
#region Property_IsNOT_IEnumerable
if (propertyInfo.IsIEnumerable() == false) // Property is None-IEnumerable
{
HtmlNode htmlNode = null;
// try to fill htmlNode based on XPath given
try
{
htmlNode = source.DocumentNode.SelectSingleNode(xPathAttribute.XPath);
}
catch (XPathException ex) // if it can not select node based on given xpath
{
throw new XPathException(ex.Message + " That means you have a syntax error in XPath property of this Property : " +
propertyInfo.PropertyType.FullName + " " + propertyInfo.Name);
}
catch (Exception ex)
{
throw new NodeNotFoundException("Cannot find node with giving XPath to bind to " +
propertyInfo.PropertyType.FullName + " " + propertyInfo.Name, ex);
}
if (htmlNode == null) // If Encapsulator could not find Node.
{
if (propertyInfo.IsDefined(typeof(SkipNodeNotFoundAttribute), false) == true)
{
// set default value.
//throw new Exception("Okey !");
}
else
{
throw new NodeNotFoundException("Cannot find node with giving XPath to bind to " +
propertyInfo.PropertyType.FullName + " " + propertyInfo.Name);
}
}
else // if htmlNode is not null (Encapsulator find the Node)
{
#region Property_Is_HasXPath_UserDefinedClass
// Property is None-IEnumerable HasXPath-user-defined class
if (propertyInfo.PropertyType.IsDefinedAttribute(typeof(HasXPathAttribute)) == true)
{
HtmlDocument innerHtmlDocument = new HtmlDocument();
innerHtmlDocument.LoadHtml(Tools.GetHtmlForEncapsulation(htmlNode, xPathAttribute.NodeReturnType));
object o = GetEncapsulatedData(propertyInfo.PropertyType, innerHtmlDocument);
propertyInfo.SetValue(targetObject, o, null);
}
#endregion Property_Is_HasXPath_UserDefinedClass
#region Property_Is_SimpleType
// Property is None-IEnumerable value-type or .Net class or user-defined class.
// AND does not deifned xpath and shouldn't have property that defined xpath.
else
{
string result = string.Empty;
if (xPathAttribute.AttributeName == null) // It target value of HTMLTag
{
result = Tools.GetNodeValueBasedOnXPathReturnType<string>(htmlNode, xPathAttribute);
}
else // It target attribute of HTMLTag
{
result = htmlNode.GetAttributeValue(xPathAttribute.AttributeName, null);
}
if (result == null)
{
throw new NodeAttributeNotFoundException("Can not find " +
xPathAttribute.AttributeName + " Attribute in " + htmlNode.Name +
" related to " + propertyInfo.PropertyType.FullName + " " + propertyInfo.Name);
}
object resultCastedToTargetPropertyType;
try
{
resultCastedToTargetPropertyType = Convert.ChangeType(result, propertyInfo.PropertyType);
}
catch (FormatException)
{
throw new FormatException("Can not convert Invalid string to " + propertyInfo.PropertyType.FullName + " " + propertyInfo.Name);
}
catch (Exception ex)
{
throw new Exception("Unhandled Exception : " + ex.Message);
}
propertyInfo.SetValue(targetObject, resultCastedToTargetPropertyType, null);
}
#endregion Property_Is_SimpleType
}
}
#endregion Property_IsNOT_IEnumerable
#region Property_Is_IEnumerable
else // Property is IEnumerable<T>
{
IList<Type> T_Types = propertyInfo.GetGenericTypes() as IList<Type>; // Get T type
if (T_Types == null || T_Types.Count == 0)
{
throw new ArgumentException(propertyInfo.Name + " should have one generic argument.");
}
else if (T_Types.Count > 1)
{
throw new ArgumentException(propertyInfo.Name + " should have one generic argument.");
}
else if (T_Types.Count == 1) // It is NOT something like Dictionary<Tkey , Tvalue>
{
HtmlNodeCollection nodeCollection;
// try to fill nodeCollection based on given xpath.
try
{
nodeCollection = source.DocumentNode.SelectNodes(xPathAttribute.XPath);
}
catch (XPathException ex)
{
throw new XPathException(ex.Message + " That means you have a syntax error in XPath property of this Property : " +
propertyInfo.PropertyType.FullName + " " + propertyInfo.Name);
}
catch (Exception ex)
{
throw new NodeNotFoundException("Cannot find node with giving XPath to bind to " +
propertyInfo.PropertyType.FullName + " " + propertyInfo.Name, ex);
}
if (nodeCollection == null || nodeCollection.Count == 0)
{
if (propertyInfo.IsDefined(typeof(SkipNodeNotFoundAttribute), false) == true)
{
// set default value.
//throw new Exception("Okey !");
}
else
{
throw new NodeNotFoundException("Cannot find node with giving XPath to bind to " +
propertyInfo.PropertyType.FullName + " " + propertyInfo.Name);
}
}
else
{
IList result = T_Types[0].CreateIListOfType();
#region Property_Is_IEnumerable<HasXPath-UserDefinedClass>
if (T_Types[0].IsDefinedAttribute(typeof(HasXPathAttribute)) == true) // T is IEnumerable HasXPath-user-defined class (T type Defined XPath properties)
{
foreach (HtmlNode node in nodeCollection)
{
HtmlDocument innerHtmlDocument = new HtmlDocument();
innerHtmlDocument.LoadHtml(Tools.GetHtmlForEncapsulation(node, xPathAttribute.NodeReturnType));
object o = GetEncapsulatedData(T_Types[0], innerHtmlDocument);
result.Add(o);
}
}
#endregion Property_Is_IEnumerable<HasXPath-UserDefinedClass>
#region Property_Is_IEnumerable<SimpleClass>
else // T is value-type or .Net class or user-defined class ( without xpath )
{
if (xPathAttribute.AttributeName == null) // It target value
{
try
{
result = Tools.GetNodesValuesBasedOnXPathReturnType(nodeCollection, xPathAttribute, T_Types[0]);
}
catch (FormatException)
{
throw new FormatException("Can not convert Invalid string in node collection to " + T_Types[0].FullName + " " + propertyInfo.Name);
}
catch (Exception ex)
{
throw new Exception("Unhandled Exception : " + ex.Message);
}
}
else // It target attribute
{
foreach (HtmlNode node in nodeCollection)
{
string nodeAttributeValue = node.GetAttributeValue(xPathAttribute.AttributeName, null);
if (nodeAttributeValue == null)
{
throw new NodeAttributeNotFoundException("Can not find " + xPathAttribute.AttributeName + " Attribute in " + node.Name + " related to " +
propertyInfo.PropertyType.FullName + " " + propertyInfo.Name);
}
object resultCastedToTargetPropertyType;
try
{
resultCastedToTargetPropertyType = Convert.ChangeType(nodeAttributeValue, T_Types[0]);
}
catch (FormatException) // if it can not cast result(string) to type of property.
{
throw new FormatException("Can not convert Invalid string to " + T_Types[0].FullName + " " + propertyInfo.Name);
}
catch (Exception ex)
{
throw new Exception("Unhandled Exception : " + ex.Message);
}
result.Add(resultCastedToTargetPropertyType);
}
}
}
#endregion Property_Is_IEnumerable<SimpleClass>
if (result == null || result.Count == 0)
{
throw new Exception("Cannot fill " + propertyInfo.PropertyType.FullName + " " + propertyInfo.Name + " because it is null.");
}
propertyInfo.SetValue(targetObject, result, null);
}
}
}
#endregion Property_Is_IEnumerable
}
return targetObject;
}
}
#endregion targetObject_Defined_XPath
#region targetObject_NOTDefined_XPath
else // Object doesen't have xpath attribute
{
throw new MissingXPathException("Type T must define HasXPath attribute and include properties with XPath attribute.");
}
#endregion targetObject_NOTDefined_XPath
}
}
/// <summary>
/// Includes tools that GetEncapsulatedData method uses them.
/// </summary>
internal static class Tools
{
/// <summary>
/// Determine if a type define an attribute or not , supporting both .NetStandard and .NetFramework2.0
/// </summary>
/// <param name="type">Type you want to test it.</param>
/// <param name="attributeType">Attribute that type must have or not.</param>
/// <returns>If true , The type parameter define attributeType parameter.</returns>
internal static bool IsDefinedAttribute(this Type type, Type attributeType)
{
if (type == null)
{
throw new ArgumentNullException("Parameter type is null when checking type defined attributeType or not.");
}
if (attributeType == null)
{
throw new ArgumentNullException("Parameter attributeType is null when checking type defined attributeType or not.");
}
#if !(NETSTANDARD1_3 || NETSTANDARD1_6)
if (type.IsDefined(attributeType, false) == true)
{
return true;
}
else
{
return false;
}
#endif
#if NETSTANDARD1_3 || NETSTANDARD1_6
if (type.GetTypeInfo().IsDefined(attributeType) == true)
{
return true;
}
else
{
return false;
}
#endif
throw new Exception("Can't Target any platform when checking " + type.FullName + " is a " + attributeType.FullName + " or not.");
}
/// <summary>
/// Retrive properties of type that defined <see cref="XPathAttribute"/>.
/// </summary>
/// <param name="type">Type that you want to find it's XPath-Defined properties.</param>
/// <returns>IEnumerable of property infos of a type , that defined specific attribute.</returns>
internal static IEnumerable<PropertyInfo> GetPropertiesDefinedXPath(this Type type)
{
if (type == null)
{
throw new ArgumentNullException("Parameter type is null while retrieving properties defined XPathAttribute of Type type.");
}
PropertyInfo[] properties = null;
#if !(NETSTANDARD1_3 || NETSTANDARD1_6)
properties = type.GetProperties();
#endif
#if NETSTANDARD1_3 || NETSTANDARD1_6
properties = type.GetTypeInfo().GetProperties();
#endif
return properties.HAPWhere(x => x.IsDefined(typeof(XPathAttribute), false) == true);
throw new Exception("Can't Target any platform while retrieving properties defined XPathAttribute of Type type.");
}
/// <summary>
/// Determine if a <see cref="PropertyInfo"/> has implemented <see cref="IEnumerable"/> BUT <see cref="string"/> is considered as NONE-IEnumerable !
/// </summary>
/// <param name="propertyInfo">The property info you want to test.</param>
/// <returns>True if property info is IEnumerable.</returns>
internal static bool IsIEnumerable(this PropertyInfo propertyInfo)
{
//return propertyInfo.PropertyType.GetInterface(typeof(IEnumerable<>).FullName) != null;
if (propertyInfo == null)
{
throw new ArgumentNullException("Parameter propertyInfo is null while checking propertyInfo for being IEnumerable or not.");
}
if (propertyInfo.PropertyType == typeof(string))
{
return false;
}
else
{
#if !(NETSTANDARD1_3 || NETSTANDARD1_6)
return typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType);
#endif
#if NETSTANDARD1_3 || NETSTANDARD1_6
return typeof(IEnumerable).GetTypeInfo().IsAssignableFrom(propertyInfo.PropertyType);
#endif
throw new Exception("Can't Target any platform while checking propertyInfo for being IEnumerable or not.");
}
}
/// <summary>
/// Returns T type(first generic type) of <see cref="IEnumerable{T}"/> or <see cref="List{T}"/>.
/// </summary>
/// <param name="propertyInfo">IEnumerable-Implemented property</param>
/// <returns>List of generic types.</returns>
internal static IEnumerable<Type> GetGenericTypes(this PropertyInfo propertyInfo)
{
if (propertyInfo == null)
{
throw new ArgumentNullException("Parameter propertyInfo is null while Getting generic types of Property.");
}
#if !(NETSTANDARD1_3 || NETSTANDARD1_6)
return propertyInfo.PropertyType.GetGenericArguments();
#endif
#if NETSTANDARD1_3 || NETSTANDARD1_6
return propertyInfo.PropertyType.GetTypeInfo().GetGenericArguments();
#endif
throw new Exception("Can't Target any platform while Getting generic types of Property.");
}
/// <summary>
/// Find and Return a mehtod that defined in a class by it's name.
/// </summary>
/// <param name="type">Type of class include requested method.</param>
/// <param name="methodName">Name of requested method as string.</param>
/// <returns>Method info of requested method.</returns>
internal static MethodInfo GetMethodByItsName(this Type type, string methodName)
{
if (type == null)
{
throw new ArgumentNullException("Parameter type is null while Getting method from it.");
}
if (methodName == null || methodName == "")
{
throw new ArgumentNullException("Parameter methodName is null while Getting method from Type type.");
}
#if !(NETSTANDARD1_3 || NETSTANDARD1_6)
return type.GetMethod(methodName);
#endif
#if NETSTANDARD1_3 || NETSTANDARD1_6
return type.GetTypeInfo().GetMethod(methodName);
#endif
throw new Exception("Can't Target any platform while getting Method methodName from Type type.");
}
/// <summary>
/// Create <see cref="IList"/> of given type.
/// </summary>
/// <param name="type">Type that you want to make a List of it.</param>
/// <returns>Returns IList of given type.</returns>
internal static IList CreateIListOfType(this Type type)
{
if (type == null)
{
throw new ArgumentNullException("Parameter type is null while creating List<type>.");
}
Type listType = typeof(List<>);
Type constructedListType = listType.MakeGenericType(type);
return Activator.CreateInstance(constructedListType) as IList;
}
/// <summary>
/// Returns the part of value of <see cref="HtmlNode"/> you want as .
/// </summary>
/// <param name="htmlNode">A htmlNode instance.</param>
/// <param name="xPathAttribute">Attribute that includes ReturnType</param>
/// <returns>String that choosen from HtmlNode as result.</returns>
internal static T GetNodeValueBasedOnXPathReturnType<T>(HtmlNode htmlNode, XPathAttribute xPathAttribute)
{
if (htmlNode == null)
{
throw new ArgumentNullException("parameter html node is null");
}
if (xPathAttribute == null)
{
throw new ArgumentNullException("parameter xpathAttribute is null");
}
return (T)Convert.ChangeType(GetHtmlForEncapsulation(htmlNode, xPathAttribute.NodeReturnType), typeof(T));
}
/// <summary>
/// Returns parts of values of <see cref="HtmlNode"/> you want as <see cref="IList{T}"/>.
/// </summary>
/// <param name="htmlNodeCollection"><see cref="HtmlNodeCollection"/> that you want to retrive each <see cref="HtmlNode"/> value.</param>
/// <param name="xPathAttribute">A <see cref="XPathAttribute"/> instnce incules <see cref="ReturnType"/>.</param>
/// <param name="listGenericType">Type of IList generic you want.</param>
/// <returns></returns>
internal static IList GetNodesValuesBasedOnXPathReturnType(HtmlNodeCollection htmlNodeCollection, XPathAttribute xPathAttribute, Type listGenericType)
{
if (htmlNodeCollection == null || htmlNodeCollection.Count == 0)
{
throw new ArgumentNullException("parameter htmlNodeCollection is null or empty.");
}
if (xPathAttribute == null)
{
throw new ArgumentNullException("parameter xpathAttribute is null");
}
IList result = listGenericType.CreateIListOfType();
foreach (HtmlNode node in htmlNodeCollection)
{
result.Add(Convert.ChangeType(GetHtmlForEncapsulation(node, xPathAttribute.NodeReturnType), listGenericType));
}
return result;
}
/// <summary>
/// Simulate Func method to use in Lambada Expression.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="arg"></param>
/// <returns></returns>
internal delegate TResult HAPFunc<T, TResult>(T arg);
/// <summary>
/// This method works like Where method in LINQ.
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <param name="source"></param>
/// <param name="predicate"></param>
/// <returns></returns>
internal static IEnumerable<TSource> HAPWhere<TSource>(this IEnumerable<TSource> source, HAPFunc<TSource, bool> predicate)
{
foreach (TSource item in source)
{
if (predicate(item))
{
yield return item;
}
}
}
/// <summary>
/// Check if the type can instantiated.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
internal static bool IsInstantiable(this Type type)
{
if (type == null)
{
throw new ArgumentNullException("type is null");
}
#if !(NETSTANDARD1_3 || NETSTANDARD1_6)
// checking for having parameterless constructor.
if (type.GetConstructor(Type.EmptyTypes) == null)
{
return false;
}
else
{
return true;
}
#endif
#if NETSTANDARD1_3 || NETSTANDARD1_6
// checking for having parameterless constructor.
if (type.GetTypeInfo().DeclaredConstructors.HAPWhere(x => x.GetParameters().Length == 0).CountOfIEnumerable() == 0)
{
return false;
}
else
{
return true;
}
#endif
throw new Exception("Can't Target any platform while getting Method methodName from Type type.");
}
/// <summary>
/// Returns count of elements stored in IEnumerable of T
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <returns></returns>
internal static int CountOfIEnumerable<T>(this IEnumerable<T> source)
{
if (source == null)
{
throw new ArgumentNullException("Parameter source is null while counting the IEnumerable");
}
int counter = 0;
foreach (T item in source)
{
counter++;
}
return counter;
}
/// <summary>
/// Return html part of <see cref="HtmlNode"/> based on <see cref="ReturnType"/>
/// </summary>
/// <param name="node">A htmlNode instance.</param>
/// <param name="returnType"><see cref="ReturnType"/></param>
/// <returns>Html part</returns>
/// <exception cref="IndexOutOfRangeException">Out of range to the <see cref="ReturnType"/></exception>
internal static string GetHtmlForEncapsulation(HtmlNode node, ReturnType returnType)
{
switch (returnType)
{
case ReturnType.InnerText:
return node.InnerText;
case ReturnType.InnerHtml:
return node.InnerHtml;
case ReturnType.OuterHtml:
return node.OuterHtml;
default:
throw new IndexOutOfRangeException("Unhandled ReturnType : " + returnType.ToString());
};
}
}
/// <summary>
/// Specify which part of <see cref="HtmlNode"/> is requested.
/// </summary>
public enum ReturnType
{
/// <summary>
/// The text between the start and end tags of the object.
/// </summary>
InnerText,
/// <summary>
/// The HTML between the start and end tags of the object
/// </summary>
InnerHtml,
/// <summary>
/// The object and its content in HTML
/// </summary>
OuterHtml
}
/// <summary>
/// Just mark and flag classes to show they have properties that defined <see cref="XPathAttribute"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
public sealed class HasXPathAttribute : Attribute
{
}
/// <summary>
/// Includes XPath and <see cref="NodeReturnType"/>. XPath for finding html tags and <see cref="NodeReturnType"/> for specify which part of <see cref="HtmlNode"/> you want to return.
/// </summary>
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class XPathAttribute : Attribute
{
/// <summary>
/// XPath Expression that is used to find related html node.
/// </summary>
public string XPath { get; }
/// <summary>
/// Html Attribute name
/// </summary>
public string AttributeName { get; set; }
/// <summary>
/// The methode of output
/// </summary>
public ReturnType NodeReturnType { get; set; }
/// <summary>
/// Specify Xpath to find related Html Node.
/// </summary>
/// <param name="xpathString"></param>
public XPathAttribute(string xpathString)
{
XPath = xpathString;
NodeReturnType = ReturnType.InnerText;
}
/// <summary>
/// Specify Xpath to find related Html Node.
/// </summary>
/// <param name="xpathString"></param>
/// <param name="nodeReturnType">Specify you want the output include html text too.</param>
public XPathAttribute(string xpathString, ReturnType nodeReturnType)
{
XPath = xpathString;
NodeReturnType = nodeReturnType;
}
/// <summary>
/// Specify Xpath and Attribute to find related Html Node and its attribute value.
/// </summary>
/// <param name="xpathString"></param>
/// <param name="attributeName"></param>
public XPathAttribute(string xpathString, string attributeName)
{
XPath = xpathString;
AttributeName = attributeName;
}
}
/// <summary>
/// Tagging a property with this Attribute make Encapsulator to ignore that property if it causes an error.
/// </summary>
[AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
public sealed class SkipNodeNotFoundAttribute : Attribute
{
}
/// <summary>
/// Exception that often occures when there is no way to bind a XPath to a Html Tag.
/// </summary>
public class NodeNotFoundException : Exception
{
/// <summary>
///
/// </summary>
public NodeNotFoundException() { }
/// <summary>
///
/// </summary>
/// <param name="message"></param>
public NodeNotFoundException(string message) : base(message) { }
/// <summary>
///
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
public NodeNotFoundException(string message, Exception inner) : base(message, inner) { }
}
/// <summary>
/// Exception that often occures when there is no way to bind a XPath to a HtmlTag Attribute.
/// </summary>
public class NodeAttributeNotFoundException : Exception
{
/// <summary>
///
/// </summary>
public NodeAttributeNotFoundException() { }
/// <summary>
///
/// </summary>
/// <param name="message"></param>
public NodeAttributeNotFoundException(string message) : base(message) { }
/// <summary>
///
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
public NodeAttributeNotFoundException(string message, Exception inner) : base(message, inner) { }
}
/// <summary>
/// Exception that often occures when there is no property that assigned with XPath Property in Class.
/// </summary>
public class MissingXPathException : Exception
{
/// <summary>
///
/// </summary>
public MissingXPathException() { }
/// <summary>
///
/// </summary>
/// <param name="message"></param>
public MissingXPathException(string message) : base(message) { }
/// <summary>
///
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
public MissingXPathException(string message, Exception inner) : base(message, inner) { }
}
}
#if FX20
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method |
AttributeTargets.Class | AttributeTargets.Assembly)]
public sealed class ExtensionAttribute : Attribute
{
}
}
#endif
#endif