~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/htmlparser/tests/outsinks/Convert.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 
2
 * The contents of this file are subject to the Netscape Public
 
3
 * License Version 1.1 (the "License"); you may not use this file
 
4
 * except in compliance with the License. You may obtain a copy of
 
5
 * the License at http://www.mozilla.org/NPL/
 
6
 *  
 
7
 * Software distributed under the License is distributed on an "AS
 
8
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
9
 * implied. See the License for the specific language governing
 
10
 * rights and limitations under the License.
 
11
 *  
 
12
 * The Original Code is Mozilla Communicator client code, released
 
13
 * March 31, 1998.
 
14
 * 
 
15
 * The Initial Developer of the Original Code is Netscape
 
16
 * Communications Corporation. Portions created by Netscape are
 
17
 * Copyright (C) 1998-1999 Netscape Communications Corporation. All
 
18
 * Rights Reserved.
 
19
 * 
 
20
 * Contributor(s): Akkana Peck.
 
21
 */
 
22
 
 
23
#include <ctype.h>      // for isdigit()
 
24
 
 
25
#include "nsXPCOM.h"
 
26
#include "nsParserCIID.h"
 
27
#include "nsIParser.h"
 
28
#include "nsIHTMLContentSink.h"
 
29
#include "nsIContentSerializer.h"
 
30
#include "nsLayoutCID.h"
 
31
#include "nsIHTMLToTextSink.h"
 
32
#include "nsIComponentManager.h"
 
33
#include "nsIServiceManager.h"
 
34
#include "nsIComponentRegistrar.h"
 
35
#include "nsReadableUtils.h"
 
36
#include "nsCRT.h"
 
37
 
 
38
static NS_DEFINE_IID(kIParserIID, NS_IPARSER_IID);
 
39
static NS_DEFINE_CID(kParserCID, NS_PARSER_CID);
 
40
 
 
41
int
 
42
Compare(nsString& str, nsString& aFileName)
 
43
{
 
44
  // Open the file in a Unix-centric way,
 
45
  // until I find out how to use nsFileSpec:
 
46
  char* filename = ToNewCString(aFileName);
 
47
  FILE* file = fopen(filename, "r");
 
48
  if (!file)
 
49
  {
 
50
    fprintf(stderr, "Can't open file %s", filename);
 
51
    perror(" ");
 
52
    delete[] filename;
 
53
    return 2;
 
54
  }
 
55
  delete[] filename;
 
56
 
 
57
  // Inefficiently read from the file:
 
58
  nsString inString;
 
59
  int c;
 
60
  int index = 0;
 
61
  int different = 0;
 
62
  while ((c = getc(file)) != EOF)
 
63
  {
 
64
    inString.Append(PRUnichar(c));
 
65
    // CVS isn't doing newline comparisons on these files for some reason.
 
66
    // So compensate for possible newline problems in the CVS file:
 
67
    if (c == '\n' && str[index] == '\r')
 
68
      ++index;
 
69
    if (c != str[index++])
 
70
    {
 
71
      //printf("Comparison failed at char %d: generated was %d, file had %d\n",
 
72
      //       index, (int)str[index-1], (int)c);
 
73
      different = index;
 
74
      break;
 
75
    }
 
76
  }
 
77
  if (file != stdin)
 
78
    fclose(file);
 
79
 
 
80
  if (!different)
 
81
    return 0;
 
82
  else
 
83
  {
 
84
    nsAutoString left;
 
85
    str.Left(left, different);
 
86
    char* cstr = ToNewUTF8String(left);
 
87
    printf("Comparison failed at char %d:\n-----\n%s\n-----\n",
 
88
           different, cstr);
 
89
    Recycle(cstr);
 
90
    return 1;
 
91
  }
 
92
}
 
93
 
 
94
//----------------------------------------------------------------------
 
95
// Convert html on stdin to either plaintext or (if toHTML) html
 
96
//----------------------------------------------------------------------
 
97
nsresult
 
98
HTML2text(nsString& inString, nsString& inType, nsString& outType,
 
99
          int flags, int wrapCol, nsString& compareAgainst)
 
100
{
 
101
  nsresult rv = NS_OK;
 
102
 
 
103
  nsString outString;
 
104
 
 
105
  // Create a parser
 
106
  nsIParser* parser;
 
107
   rv = nsComponentManager::CreateInstance(kParserCID, nsnull,
 
108
                                           kIParserIID,(void**)&parser);
 
109
  if (NS_FAILED(rv))
 
110
  {
 
111
    printf("Unable to create a parser : 0x%x\n", rv);
 
112
    return NS_ERROR_FAILURE;
 
113
  }
 
114
 
 
115
  // Create the appropriate output sink
 
116
#ifdef USE_SERIALIZER
 
117
  nsCAutoString progId(NS_CONTENTSERIALIZER_CONTRACTID_PREFIX);
 
118
  progId.AppendWithConversion(outType);
 
119
 
 
120
  // The syntax used here doesn't work
 
121
  nsCOMPtr<nsIContentSerializer> mSerializer;
 
122
  mSerializer = do_CreateInstance(NS_STATIC_CAST(const char *, progId));
 
123
  NS_ENSURE_TRUE(mSerializer, NS_ERROR_NOT_IMPLEMENTED);
 
124
 
 
125
  mSerializer->Init(flags, wrapCol);
 
126
 
 
127
  nsCOMPtr<nsIHTMLContentSink> sink (do_QueryInterface(mSerializer));
 
128
  if (!sink)
 
129
  {
 
130
    printf("Couldn't get content sink!\n");
 
131
    return NS_ERROR_UNEXPECTED;
 
132
  }
 
133
#else /* USE_SERIALIZER */
 
134
  nsCOMPtr<nsIContentSink> sink;
 
135
  if (inType != NS_LITERAL_STRING("text/html")
 
136
      || outType != NS_LITERAL_STRING("text/plain"))
 
137
  {
 
138
    char* in = ToNewCString(inType);
 
139
    char* out = ToNewCString(outType);
 
140
    printf("Don't know how to convert from %s to %s\n", in, out);
 
141
    Recycle(in);
 
142
    Recycle(out);
 
143
    return NS_ERROR_FAILURE;
 
144
  }
 
145
 
 
146
  sink = do_CreateInstance(NS_PLAINTEXTSINK_CONTRACTID);
 
147
  NS_ENSURE_TRUE(sink, NS_ERROR_FAILURE);
 
148
 
 
149
  nsCOMPtr<nsIHTMLToTextSink> textSink(do_QueryInterface(sink));
 
150
  NS_ENSURE_TRUE(textSink, NS_ERROR_FAILURE);
 
151
 
 
152
  textSink->Initialize(&outString, flags, wrapCol);
 
153
#endif /* USE_SERIALIZER */
 
154
 
 
155
  parser->SetContentSink(sink);
 
156
   nsCOMPtr<nsIDTD> dtd;
 
157
  if (inType.Equals(NS_LITERAL_STRING("text/html"))) {
 
158
    static NS_DEFINE_CID(kNavDTDCID, NS_CNAVDTD_CID);
 
159
    rv=nsComponentManager::CreateInstance(kNavDTDCID,nsnull,NS_GET_IID(nsIDTD),getter_AddRefs(dtd));
 
160
  }
 
161
  else
 
162
  {
 
163
    printf("Don't know how to deal with non-html input!\n");
 
164
    return NS_ERROR_NOT_IMPLEMENTED;
 
165
  }
 
166
  if (NS_FAILED(rv))
 
167
  {
 
168
    printf("Couldn't create new HTML DTD: 0x%x\n", rv);
 
169
    return rv;
 
170
  }
 
171
 
 
172
  parser->RegisterDTD(dtd);
 
173
 
 
174
  rv = parser->Parse(inString, 0, NS_LossyConvertUCS2toASCII(inType), PR_FALSE, PR_TRUE);
 
175
  if (NS_FAILED(rv))
 
176
  {
 
177
    printf("Parse() failed! 0x%x\n", rv);
 
178
    return rv;
 
179
  }
 
180
  NS_RELEASE(parser);
 
181
 
 
182
  if (compareAgainst.Length() > 0)
 
183
    return Compare(outString, compareAgainst);
 
184
 
 
185
  char* charstar = ToNewUTF8String(outString);
 
186
  printf("Output string is:\n--------------------\n%s--------------------\n",
 
187
         charstar);
 
188
  delete[] charstar;
 
189
 
 
190
  return NS_OK;
 
191
}
 
192
 
 
193
//----------------------------------------------------------------------
 
194
 
 
195
int main(int argc, char** argv)
 
196
{
 
197
  nsString inType(NS_LITERAL_STRING("text/html"));
 
198
  nsString outType(NS_LITERAL_STRING("text/plain"));
 
199
  int wrapCol = 72;
 
200
  int flags = 0;
 
201
  nsString compareAgainst;
 
202
 
 
203
 
 
204
  // Skip over progname arg:
 
205
  const char* progname = argv[0];
 
206
  --argc; ++argv;
 
207
 
 
208
  // Process flags
 
209
  while (argc > 0 && argv[0][0] == '-')
 
210
  {
 
211
    switch (argv[0][1])
 
212
    {
 
213
      case 'h':
 
214
        printf("\
 
215
Usage: %s [-i intype] [-o outtype] [-f flags] [-w wrapcol] [-c comparison_file] infile\n\
 
216
\tIn/out types are mime types (e.g. text/html)\n\
 
217
\tcomparison_file is a file against which to compare the output\n\
 
218
\n\
 
219
\tDefaults are -i text/html -o text/plain -f 0 -w 72 [stdin]\n",
 
220
               progname);
 
221
        exit(0);
 
222
 
 
223
        case 'i':
 
224
        if (argv[0][2] != '\0')
 
225
          inType.AssignWithConversion(argv[0]+2);
 
226
        else {
 
227
          inType.AssignWithConversion(argv[1]);
 
228
          --argc;
 
229
          ++argv;
 
230
        }
 
231
        break;
 
232
 
 
233
      case 'o':
 
234
        if (argv[0][2] != '\0')
 
235
          outType.AssignWithConversion(argv[0]+2);
 
236
        else {
 
237
          outType.AssignWithConversion(argv[1]);
 
238
          --argc;
 
239
          ++argv;
 
240
        }
 
241
        break;
 
242
 
 
243
      case 'w':
 
244
        if (isdigit(argv[0][2]))
 
245
          wrapCol = atoi(argv[0]+2);
 
246
        else {
 
247
          wrapCol = atoi(argv[1]);
 
248
          --argc;
 
249
          ++argv;
 
250
        }
 
251
        break;
 
252
 
 
253
      case 'f':
 
254
        if (isdigit(argv[0][2]))
 
255
          flags = atoi(argv[0]+2);
 
256
        else {
 
257
          flags = atoi(argv[1]);
 
258
          --argc;
 
259
          ++argv;
 
260
        }
 
261
        break;
 
262
 
 
263
      case 'c':
 
264
        if (argv[0][2] != '\0')
 
265
          compareAgainst.AssignWithConversion(argv[0]+2);
 
266
        else {
 
267
          compareAgainst.AssignWithConversion(argv[1]);
 
268
          --argc;
 
269
          ++argv;
 
270
        }
 
271
        break;
 
272
    }
 
273
    ++argv;
 
274
    --argc;
 
275
  }
 
276
 
 
277
  FILE* file = 0;
 
278
  if (argc > 0)         // read from a file
 
279
  {
 
280
    // Open the file in a Unix-centric way,
 
281
    // until I find out how to use nsFileSpec:
 
282
    file = fopen(argv[0], "r");
 
283
    if (!file)
 
284
    {
 
285
      fprintf(stderr, "Can't open file %s", argv[0]);
 
286
      perror(" ");
 
287
      exit(1);
 
288
    }
 
289
  }
 
290
  else
 
291
    file = stdin;
 
292
 
 
293
  nsresult ret;
 
294
  {
 
295
    nsCOMPtr<nsIServiceManager> servMan;
 
296
    NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
 
297
    nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
 
298
    NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
 
299
    registrar->AutoRegister(nsnull);
 
300
 
 
301
    // Read in the string: very inefficient, but who cares?
 
302
    nsString inString;
 
303
    int c;
 
304
    while ((c = getc(file)) != EOF)
 
305
      inString.Append(PRUnichar(c));
 
306
 
 
307
    if (file != stdin)
 
308
      fclose(file);
 
309
 
 
310
    ret = HTML2text(inString, inType, outType, flags, wrapCol, compareAgainst);
 
311
  } // this scopes the nsCOMPtrs
 
312
  // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
 
313
  nsresult rv = NS_ShutdownXPCOM( NULL );
 
314
  NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
 
315
  return ret;
 
316
}