using System; using System.Linq; using System.Collections.Generic; using SwissAcademic.Citavi; using SwissAcademic.Citavi.Metadata; using SwissAcademic.Collections; using SwissAcademic.Drawing; namespace SwissAcademic.Citavi.Citations { public class ComponentPartFilter : IComponentPartFilter { public IEnumerable GetTextUnits(ComponentPart componentPart, Template template, Citation citation, out bool handled) { handled = false; //ComponentPartFilter - SuppressURIParts V1.0.cs //EXAMPLE //http://www.url.de/path1/path2/path3/page.html var suppressHTTP = false; //true: www.url.de/path1/path2/path3/page.html var suppressWWW = false; //true: url.de/path1/path2/path3/page.html var suppressPath = true; //true: url.de if (componentPart == null) return null; if (citation == null || citation.Reference == null) return null; var onlineAddress = citation.Reference.OnlineAddress; if (string.IsNullOrEmpty(onlineAddress)) return null; var onlineAddressFieldElement = componentPart.GetFieldElements().FirstOrDefault(fieldElement => fieldElement.PropertyId == ReferencePropertyId.OnlineAddress); if (onlineAddressFieldElement == null) return null; var onlineAddressUri = new Uri(onlineAddress); if (onlineAddressUri == null) return null; var scheme = onlineAddressUri.Scheme; //EXAMPLE: http var path = onlineAddressUri.PathAndQuery; //EXAMPLE: /path1/path2/path3/page.html var host = onlineAddressUri.Host; //EXAMPLE: www.url.de var hostWithoutWWW = string.Empty; var hostParts = onlineAddressUri.Host.Split('.'); if (hostParts.Count() > 0 && hostParts[0].Equals("www", StringComparison.OrdinalIgnoreCase)) { hostWithoutWWW = String.Join(".", hostParts.Where((x, index) => index > 0)); //EXAMPLE: url.de } else if (hostParts.Count() > 0) { hostWithoutWWW = String.Join(".", hostParts); } if (suppressHTTP) scheme = string.Empty; if (!string.IsNullOrEmpty(scheme)) scheme = scheme + "://"; if (suppressWWW) host = hostWithoutWWW; if (suppressPath) path = string.Empty; var result = scheme + host + path; if (string.IsNullOrEmpty(result)) return null; var hostNameLiteralElement = new LiteralElement(componentPart, result); componentPart.Elements.ReplaceItem(onlineAddressFieldElement, hostNameLiteralElement); //all literal elements should always be output: foreach(LiteralElement literalElement in componentPart.Elements.OfType()) { literalElement.ApplyCondition = ElementApplyCondition.Always; } return null; } } }