~ubuntu-branches/ubuntu/trusty/pdfmod/trusty

« back to all changes in this revision

Viewing changes to lib/PdfSharp/PdfSharp.Pdf/PdfName.cs

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2010-06-18 03:44:46 UTC
  • Revision ID: james.westby@ubuntu.com-20100618034446-bogifrsscpayp361
Tags: upstream-0.8.3
ImportĀ upstreamĀ versionĀ 0.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region PDFsharp - A .NET library for processing PDF
 
2
//
 
3
// Authors:
 
4
//   Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
 
5
//
 
6
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
 
7
//
 
8
// http://www.pdfsharp.com
 
9
// http://sourceforge.net/projects/pdfsharp
 
10
//
 
11
// Permission is hereby granted, free of charge, to any person obtaining a
 
12
// copy of this software and associated documentation files (the "Software"),
 
13
// to deal in the Software without restriction, including without limitation
 
14
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
15
// and/or sell copies of the Software, and to permit persons to whom the
 
16
// Software is furnished to do so, subject to the following conditions:
 
17
//
 
18
// The above copyright notice and this permission notice shall be included
 
19
// in all copies or substantial portions of the Software.
 
20
//
 
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
22
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
23
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
24
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
25
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
26
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 
27
// DEALINGS IN THE SOFTWARE.
 
28
#endregion
 
29
 
 
30
using System;
 
31
using System.Diagnostics;
 
32
using System.Collections;
 
33
using System.Text;
 
34
using System.IO;
 
35
using PdfSharp.Internal;
 
36
using PdfSharp.Pdf.IO;
 
37
 
 
38
namespace PdfSharp.Pdf
 
39
{
 
40
  /// <summary>
 
41
  /// Represents a PDF name value.
 
42
  /// </summary>
 
43
  [DebuggerDisplay("({Value})")]
 
44
  public sealed class PdfName : PdfItem
 
45
  {
 
46
    /// <summary>
 
47
    /// Initializes a new instance of the <see cref="PdfName"/> class.
 
48
    /// </summary>
 
49
    public PdfName()
 
50
    {
 
51
      this.value = "/";  // Empty name.
 
52
    }
 
53
 
 
54
    /// <summary>
 
55
    /// Initializes a new instance of the <see cref="PdfName"/> class.
 
56
    /// </summary>
 
57
    public PdfName(string value)
 
58
    {
 
59
      if (value == null)
 
60
        throw new ArgumentNullException("value");
 
61
      if (value.Length == 0 || value[0] != '/')
 
62
        throw new ArgumentException(PSSR.NameMustStartWithSlash);
 
63
 
 
64
      this.value = value;
 
65
    }
 
66
 
 
67
    /// <summary>
 
68
    /// Determines whether the specified object is equal to this name.
 
69
    /// </summary>
 
70
    public override bool Equals(object obj)
 
71
    {
 
72
      return this.value.Equals(obj);
 
73
    }
 
74
 
 
75
    /// <summary>
 
76
    /// Returns the hash code for this instance.
 
77
    /// </summary>
 
78
    public override int GetHashCode()
 
79
    {
 
80
      return this.value.GetHashCode();
 
81
    }
 
82
 
 
83
    /// <summary>
 
84
    /// Gets the name as a string.
 
85
    /// </summary>
 
86
    public string Value
 
87
    {
 
88
      // This class must behave like a value type. Therefore it cannot be changed (like System.String).
 
89
      get { return this.value; }
 
90
    }
 
91
    string value;
 
92
 
 
93
    /// <summary>
 
94
    /// Returns the name. The string always begins with a slash.
 
95
    /// </summary>
 
96
    public override string ToString()
 
97
    {
 
98
      return this.value;
 
99
    }
 
100
 
 
101
    /// <summary>
 
102
    /// Determines whether the specified name and string are equal.
 
103
    /// </summary>
 
104
    public static bool operator ==(PdfName name, string str)
 
105
    {
 
106
      return name.value == str;
 
107
    }
 
108
 
 
109
    /// <summary>
 
110
    /// Determines whether the specified name and string are not equal.
 
111
    /// </summary>
 
112
    public static bool operator !=(PdfName name, string str)
 
113
    {
 
114
      return name.value != str;
 
115
    }
 
116
 
 
117
#if leads_to_ambiguity
 
118
    public static bool operator ==(string str, PdfName name)
 
119
    {
 
120
      return str == name.value;
 
121
    }
 
122
 
 
123
    public static bool operator !=(string str, PdfName name)
 
124
    {
 
125
      return str == name.value;
 
126
    }
 
127
 
 
128
    public static bool operator ==(PdfName name1, PdfName name2)
 
129
    {
 
130
      return name1.value == name2.value;
 
131
    }
 
132
 
 
133
    public static bool operator !=(PdfName name1, PdfName name2)
 
134
    {
 
135
      return name1.value != name2.value;
 
136
    }
 
137
#endif
 
138
 
 
139
    /// <summary>
 
140
    /// Represents the empty name.
 
141
    /// </summary>
 
142
    public static readonly PdfName Empty = new PdfName("/");
 
143
 
 
144
    /// <summary>
 
145
    /// Writes the name including the leading slash.
 
146
    /// </summary>
 
147
    internal override void WriteObject(PdfWriter writer)
 
148
    {
 
149
      // TODO: what if unicode character are part of the name? 
 
150
      writer.Write(this);
 
151
    }
 
152
 
 
153
    /// <summary>
 
154
    /// Gets the comparer for this type.
 
155
    /// </summary>
 
156
    public static PdfXNameComparer Comparer
 
157
    {
 
158
      get { return new PdfXNameComparer(); }
 
159
    }
 
160
 
 
161
    /// <summary>
 
162
    /// Implements a comparer that compares PdfName objects.
 
163
    /// </summary>
 
164
    public class PdfXNameComparer : IComparer
 
165
    {
 
166
      /// <summary>
 
167
      /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
 
168
      /// </summary>
 
169
      /// <param name="x">The first object to compare.</param>
 
170
      /// <param name="y">The second object to compare.</param>
 
171
      public int Compare(object x, object y)
 
172
      {
 
173
        PdfName l = x as PdfName;
 
174
        PdfName r = y as PdfName;
 
175
        if (l != null)
 
176
        {
 
177
          if (r != null)
 
178
            return l.value.CompareTo(r.value);
 
179
          else
 
180
            return -1;
 
181
        }
 
182
        else if (r != null)
 
183
          return 1;
 
184
        else
 
185
          return 0;
 
186
      }
 
187
    }
 
188
  }
 
189
}