KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > database > CmsHtmlImport


1 /*
2  * File :
3  * Date :
4  * Version:
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.tools.database;
33
34 import org.opencms.db.CmsDbIoException;
35 import org.opencms.file.CmsFolder;
36 import org.opencms.file.CmsObject;
37 import org.opencms.file.CmsProperty;
38 import org.opencms.file.CmsPropertyDefinition;
39 import org.opencms.file.CmsResource;
40 import org.opencms.file.CmsResourceFilter;
41 import org.opencms.file.types.CmsResourceTypeFolder;
42 import org.opencms.file.types.CmsResourceTypeImage;
43 import org.opencms.file.types.CmsResourceTypePlain;
44 import org.opencms.file.types.CmsResourceTypePointer;
45 import org.opencms.file.types.CmsResourceTypeXmlPage;
46 import org.opencms.i18n.CmsEncoder;
47 import org.opencms.i18n.CmsLocaleManager;
48 import org.opencms.i18n.CmsMessageContainer;
49 import org.opencms.importexport.CmsImportExportException;
50 import org.opencms.jsp.CmsJspActionElement;
51 import org.opencms.loader.CmsResourceManager;
52 import org.opencms.lock.CmsLock;
53 import org.opencms.main.CmsException;
54 import org.opencms.main.CmsIllegalArgumentException;
55 import org.opencms.main.CmsLog;
56 import org.opencms.main.OpenCms;
57 import org.opencms.report.I_CmsReport;
58 import org.opencms.staticexport.CmsLink;
59 import org.opencms.staticexport.CmsLinkTable;
60 import org.opencms.util.CmsStringUtil;
61 import org.opencms.xml.page.CmsXmlPage;
62
63 import java.io.File JavaDoc;
64 import java.io.FileInputStream JavaDoc;
65 import java.io.IOException JavaDoc;
66 import java.net.MalformedURLException JavaDoc;
67 import java.net.URL JavaDoc;
68 import java.util.ArrayList JavaDoc;
69 import java.util.Enumeration JavaDoc;
70 import java.util.HashMap JavaDoc;
71 import java.util.HashSet JavaDoc;
72 import java.util.Hashtable JavaDoc;
73 import java.util.Iterator JavaDoc;
74 import java.util.List JavaDoc;
75 import java.util.Locale JavaDoc;
76 import java.util.Map JavaDoc;
77 import java.util.StringTokenizer JavaDoc;
78
79 import javax.servlet.http.HttpServletRequest JavaDoc;
80
81 import org.apache.commons.collections.ExtendedProperties;
82 import org.apache.commons.logging.Log;
83
84 /**
85  * This class implements the HTML->OpenCms Template converter for OpenCms 6.x.<p>
86  *
87  * @author Michael Emmerich
88  * @author Armen Markarian
89  * @author Peter Bonrad
90  *
91  * @version $Revision: 1.15 $
92  *
93  * @since 6.0.0
94  */

95 public class CmsHtmlImport {
96
97     /** filename of the meta.properties file. */
98     public static final String JavaDoc META_PROPERTIES = "meta.properties";
99
100     /** The log object for this class. */
101     private static final Log LOG = CmsLog.getLog(CmsHtmlImport.class);
102
103     /** the base URL for link modification. */
104     private URL JavaDoc m_baseUrl;
105
106     private CmsJspActionElement m_cms;
107
108     /** the CmsObject to use. */
109     private CmsObject m_cmsObject;
110
111     /** the destination directory in the OpenCms VFS. */
112     private String JavaDoc m_destinationDir;
113
114     /** he download gallery name. */
115     private String JavaDoc m_downloadGallery;
116
117     private String JavaDoc m_element;
118
119     /** the end pattern for extracting content. */
120     private String JavaDoc m_endPattern;
121
122     /** HashMap of all known extensions in OpenCms. */
123     private Map JavaDoc m_extensions;
124
125     /**
126      * Storage for external links.<p>
127      *
128      * It is filled by the HtmlConverter each time a new external link is found.<p>
129      */

130     private HashSet JavaDoc m_externalLinks;
131
132     /**
133      * The file index contains all resourcenames in the real file system and their renamed ones in the OpenCms VFS.<p>
134      */

135     private HashMap JavaDoc m_fileIndex;
136
137     /** the HTML converter to parse and modifiy the content. */
138     private CmsHtmlImportConverter m_htmlConverter;
139
140     /** reference to the import thread. */
141     private CmsHtmlImportThread m_htmlImportThread;
142
143     /** the image gallery name. */
144     private String JavaDoc m_imageGallery;
145
146     /**
147      * Storage for image alt tags, it is filled by the HtmlConverter each time a new image is found.
148      */

149     private HashMap JavaDoc m_imageInfo;
150
151     /** the input directory in the "real" file system. */
152     private String JavaDoc m_inputDir;
153
154     /** the encoding used for all imported input files. */
155     private String JavaDoc m_inputEncoding;
156
157     /** should broken links be kept. */
158     private String JavaDoc m_keepBrokenLinks;
159
160     /** the keep broken links mode flag. */
161     private boolean m_keepBrokenLinksMode;
162
163     /** leave downloads at the original location. */
164     private String JavaDoc m_leaveDownloads;
165
166     /** the leave downloads mode flag. */
167     private boolean m_leaveDownloadsMode;
168
169     /** leave external links at the original location. */
170     private String JavaDoc m_leaveExternalLinks;
171
172     /** the leave external links mode flag. */
173     private boolean m_leaveExternalLinksMode;
174
175     /** leave images at the original location. */
176     private String JavaDoc m_leaveImages;
177
178     /** the leave images mode flag. */
179     private boolean m_leaveImagesMode;
180
181     /** the external link gallery name. */
182     private String JavaDoc m_linkGallery;
183
184     /** the locale use for content definition. */
185     private Locale JavaDoc m_locale;
186
187     /** the overwrite value new resources. */
188     private String JavaDoc m_overwrite;
189
190     /** the overwrite mode flag. */
191     private boolean m_overwriteMode;
192
193     /** Map with all parents in file system to OpenCms. */
194     private HashMap JavaDoc m_parents;
195
196     /** the report for the output. */
197     private I_CmsReport m_report;
198
199     /** the start pattern for extracting content. */
200     private String JavaDoc m_startPattern;
201
202     /** the template use for all pages. */
203     private String JavaDoc m_template;
204
205     /**
206      * Creates new HtmlImport Object with http request parameters.<p>
207      *
208      * @param cms the current CmsJspActionElement
209      * @param request the http servlet request
210      */

211     public CmsHtmlImport(CmsJspActionElement cms, HttpServletRequest JavaDoc request) {
212
213         this(
214             cms,
215             request.getParameter("inputDir"),
216             request.getParameter("destinationDir"),
217             request.getParameter("imageGallery"),
218             request.getParameter("linkGallery"),
219             request.getParameter("downloadGallery"),
220             request.getParameter("template"),
221             request.getParameter("element"),
222             request.getParameter("locale"),
223             request.getParameter("encoding"),
224             request.getParameter("startPattern"),
225             request.getParameter("endPattern"),
226             request.getParameter("overwrite"),
227             request.getParameter("leaveimages"),
228             request.getParameter("leavedownloads"),
229             request.getParameter("keepbrokenlinks"),
230             request.getParameter("leaveexternallinks"),
231             request);
232     }
233
234     /**
235      * Constructor, creates a new HtmlImport.<p>
236      *
237      * @param cms the current CmsJspActionElement
238      * @param inputDir the input directory in the "real" file system
239      * @param destinationDir the destination directory in the OpenCms VFS
240      * @param imageGallery the image gallery name
241      * @param linkGallery the external link gallery name
242      * @param downloadGallery the download gallery name
243      * @param template the template use for all pages
244      * @param element the element property use for all pages
245      * @param locale the full locale name
246      * @param encoding encoding used for importing all pages
247      * @param startPattern the start pattern definition for content extracting
248      * @param endPattern the end pattern definition for content extracting
249      * @param overwrite the overwrite mode
250      * @param leaveImages flag if images are left in original location
251      * @param leaveDownloads flag if downloads are left in original location
252      * @param keepBrokenLinks flag if broken links are kept or replaced by a '#'
253      * @param leaveExternalLinks flag if external links are left at their original location
254      */

255     public CmsHtmlImport(
256         CmsJspActionElement cms,
257         String JavaDoc inputDir,
258         String JavaDoc destinationDir,
259         String JavaDoc imageGallery,
260         String JavaDoc linkGallery,
261         String JavaDoc downloadGallery,
262         String JavaDoc template,
263         String JavaDoc element,
264         String JavaDoc locale,
265         String JavaDoc encoding,
266         String JavaDoc startPattern,
267         String JavaDoc endPattern,
268         String JavaDoc overwrite,
269         String JavaDoc leaveImages,
270         String JavaDoc leaveDownloads,
271         String JavaDoc keepBrokenLinks,
272         String JavaDoc leaveExternalLinks,
273         HttpServletRequest JavaDoc request) {
274
275         if (inputDir == null) {
276             inputDir = "";
277         }
278         if (destinationDir == null) {
279             destinationDir = "";
280         }
281         if (imageGallery == null) {
282             imageGallery = "";
283         }
284         if (linkGallery == null) {
285             linkGallery = "";
286         }
287         if (downloadGallery == null) {
288             downloadGallery = "";
289         }
290         if (template == null) {
291             template = "";
292         }
293         if (element == null) {
294             element = "body";
295         }
296         if (encoding == null) {
297             encoding = "";
298         }
299         if (startPattern == null) {
300             startPattern = "";
301         }
302         if (endPattern == null) {
303             endPattern = "";
304         }
305         if (overwrite == null) {
306             overwrite = "";
307         }
308         String JavaDoc action = request.getParameter("action");
309         if (action == null) {
310             overwrite = "checked";
311         }
312         if (leaveImages == null) {
313             leaveImages = "";
314         }
315         if (leaveDownloads == null) {
316             leaveDownloads = "";
317         }
318
319         if (keepBrokenLinks == null) {
320             keepBrokenLinks = "";
321         }
322
323         if (leaveExternalLinks == null) {
324             leaveExternalLinks = "";
325         }
326
327         // store all member variables
328
m_cms = cms;
329         m_cmsObject = m_cms.getCmsObject();
330         m_locale = CmsLocaleManager.getLocale(locale);
331         if (m_locale == null) {
332             m_locale = m_cms.getRequestContext().getLocale();
333         }
334         // body element should be set by html-form
335
m_inputDir = inputDir.trim();
336
337         // cut of a trailing '/' or '\'
338
if (m_inputDir.endsWith("/") || m_inputDir.endsWith("\\")) {
339             m_inputDir = m_inputDir.substring(0, m_inputDir.length() - 1);
340         }
341
342         m_destinationDir = destinationDir.trim();
343         if ((!m_destinationDir.equals("")) && (!m_destinationDir.endsWith("/"))) {
344             m_destinationDir += "/";
345         }
346
347         m_imageGallery = imageGallery.trim();
348         if ((!m_imageGallery.equals("")) && (!m_imageGallery.endsWith("/"))) {
349             m_imageGallery += "/";
350         }
351
352         m_linkGallery = linkGallery.trim();
353         if ((!m_linkGallery.equals("")) && (!m_linkGallery.endsWith("/"))) {
354             m_linkGallery += "/";
355         }
356
357         m_downloadGallery = downloadGallery.trim();
358         if ((!m_downloadGallery.equals("")) && (!m_downloadGallery.endsWith("/"))) {
359             m_downloadGallery += "/";
360         }
361
362         m_template = template;
363         m_element = element;
364         m_inputEncoding = encoding;
365
366         if (CmsStringUtil.isEmpty(m_inputEncoding)) {
367             m_inputEncoding = CmsEncoder.ENCODING_ISO_8859_1;
368         }
369         m_startPattern = startPattern;
370         m_endPattern = endPattern;
371
372         m_overwrite = overwrite.trim();
373         if (m_overwrite.equals("checked")) {
374             m_overwriteMode = true;
375         } else {
376             m_overwriteMode = false;
377         }
378
379         m_leaveImages = leaveImages.trim();
380         if (m_leaveImages.equals("checked")) {
381             m_leaveImagesMode = true;
382         } else {
383             m_leaveImagesMode = false;
384         }
385
386         m_leaveDownloads = leaveDownloads.trim();
387         if (m_leaveDownloads.equals("checked")) {
388             m_leaveDownloadsMode = true;
389         } else {
390             m_leaveDownloadsMode = false;
391         }
392
393         m_keepBrokenLinks = keepBrokenLinks.trim();
394         if (m_keepBrokenLinks.equals("checked")) {
395             m_keepBrokenLinksMode = true;
396         } else {
397             m_keepBrokenLinksMode = false;
398         }
399
400         m_leaveExternalLinks = leaveExternalLinks.trim();
401         if (m_leaveExternalLinks.equals("checked")) {
402             m_leaveExternalLinksMode = true;
403         } else {
404             m_leaveExternalLinksMode = false;
405         }
406
407         // create all other required member objects
408
m_fileIndex = new HashMap JavaDoc();
409         m_parents = new HashMap JavaDoc();
410         m_externalLinks = new HashSet JavaDoc();
411         m_imageInfo = new HashMap JavaDoc();
412         m_extensions = OpenCms.getResourceManager().getExtensionMapping();
413         m_htmlConverter = new CmsHtmlImportConverter(this, false);
414         m_baseUrl = null;
415         try {
416             m_baseUrl = new URL JavaDoc("file://");
417         } catch (MalformedURLException JavaDoc e) {
418             // this won't happen
419
}
420     }
421
422     /**
423      * Tests if all given input parameters for the HTML Import are valid, i.e. that all the
424      * given folders do exist. <p>
425      *
426      * @throws CmsIllegalArgumentException if some parameters are not valid
427      */

428     public void checkParameters() throws CmsIllegalArgumentException {
429
430         // check the input directory
431
File JavaDoc inputDir = new File JavaDoc(m_inputDir);
432         if (!inputDir.exists() || inputDir.isFile()) {
433             // the input directory is not valid
434
throw new CmsIllegalArgumentException(Messages.get().container(
435                 Messages.GUI_HTMLIMPORT_INPUTDIR_1,
436                 m_inputDir));
437         }
438
439         // check the destination directory
440
try {
441             m_cmsObject.readFolder(m_destinationDir);
442         } catch (CmsException e) {
443             // an excpetion is thrown if the folder does not exist
444
throw new CmsIllegalArgumentException(Messages.get().container(
445                 Messages.GUI_HTMLIMPORT_DESTDIR_1,
446                 m_destinationDir), e);
447         }
448
449         // check the image gallery
450
// only if flag for leaving images at original location is off
451
if (!m_leaveImagesMode) {
452             try {
453                 CmsFolder folder = m_cmsObject.readFolder(m_imageGallery);
454                 // check if folder is a image gallery
455
String JavaDoc name = OpenCms.getResourceManager().getResourceType(folder.getTypeId()).getTypeName();
456                 if (!name.equals("imagegallery")) {
457                     throw new CmsIllegalArgumentException(Messages.get().container(
458                         Messages.GUI_HTMLIMPORT_IMGGALLERY_INVALID_1,
459                         m_imageGallery));
460                 }
461             } catch (CmsException e) {
462                 // an excpetion is thrown if the folder does not exist
463
throw new CmsIllegalArgumentException(Messages.get().container(
464                     Messages.GUI_HTMLIMPORT_IMGGALLERY_1,
465                     m_imageGallery), e);
466             }
467         }
468
469         // check the link gallery
470
// only if flag for leaving external links at original location is off
471
if (!m_leaveExternalLinksMode) {
472             try {
473                 CmsFolder folder = m_cmsObject.readFolder(m_linkGallery);
474                 // check if folder is a link gallery
475
String JavaDoc name = OpenCms.getResourceManager().getResourceType(folder.getTypeId()).getTypeName();
476                 if (!name.equals("linkgallery")) {
477                     throw new CmsIllegalArgumentException(Messages.get().container(
478                         Messages.GUI_HTMLIMPORT_LINKGALLERY_INVALID_1,
479                         m_linkGallery));
480                 }
481             } catch (CmsException e) {
482                 // an excpetion is thrown if the folder does not exist
483
throw new CmsIllegalArgumentException(Messages.get().container(
484                     Messages.GUI_HTMLIMPORT_LINKGALLERY_1,
485                     m_linkGallery), e);
486             }
487         }
488
489         // check the download gallery
490
if ((!isExternal(m_downloadGallery)) && (!m_leaveDownloadsMode)) {
491             try {
492                 CmsFolder folder = m_cmsObject.readFolder(m_downloadGallery);
493                 // check if folder is a download gallery
494
String JavaDoc name = OpenCms.getResourceManager().getResourceType(folder.getTypeId()).getTypeName();
495                 if (!name.equals("downloadgallery")) {
496                     throw new CmsIllegalArgumentException(Messages.get().container(
497                         Messages.GUI_HTMLIMPORT_DOWNGALLERY_INVALID_1,
498                         m_downloadGallery));
499                 }
500             } catch (CmsException e) {
501                 // an excpetion is thrown if the folder does not exist
502
throw new CmsIllegalArgumentException(Messages.get().container(
503                     Messages.GUI_HTMLIMPORT_DOWNGALLERY_1,
504                     m_downloadGallery), e);
505             }
506         }
507
508         // check the template
509
try {
510             m_cmsObject.readResource(m_template, CmsResourceFilter.ALL);
511         } catch (CmsException e) {
512             // an excpetion is thrown if the template does not exist
513
if (!isValidElement()) {
514                 throw new CmsIllegalArgumentException(Messages.get().container(
515                     Messages.GUI_HTMLIMPORT_TEMPLATE_1,
516                     m_template), e);
517             }
518         }
519
520         // check the element
521
if (!isValidElement()) {
522             throw new CmsIllegalArgumentException(Messages.get().container(
523                 Messages.GUI_HTMLIMPORT_INVALID_ELEM_2,
524                 m_element,
525                 m_template));
526         }
527
528         // check if we are in an offline project
529
if (m_cmsObject.getRequestContext().currentProject().isOnlineProject()) {
530             throw new CmsIllegalArgumentException(
531                 Messages.get().container(Messages.GUI_HTMLIMPORT_CONSTRAINT_OFFLINE_0));
532         }
533     }
534
535     /**
536      * Calculates an absolute uri from a relative "uri" and the given absolute "baseUri".<p>
537      *
538      * If "uri" is already absolute, it is returned unchanged.
539      * This method also returns "uri" unchanged if it is not well-formed.<p>
540      *
541      * @param relativeUri the relative uri to calculate an absolute uri for
542      * @param baseUri the base uri, this must be an absolute uri
543      * @return an absolute uri calculated from "uri" and "baseUri"
544      */

545     public String JavaDoc getAbsoluteUri(String JavaDoc relativeUri, String JavaDoc baseUri) {
546
547         if ((relativeUri == null) || (relativeUri.charAt(0) == '/') || (relativeUri.startsWith("#"))) {
548
549             return relativeUri;
550         }
551
552         // if we are on a windows system, we must add a ":" in the uri later
553
String JavaDoc windowsAddition = "";
554         if (File.separator.equals("\\")) {
555             windowsAddition = ":";
556         }
557
558         try {
559             URL JavaDoc url = new URL JavaDoc(new URL JavaDoc(m_baseUrl, "file://" + baseUri), relativeUri);
560             if (url.getQuery() == null) {
561                 if (url.getRef() == null) {
562                     return url.getHost() + windowsAddition + url.getPath();
563                 } else {
564                     return url.getHost() + windowsAddition + url.getPath() + "#" + url.getRef();
565                 }
566             } else {
567                 return url.getHost() + windowsAddition + url.getPath() + "?" + url.getQuery();
568             }
569         } catch (MalformedURLException JavaDoc e) {
570             return relativeUri;
571         }
572     }
573
574     /**
575      * Returns the destinationDir.<p>
576      *
577      * @return the destinationDir
578      */

579     public String JavaDoc getDestinationDir() {
580
581         return m_destinationDir;
582         // return m_destinationDir.substring(0, m_destinationDir.length() - 1);
583
}
584
585     /**
586      * Returns the downloadGallery.<p>
587      *
588      * @return the downloadGallery
589      */

590     public String JavaDoc getDownloadGallery() {
591
592         return m_downloadGallery;
593     }
594
595     /**
596      * Returns the element.<p>
597      *
598      * @return the element
599      */

600     public String JavaDoc getElement() {
601
602         return m_element;
603     }
604
605     /**
606      * Returns the endPattern.<p>
607      *
608      * @return the endPattern
609      */

610     public String JavaDoc getEndPattern() {
611
612         return m_endPattern;
613     }
614
615     /**
616      * Returns the imageGallery.<p>
617      *
618      * @return the imageGallery
619      */

620     public String JavaDoc getImageGallery() {
621
622         return m_imageGallery;
623     }
624
625     /**
626      * Returns the inputDir.<p>
627      *
628      * @return the inputDir
629      */

630     public String JavaDoc getInputDir() {
631
632         return m_inputDir;
633     }
634
635     /**
636      * Returns the inputEncoding.<p>
637      *
638      * @return the inputEncoding
639      */

640     public String JavaDoc getInputEncoding() {
641
642         return m_inputEncoding;
643     }
644
645     /**
646      * Returns the keepBrokenLinks.<p>
647      *
648      * @return the keepBrokenLinks
649      */

650     public String JavaDoc getKeepBrokenLinks() {
651
652         return m_keepBrokenLinks;
653     }
654
655     /**
656      * Returns the leaveDownloads.<p>
657      *
658      * @return the leaveDownloads
659      */

660     public String JavaDoc getLeaveDownloads() {
661
662         return m_leaveDownloads;
663     }
664
665     /**
666      * Returns the leaveExternalLinks.<p>
667      *
668      * @return the leaveExternalLinks
669      */

670     public String JavaDoc getLeaveExternalLinks() {
671
672         return m_leaveExternalLinks;
673     }
674
675     /**
676      * Returns the leaveImages.<p>
677      *
678      * @return the leaveImages
679      */

680     public String JavaDoc getLeaveImages() {
681
682         return m_leaveImages;
683     }
684
685     /**
686      * Returns the linkGallery.<p>
687      *
688      * @return the linkGallery
689      */

690     public String JavaDoc getLinkGallery() {
691
692         return m_linkGallery;
693     }
694
695     /**
696      * Returns the locale.<p>
697      *
698      * @return the locale
699      */

700     public Locale JavaDoc getLocale() {
701
702         return m_locale;
703     }
704
705     /**
706      * Returns the overwrite flag.<p>
707      *
708      * @return the overwrite flag
709      */

710     public String JavaDoc getOverwrite() {
711
712         return m_overwrite;
713     }
714
715     /**
716      * Returns the startPattern.<p>
717      *
718      * @return the startPattern
719      */

720     public String JavaDoc getStartPattern() {
721
722         return m_startPattern;
723     }
724
725     /**
726      * Returns the template.<p>
727      *
728      * @return the template
729      */

730     public String JavaDoc getTemplate() {
731
732         return m_template;
733     }
734
735     /**
736      * Returns the output of the HtmlImportThread.<p>
737      *
738      * @return log output of the import threat
739      */

740     public String JavaDoc getThreadOutput() {
741
742         String JavaDoc output = "";
743         // check if we have a thread
744
if (m_htmlImportThread != null) {
745             // is it still alive?
746
if (m_htmlImportThread.isAlive()) {
747                 output = m_htmlImportThread.getReportUpdate();
748             }
749         }
750
751         return output;
752     }
753
754     /**
755      * Sets the keepBrokenLinks.<p>
756      *
757      * @param keepBrokenLinks the keepBrokenLinks to set
758      */

759     public void setKeepBrokenLinks(String JavaDoc keepBrokenLinks) {
760
761         m_keepBrokenLinks = keepBrokenLinks;
762     }
763
764     /**
765      * Sets the leaveExternalLinks.<p>
766      *
767      * @param leaveExternalLinks the leaveExternalLinks to set
768      */

769     public void setLeaveExternalLinks(String JavaDoc leaveExternalLinks) {
770
771         m_leaveExternalLinks = leaveExternalLinks;
772     }
773
774     /**
775      * Imports all resources from the real filesystem, stores them into the correct locations
776      * in the OpenCms VFS and modifies all links. This method is called form the JSP to start the
777      * import process.<p>
778      * @param report StringBuffer for reporting
779      * @throws Exception if something goes wrong
780      */

781     public void startImport(I_CmsReport report) throws Exception JavaDoc {
782
783         try {
784             m_report = report;
785             m_report.println(Messages.get().container(Messages.RPT_HTML_IMPORT_BEGIN_0), I_CmsReport.FORMAT_HEADLINE);
786
787             // first build the index of all resources
788
buildIndex(m_inputDir);
789             // build list with all parent resources of input directory for links to outside import folder
790
buildParentPath();
791             // copy and parse all html files first. during the copy process we will collect all
792
// required data for downloads and images
793
copyHtmlFiles(m_inputDir);
794             // now copy the other files
795
copyOtherFiles(m_inputDir);
796             // finally create all the external links
797
createExternalLinks();
798             m_report.println(Messages.get().container(Messages.RPT_HTML_IMPORT_END_0), I_CmsReport.FORMAT_HEADLINE);
799         } catch (Exception JavaDoc e) {
800             e.printStackTrace();
801         } finally {
802             // clear memory
803
clear();
804         }
805
806     }
807
808     /**
809      * Add a new external link to the storage of external links.<p>
810      *
811      * All links in this storage are later used to create entries in the external link gallery.
812      * @param externalLink link to an external resource
813      * @return the complete path to the external link file, if one is created.
814      */

815     public String JavaDoc storeExternalLink(String JavaDoc externalLink) {
816
817         if (!m_leaveExternalLinksMode) {
818             m_externalLinks.add(externalLink);
819             return getExternalLinkFile(externalLink);
820         }
821
822         return null;
823     }
824
825     /**
826      * Add a new image info to the storage of image infos.<p>
827      *
828      * The image infoes are later used to set the description properties of the images.
829      * @param image the name of the image
830      * @param altText the alt-text of the image
831      */

832     public void storeImageInfo(String JavaDoc image, String JavaDoc altText) {
833
834         m_imageInfo.put(image, altText);
835     }
836
837     /**
838      * Checks if the HtmlImportThread is still alive.<p>
839      *
840      * @return true or false
841      */

842     public boolean threadAlive() {
843
844         boolean alive = false;
845         if (m_htmlImportThread != null) {
846             alive = m_htmlImportThread.isAlive();
847         }
848
849         return alive;
850     }
851
852     /**
853      * Translated a link into the real filesystem to its new location in the OpenCms VFS.<p>
854      *
855      * This is needed by the HtmlConverter to get the correct links for link translation.
856      * @param link link to the real filesystem
857      * @return string containing absulute link into the OpenCms VFS
858      */

859     public String JavaDoc translateLink(String JavaDoc link) {
860
861         String JavaDoc translatedLink = null;
862         translatedLink = (String JavaDoc)m_fileIndex.get(link.replace('\\', '/'));
863
864         if (translatedLink == null) {
865             // its an anchor link, so copy use it
866
if (link.startsWith("#")) {
867                 translatedLink = link;
868             }
869
870             // relative link to opencms root
871
else if (link.startsWith("/")) {
872
873                 // strip cms context path
874
if (link.startsWith(OpenCms.getSystemInfo().getOpenCmsContext())) {
875                     link = link.substring(OpenCms.getSystemInfo().getOpenCmsContext().length());
876                 }
877
878                 // check if resource exists
879
if ((m_keepBrokenLinksMode) || (m_cmsObject.existsResource(link))) {
880                     translatedLink = link;
881                 }
882             }
883
884             // // link to subdir
885
// else if ((link.startsWith(m_inputDir.replace('\\', '/'))) && (link.length() >= m_inputDir.length() + 1)) {
886
// // create a 'faked' link into the VFS. Original link was
887
// // directing to a missing page, so let the link so to the
888
// // same page inside of OpenCms.
889
// String relativeFSName = link.substring(m_inputDir.length() + 1);
890
// String relative = m_destinationDir + relativeFSName;
891
//
892
// if ((m_keepBrokenLinksMode) || (m_cmsObject.existsResource(relative))) {
893
// translatedLink = relative;
894
// }
895
// }
896

897             else {
898
899                 String JavaDoc fileBase = getBasePath(m_inputDir, link);
900                 String JavaDoc cmsBase = (String JavaDoc)m_parents.get(fileBase);
901                 if (cmsBase != null) {
902                     String JavaDoc outLink = cmsBase + link.substring(fileBase.length()).replace('\\', '/');
903                     if ((m_keepBrokenLinksMode) || (m_cmsObject.existsResource(outLink))) {
904                         translatedLink = outLink;
905                     }
906                 }
907             }
908         }
909
910         // if the link goes to a directory, lets link to the index page within
911
if ((translatedLink != null) && translatedLink.endsWith("/")) {
912             translatedLink += "index.html";
913         }
914
915         // final check: if the translated link is still null the original link found
916
// was broken
917
// lets link it to the same page, the link is found on
918
if (translatedLink == null) {
919             translatedLink = "#";
920         }
921
922         return translatedLink;
923     }
924
925     /**
926      * Builds an index of all files to be imported and determines their new names in the OpenCms.<p>
927      *
928      * @param startfolder the folder to start with
929      *
930      * @throws Exception if something goes wrong
931      */

932     private void buildIndex(String JavaDoc startfolder) throws Exception JavaDoc {
933
934         File JavaDoc folder = new File JavaDoc(startfolder);
935         // get all subresources
936

937         File JavaDoc[] subresources = folder.listFiles();
938         // now loop through all subresources and add them to the index list
939
for (int i = 0; i < subresources.length; i++) {
940             try {
941
942                 String JavaDoc relativeFSName = subresources[i].getAbsolutePath().substring(m_inputDir.length() + 1);
943                 String JavaDoc absoluteVFSName = getVfsName(relativeFSName, subresources[i].getName(), subresources[i].isFile());
944                 m_report.print(Messages.get().container(Messages.RPT_CREATE_INDEX_0), I_CmsReport.FORMAT_NOTE);
945                 m_report.print(org.opencms.report.Messages.get().container(
946                     org.opencms.report.Messages.RPT_ARGUMENT_1,
947                     relativeFSName.replace('\\', '/')));
948                 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
949                 m_report.print(Messages.get().container(Messages.RPT_ARROW_RIGHT_0), I_CmsReport.FORMAT_NOTE);
950                 m_report.print(org.opencms.report.Messages.get().container(
951                     org.opencms.report.Messages.RPT_ARGUMENT_1,
952                     absoluteVFSName));
953                 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
954                 m_fileIndex.put(subresources[i].getAbsolutePath().replace('\\', '/'), absoluteVFSName);
955                 // if the subresource is a folder, get all subresources of it as well
956
if (subresources[i].isDirectory()) {
957                     buildIndex(subresources[i].getAbsolutePath());
958                 }
959                 m_report.println(
960                     org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
961                     I_CmsReport.FORMAT_OK);
962             } catch (Exception JavaDoc e) {
963                 LOG.error(e);
964                 m_report.println(e);
965             }
966         }
967     }
968
969     /**
970      * Builds a map with all parents of the destination dir to the real file system.
971      * So links to resources of outside the import folder can be found.
972      *
973      */

974     private void buildParentPath() {
975
976         String JavaDoc destFolder = m_destinationDir;
977         String JavaDoc inputDir = m_inputDir.replace('\\', '/');
978         if (!inputDir.endsWith("/")) {
979             inputDir += "/";
980         }
981         int pos = inputDir.lastIndexOf("/");
982         while ((pos > 0) && (destFolder != null)) {
983             inputDir = inputDir.substring(0, pos);
984             m_parents.put(inputDir + "/", destFolder);
985             
986             pos = inputDir.lastIndexOf("/", pos-1);
987             destFolder = CmsResource.getParentFolder(destFolder);
988         }
989     }
990
991     /**
992      * Clear all used indices and lists.<p>
993      *
994      * This should only be done when the import has been done.
995      */

996     private void clear() {
997
998         m_fileIndex = null;
999         m_externalLinks = null;
1000        m_imageInfo = null;
1001    }
1002
1003    /**
1004     * Copies all HTML files to the VFS.<p>
1005     *
1006     * @param startfolder startfolder the folder to start with
1007     *
1008     * @throws Exception if something goes wrong
1009     */

1010    private void copyHtmlFiles(String JavaDoc startfolder) throws Exception JavaDoc {
1011
1012        try {
1013            File JavaDoc folder = new File JavaDoc(startfolder);
1014            // get all subresources
1015
File JavaDoc[] subresources = folder.listFiles();
1016            // now loop through all subresources
1017
for (int i = 0; i < subresources.length; i++) {
1018                // if the subresource is a folder, get all subresources of it as well
1019
if (subresources[i].isDirectory()) {
1020                    // first, create the folder in the VFS
1021
Hashtable JavaDoc properties = new Hashtable JavaDoc();
1022                    createFolder(subresources[i].getAbsolutePath(), i, properties);
1023                    // now process all rescources inside of the folder
1024
copyHtmlFiles(subresources[i].getAbsolutePath());
1025                } else {
1026                    // create a new file in the VFS
1027
String JavaDoc vfsFileName = (String JavaDoc)m_fileIndex.get(subresources[i].getAbsolutePath().replace('\\', '/'));
1028                    // check if this is an Html file, do only import and parse those
1029
int type = getFileType(vfsFileName);
1030                    if (CmsResourceTypePlain.getStaticTypeId() == type) {
1031                        Hashtable JavaDoc properties = new Hashtable JavaDoc();
1032                        // the subresource is a file, so start the parsing process
1033
String JavaDoc content = new String JavaDoc();
1034                        try {
1035                            content = parseHtmlFile(subresources[i], properties);
1036                        } catch (CmsException e) {
1037                            m_report.println(e);
1038                        }
1039                        properties.put("template", m_template);
1040
1041                        // create the file in the VFS
1042
createFile(subresources[i].getAbsolutePath(), i, content, properties);
1043                    }
1044                }
1045            }
1046        } catch (Exception JavaDoc e) {
1047            LOG.error(e);
1048        }
1049    }
1050
1051    /**
1052     * Copies all files except HTML files to the VFS.<p>
1053     *
1054     * @param startfolder startfolder the folder to start with
1055     */

1056    private void copyOtherFiles(String JavaDoc startfolder) {
1057
1058        try {
1059            File JavaDoc folder = new File JavaDoc(startfolder);
1060            // get all subresources
1061
File JavaDoc[] subresources = folder.listFiles();
1062            // now loop through all subresources
1063
for (int i = 0; i < subresources.length; i++) {
1064                // if the subresource is a folder, get all subresources of it as well
1065
if (subresources[i].isDirectory()) {
1066                    copyOtherFiles(subresources[i].getAbsolutePath());
1067                } else {
1068                    // do not import the "meta.properties" file
1069
if (!subresources[i].getName().equals(META_PROPERTIES)) {
1070                        // create a new file in the VFS
1071
String JavaDoc vfsFileName = (String JavaDoc)m_fileIndex.get(subresources[i].getAbsolutePath().replace(
1072                            '\\',
1073                            '/'));
1074                        // get the file type of the FS file
1075
int type = getFileType(vfsFileName);
1076                        if (CmsResourceTypePlain.getStaticTypeId() != type) {
1077
1078                            if (isExternal(vfsFileName)) {
1079
1080                                m_report.print(
1081                                    Messages.get().container(Messages.RPT_SKIP_EXTERNAL_0),
1082                                    I_CmsReport.FORMAT_NOTE);
1083                                m_report.print(org.opencms.report.Messages.get().container(
1084                                    org.opencms.report.Messages.RPT_ARGUMENT_1,
1085                                    subresources[i]));
1086                                m_report.print(org.opencms.report.Messages.get().container(
1087                                    org.opencms.report.Messages.RPT_DOTS_0));
1088                                m_report.print(
1089                                    Messages.get().container(Messages.RPT_ARROW_RIGHT_0),
1090                                    I_CmsReport.FORMAT_NOTE);
1091                                m_report.println(org.opencms.report.Messages.get().container(
1092                                    org.opencms.report.Messages.RPT_ARGUMENT_1,
1093                                    vfsFileName));
1094                            } else {
1095
1096                                m_report.print(Messages.get().container(Messages.RPT_IMPORT_0), I_CmsReport.FORMAT_NOTE);
1097                                m_report.print(org.opencms.report.Messages.get().container(
1098                                    org.opencms.report.Messages.RPT_ARGUMENT_1,
1099                                    vfsFileName));
1100                                m_report.print(org.opencms.report.Messages.get().container(
1101                                    org.opencms.report.Messages.RPT_DOTS_0));
1102
1103                                // get the content of the FS file
1104
byte[] content = getFileBytes(subresources[i]);
1105                                // get the filename from the fileIndex list
1106

1107                                // check if there are some image infos stored for this resource
1108
List JavaDoc properties = new ArrayList JavaDoc();
1109                                String JavaDoc altText = (String JavaDoc)m_imageInfo.get(subresources[i].getAbsolutePath().replace(
1110                                    '\\',
1111                                    '/'));
1112                                CmsProperty property1 = new CmsProperty(
1113                                    CmsPropertyDefinition.PROPERTY_DESCRIPTION,
1114                                    altText,
1115                                    altText);
1116                                CmsProperty property2 = new CmsProperty(
1117                                    CmsPropertyDefinition.PROPERTY_TITLE,
1118                                    altText,
1119                                    altText);
1120                                // add them to the title and description property
1121
if (altText != null) {
1122                                    properties.add(property1);
1123                                    properties.add(property2);
1124                                }
1125                                // create the file
1126
if (!m_overwriteMode) {
1127                                    m_cmsObject.createResource(vfsFileName, type, content, properties);
1128                                } else {
1129                                    try {
1130                                        CmsLock lock = m_cmsObject.getLock(vfsFileName);
1131                                        if (lock.getType() != CmsLock.TYPE_EXCLUSIVE) {
1132                                            m_cmsObject.lockResource(vfsFileName);
1133                                        }
1134                                        m_cmsObject.deleteResource(vfsFileName, CmsResource.DELETE_PRESERVE_SIBLINGS);
1135                                    } catch (CmsException e) {
1136                                        // the file did not exist, so create it
1137
} finally {
1138                                        m_cmsObject.createResource(vfsFileName, type, content, properties);
1139                                    }
1140
1141                                    m_report.print(
1142                                        Messages.get().container(Messages.RPT_OVERWRITE_0),
1143                                        I_CmsReport.FORMAT_NOTE);
1144                                    m_report.print(org.opencms.report.Messages.get().container(
1145                                        org.opencms.report.Messages.RPT_DOTS_0));
1146                                }
1147                                m_report.println(org.opencms.report.Messages.get().container(
1148                                    org.opencms.report.Messages.RPT_OK_0), I_CmsReport.FORMAT_OK);
1149                            }
1150                        }
1151                    }
1152                }
1153            }
1154        } catch (Exception JavaDoc e) {
1155            LOG.error(e);
1156            m_report.println(e);
1157        }
1158    }
1159
1160    /**
1161     * Creates all external links, which were found during the HTML-page processing.<p>
1162     *
1163     */

1164    private void createExternalLinks() {
1165
1166        // loop through all links
1167
Iterator JavaDoc i = m_externalLinks.iterator();
1168        while (i.hasNext()) {
1169            String JavaDoc linkUrl = (String JavaDoc)i.next();
1170            String JavaDoc filename = getExternalLinkFile(linkUrl);
1171
1172            m_report.print(Messages.get().container(Messages.RPT_CREATE_EXTERNAL_LINK_0), I_CmsReport.FORMAT_NOTE);
1173            m_report.print(org.opencms.report.Messages.get().container(
1174                org.opencms.report.Messages.RPT_ARGUMENT_1,
1175                filename));
1176            m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
1177
1178            List JavaDoc properties = new ArrayList JavaDoc();
1179            CmsProperty property1 = new CmsProperty(
1180                CmsPropertyDefinition.PROPERTY_TITLE,
1181                "Link to " + linkUrl,
1182                "Link to " + linkUrl);
1183            properties.add(property1);
1184            try {
1185                m_cmsObject.createResource(
1186                    m_linkGallery + filename,
1187                    CmsResourceTypePointer.getStaticTypeId(),
1188                    linkUrl.getBytes(),
1189                    properties);
1190            } catch (CmsException e) {
1191                // do nothing here, an exception will be thrown if this link already exisits
1192
}
1193            m_report.println(
1194                org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
1195                I_CmsReport.FORMAT_OK);
1196        }
1197    }
1198
1199    /**
1200     * Creates a file in the VFS.<p>
1201     *
1202     * @param filename the complete filename in the real file system
1203     * @param position the default nav pos of this folder
1204     * @param content the html content of the file
1205     * @param properties the file properties
1206     */

1207    private void createFile(String JavaDoc filename, int position, String JavaDoc content, Hashtable JavaDoc properties) {
1208
1209        String JavaDoc vfsFileName = (String JavaDoc)m_fileIndex.get(filename.replace('\\', '/'));
1210
1211        if (vfsFileName != null) {
1212            try {
1213
1214                m_report.print(Messages.get().container(Messages.RPT_CREATE_FILE_0), I_CmsReport.FORMAT_NOTE);
1215                m_report.print(org.opencms.report.Messages.get().container(
1216                    org.opencms.report.Messages.RPT_ARGUMENT_1,
1217                    vfsFileName));
1218                m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
1219
1220                // check if we have to set the navpos property.
1221
if ((properties.get(CmsPropertyDefinition.PROPERTY_NAVPOS) == null)
1222                    && (properties.get(CmsPropertyDefinition.PROPERTY_NAVTEXT) != null)) {
1223                    // set the position in the folder as navpos
1224
// we have to add one to the postion, since it is counted from 0
1225
properties.put(CmsPropertyDefinition.PROPERTY_NAVPOS, (position + 1) + "");
1226                }
1227
1228                // create new xml page
1229
CmsXmlPage page = new CmsXmlPage(m_locale, OpenCms.getSystemInfo().getDefaultEncoding());
1230                page.addValue(m_element, m_locale);
1231                page.setStringValue(m_cmsObject, m_element, m_locale, content);
1232
1233                // check links
1234
CmsLinkTable linkTable = page.getLinkTable(m_element, m_locale);
1235                Iterator JavaDoc i = linkTable.iterator();
1236                while (i.hasNext()) {
1237                    CmsLink link = (CmsLink)i.next();
1238                    String JavaDoc target = link.getTarget();
1239                    // do only update internal links
1240
if (target.indexOf("://") == 0) {
1241                        //if (!target.startsWith("http") && !target.startsWith("mailto")) {
1242
target = m_cmsObject.getRequestContext().getFileTranslator().translateResource(target);
1243                        // update link
1244
link.updateLink(target, link.getAnchor(), link.getQuery());
1245                    }
1246                }
1247                // marshal xml page and get the content
1248
byte[] contentByteArray = page.marshal();
1249                List JavaDoc oldProperties = new ArrayList JavaDoc();
1250
1251                if (!m_overwriteMode) {
1252                    m_cmsObject.createResource(
1253                        vfsFileName,
1254                        CmsResourceTypeXmlPage.getStaticTypeId(),
1255                        contentByteArray,
1256                        new ArrayList JavaDoc());
1257                } else {
1258                    try {
1259                        // try if the file is there
1260
oldProperties = m_cmsObject.readPropertyObjects(vfsFileName, false);
1261                        CmsLock lock = m_cmsObject.getLock(vfsFileName);
1262                        if (lock.getType() != CmsLock.TYPE_EXCLUSIVE) {
1263                            m_cmsObject.lockResource(vfsFileName);
1264                        }
1265                        m_cmsObject.deleteResource(vfsFileName, CmsResource.DELETE_PRESERVE_SIBLINGS);
1266                    } catch (CmsException e) {
1267                        // the file did not exist, so we do not have to delete it
1268
} finally {
1269                        // create the new resource
1270
m_report.print(Messages.get().container(Messages.RPT_OVERWRITE_0), I_CmsReport.FORMAT_NOTE);
1271                        m_report.print(org.opencms.report.Messages.get().container(
1272                            org.opencms.report.Messages.RPT_DOTS_0));
1273                        m_cmsObject.createResource(
1274                            vfsFileName,
1275                            CmsResourceTypeXmlPage.getStaticTypeId(),
1276                            contentByteArray,
1277                            new ArrayList JavaDoc());
1278                    }
1279                }
1280                // create all properties and put them in an ArrayList
1281
Enumeration JavaDoc en = properties.keys();
1282                List JavaDoc propertyList = new ArrayList JavaDoc();
1283                while (en.hasMoreElements()) {
1284                    // get property and value
1285
String JavaDoc propertyKey = (String JavaDoc)en.nextElement();
1286                    String JavaDoc propertyVal = (String JavaDoc)properties.get(propertyKey);
1287                    // create new Property Object
1288
CmsProperty property = new CmsProperty(propertyKey, propertyVal, propertyVal);
1289                    // create implicitly if Property doesn't exist already
1290
property.setAutoCreatePropertyDefinition(true);
1291                    // add new property to the list
1292
propertyList.add(property);
1293                }
1294                // try to write the property
1295
try {
1296                    m_cmsObject.writePropertyObjects(vfsFileName, propertyList);
1297                    // write the old properties if available
1298
m_cmsObject.writePropertyObjects(vfsFileName, oldProperties);
1299                } catch (CmsException e1) {
1300                    e1.printStackTrace();
1301                }
1302                m_report.println(
1303                    org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
1304                    I_CmsReport.FORMAT_OK);
1305            } catch (CmsException e) {
1306                m_report.println(e);
1307                LOG.error(e);
1308            }
1309        }
1310    }
1311
1312    /**
1313     * Creates a folder in the VFS.<p>
1314     *
1315     * @param foldername the complete foldername in the real file system
1316     * @param position the default nav pos of this folder
1317     * @param properties the file properties
1318     */

1319    private void createFolder(String JavaDoc foldername, int position, Hashtable JavaDoc properties) {
1320
1321        String JavaDoc vfsFolderName = (String JavaDoc)m_fileIndex.get(foldername.replace('\\', '/'));
1322
1323        m_report.print(Messages.get().container(Messages.RPT_CREATE_FOLDER_0), I_CmsReport.FORMAT_NOTE);
1324        m_report.print(org.opencms.report.Messages.get().container(
1325            org.opencms.report.Messages.RPT_ARGUMENT_1,
1326            vfsFolderName));
1327        m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
1328
1329        if (vfsFolderName != null) {
1330            String JavaDoc path = vfsFolderName.substring(
1331                0,
1332                vfsFolderName.substring(0, vfsFolderName.length() - 1).lastIndexOf("/"));
1333            String JavaDoc folder = vfsFolderName.substring(path.length(), vfsFolderName.length());
1334            try {
1335                // try to find a meta.properties file in the folder
1336
String JavaDoc propertyFileName = foldername + File.separator + META_PROPERTIES;
1337
1338                boolean metaPropertiesFound = false;
1339                ExtendedProperties propertyFile = new ExtendedProperties();
1340                try {
1341                    propertyFile.load(new FileInputStream JavaDoc(new File JavaDoc(propertyFileName)));
1342                    metaPropertiesFound = true;
1343                } catch (Exception JavaDoc e1) {
1344                    // do nothing if the propertyfile could not be loaded since it is not required
1345
// that such s file does exist
1346
}
1347                // now copy all values from the propertyfile to the already found properties of the
1348
// new folder in OpenCms
1349
// only do this if we have found a meta.properties file
1350
if (metaPropertiesFound) {
1351                    Enumeration JavaDoc enu = propertyFile.keys();
1352                    String JavaDoc property = "";
1353                    while (enu.hasMoreElements()) {
1354                        // get property and value
1355
try {
1356                            property = (String JavaDoc)enu.nextElement();
1357                            String JavaDoc propertyvalue = (String JavaDoc)propertyFile.get(property);
1358                            // copy to the properties of the OpenCms folder
1359
properties.put(property, propertyvalue);
1360                        } catch (Exception JavaDoc e2) {
1361                            // just skip this property if it could ne be read.
1362
e2.printStackTrace();
1363                        }
1364                    }
1365
1366                    // check if we have to set the navpos property.
1367
if (properties.get(CmsPropertyDefinition.PROPERTY_NAVPOS) == null) {
1368                        // set the position in the folder as navpos
1369
// we have to add one to the postion, since it is counted from 0
1370
properties.put(CmsPropertyDefinition.PROPERTY_NAVPOS, (position + 1) + "");
1371                    }
1372                    // check if we have to set the navpos property.
1373
if (properties.get(CmsPropertyDefinition.PROPERTY_NAVTEXT) == null) {
1374                        // set the foldername in the folder as navtext
1375
String JavaDoc navtext = folder.substring(1, 2).toUpperCase()
1376                            + folder.substring(2, folder.length() - 1);
1377                        properties.put(CmsPropertyDefinition.PROPERTY_NAVTEXT, navtext);
1378                    }
1379                } else {
1380                    // if there was no meta.properties file, no properties should be added to the
1381
// folder
1382
properties = new Hashtable JavaDoc();
1383                }
1384                // try to read the folder, it its there we must not create it again
1385
try {
1386                    m_cmsObject.readFolder(path + folder);
1387                    m_cmsObject.lockResource(path + folder);
1388                } catch (CmsException e1) {
1389                    // the folder was not there, so create it
1390
m_cmsObject.createResource(path + folder, CmsResourceTypeFolder.getStaticTypeId());
1391                }
1392                // create all properties and put them in an ArrayList
1393
Enumeration JavaDoc enu = properties.keys();
1394                List JavaDoc propertyList = new ArrayList JavaDoc();
1395                while (enu.hasMoreElements()) {
1396                    // get property and value
1397
String JavaDoc propertyKey = (String JavaDoc)enu.nextElement();
1398                    String JavaDoc propertyVal = (String JavaDoc)properties.get(propertyKey);
1399                    CmsProperty property = new CmsProperty(propertyKey, propertyVal, propertyVal);
1400                    // create implicitly if Property doesn't exist already
1401
property.setAutoCreatePropertyDefinition(true);
1402                    // add new property to the list
1403
propertyList.add(property);
1404                }
1405                // try to write the property Objects
1406
try {
1407                    m_cmsObject.writePropertyObjects(path + folder, propertyList);
1408                } catch (CmsException e1) {
1409                    e1.printStackTrace();
1410                }
1411                m_report.println(
1412                    org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
1413                    I_CmsReport.FORMAT_OK);
1414            } catch (CmsException e) {
1415                m_report.println(e);
1416                LOG.error(e);
1417            }
1418        }
1419    }
1420
1421    /**
1422     * Compares 2 pathes for the base part which have both equal.
1423     *
1424     * @param path1 the first path to compare
1425     * @param path2 the second path to compare
1426     * @return the base path of both which are equal
1427     */

1428    private String JavaDoc getBasePath(String JavaDoc path1, String JavaDoc path2) {
1429
1430        StringBuffer JavaDoc base = new StringBuffer JavaDoc();
1431        path1 = path1.replace('\\', '/');
1432        path2 = path2.replace('\\', '/');
1433
1434        String JavaDoc[] parts1 = path1.split("/");
1435        String JavaDoc[] parts2 = path2.split("/");
1436
1437        for (int i = 0; i < parts1.length; i++) {
1438            if (i >= parts2.length) {
1439                break;
1440            }
1441            if (parts1[i].equals(parts2[i])) {
1442                base.append(parts1[i] + "/");
1443            }
1444        }
1445
1446        return base.toString();
1447    }
1448
1449    /**
1450     * Creates the filename of the file of the external link.
1451     *
1452     * @param link the link to get the file path for.
1453     * @return the filename of the file for the external link.
1454     */

1455    private String JavaDoc getExternalLinkFile(String JavaDoc link) {
1456
1457        String JavaDoc filename = link.substring(link.indexOf("://") + 3, link.length());
1458        filename = m_cmsObject.getRequestContext().getFileTranslator().translateResource(filename.replace('/', '-'));
1459        return filename;
1460    }
1461
1462    /**
1463     * Returns a byte array containing the content of server FS file.<p>
1464     *
1465     * @param file the name of the file to read
1466     * @return bytes[] the content of the file
1467     * @throws CmsException if something goes wrong
1468     */

1469    private byte[] getFileBytes(File JavaDoc file) throws CmsException {
1470
1471        byte[] buffer = null;
1472
1473        FileInputStream JavaDoc fileStream = null;
1474        int charsRead;
1475        int size;
1476        try {
1477            fileStream = new FileInputStream JavaDoc(file);
1478            charsRead = 0;
1479            size = new Long JavaDoc(file.length()).intValue();
1480            buffer = new byte[size];
1481            while (charsRead < size) {
1482                charsRead += fileStream.read(buffer, charsRead, size - charsRead);
1483            }
1484            return buffer;
1485        } catch (IOException JavaDoc e) {
1486            throw new CmsDbIoException(
1487                Messages.get().container(Messages.ERR_GET_FILE_BYTES_1, file.getAbsolutePath()),
1488                e);
1489        } finally {
1490            try {
1491                if (fileStream != null) {
1492                    fileStream.close();
1493                }
1494            } catch (IOException JavaDoc e) {
1495                e.printStackTrace();
1496            }
1497        }
1498    }
1499
1500    /**
1501     * Returns the OpenCms file type of a real filesystem file. <p>
1502     *
1503     * This is made by checking the extension.
1504     *
1505     * @param filename the name of the file in the real filesystem
1506     *
1507     * @return the id of the OpenCms file type
1508     *
1509     * @throws Exception if something goes wrong
1510     */

1511    private int getFileType(String JavaDoc filename) throws Exception JavaDoc {
1512
1513        String JavaDoc extension = "";
1514        if (filename.indexOf(".") > -1) {
1515            extension = filename.substring((filename.lastIndexOf(".")));
1516        }
1517
1518        String JavaDoc typename = (String JavaDoc)m_extensions.get(extension.toLowerCase());
1519        if (typename == null) {
1520            typename = "binary";
1521        }
1522        CmsResourceManager resourceManager = OpenCms.getResourceManager();
1523
1524        return resourceManager.getResourceType(typename).getTypeId();
1525    }
1526
1527    /**
1528     * Gets a valid VfsName form a given name in the real filesystem.<p>
1529     *
1530     * This name will ater be used for all link translations during the HTML-parsing process.
1531     * @param relativeName the name in the real fielsystem, relative to the start folder
1532     * @param name the name of the file
1533     * @param isFile flag to indicate that the resource is a file
1534     *
1535     * @return a valid name in the VFS
1536     *
1537     * @throws Exception if something goes wrong
1538     */

1539    private String JavaDoc getVfsName(String JavaDoc relativeName, String JavaDoc name, boolean isFile) throws Exception JavaDoc {
1540
1541        // first translate all fileseperators to the valid "/" in OpenCms
1542
String JavaDoc vfsName = relativeName.replace('\\', '/');
1543        // the resource is a file
1544
if (isFile) {
1545            // we must check if it might be copied into a gallery. this can be done by checking the
1546
// file extension
1547
int filetype = getFileType(name);
1548
1549            // there is no name before the ".extension"
1550
if (name.indexOf(".") == 0) {
1551                name = "unknown" + name;
1552                int dot = relativeName.lastIndexOf(".");
1553
1554                relativeName = relativeName.substring(0, dot) + name;
1555            }
1556
1557            // depending on the filetype, the resource must be moved into a special folder in
1558
// OpenCms:
1559
// images -> move into image gallery, if flag to leave at original location is off
1560
// binary -> move into download gallery, if flag to leaave at original location is off
1561
// plain -> move into destination folder
1562
// other -> move into download gallery, if flag to leaave at original location is off
1563
if ((CmsResourceTypeImage.getStaticTypeId() == filetype) && (!m_leaveImagesMode)) {
1564
1565                // move to image gallery
1566
// as the image gallery is "flat", we must use the file name and not the complete
1567
// relative name
1568
vfsName = m_imageGallery + name;
1569            } else if ((CmsResourceTypePlain.getStaticTypeId() == filetype)
1570                || (m_leaveImagesMode)
1571                || (m_leaveDownloadsMode)) {
1572                // move to destination folder
1573
//vfsName=m_destinationDir+relativeName;
1574

1575                // we have to check if there is a folder with the same name but without extension
1576
// if so, we will move the file into the folder and name it "index.html"
1577
String JavaDoc folderName = relativeName;
1578                if (folderName.indexOf(".") > 0) {
1579                    folderName = folderName.substring(0, folderName.indexOf("."));
1580                }
1581                folderName = m_inputDir + "\\" + folderName;
1582                File JavaDoc folder = new File JavaDoc(folderName);
1583
1584                if ((folder != null) && (folder.isDirectory())) {
1585                    vfsName = m_destinationDir + relativeName.substring(0, relativeName.indexOf(".")) + "/index.html";
1586                    // System.err.println("MOVING "+ relativeName + " -> " + name.substring(0,name.indexOf("."))+"/index.html");
1587
} else {
1588                    // move to destination folder
1589
vfsName = m_destinationDir + relativeName;
1590                }
1591
1592            } else {
1593                // everything else will be moved to the download gallery.
1594
// as the download gallery is "flat", we must use the file name and not the complete
1595
// relative name
1596
vfsName = m_downloadGallery + name;
1597            }
1598            // now we have the filename in the VFS. its possible that a file with the same name
1599
// is already existing, in this case, we have to adjust the filename.
1600
return validateFilename(vfsName);
1601        } else {
1602            // folders are always moved to the destination folder
1603
vfsName = m_destinationDir + vfsName + "/";
1604            return vfsName;
1605        }
1606    }
1607
1608    /**
1609     * Tests if a filename is an external name, i.e. this name does not point into the OpenCms Vfs.<p>
1610     *
1611     * A filename is an external name if it contains the string "://", e.g. "http://" or "ftp://"
1612     * @param filename the filename to test
1613     * @return true or false
1614     */

1615    private boolean isExternal(String JavaDoc filename) {
1616
1617        boolean external = false;
1618        if (filename.indexOf("://") > 0) {
1619            external = true;
1620        }
1621        return external;
1622    }
1623
1624    /**
1625     * Checks if m_element is valid element.<p>
1626     *
1627     * @return true if element is valid, otherwise false
1628     */

1629    private boolean isValidElement() {
1630
1631        boolean validElement = false;
1632        List JavaDoc elementList = new ArrayList JavaDoc();
1633        try {
1634            // get Elements of template stored in Property "template-elements"
1635
String JavaDoc elements = m_cms.property(CmsPropertyDefinition.PROPERTY_TEMPLATE_ELEMENTS, m_template);
1636            // template may contain more than one Element
1637
// Elements are seperated by the delimiter ","
1638
if (elements != null) {
1639                StringTokenizer JavaDoc T = new StringTokenizer JavaDoc(elements, ",");
1640                while (T.hasMoreTokens()) {
1641                    // current element probably looks like "body*|Body" <name><mandatory>|<nicename>
1642
String JavaDoc currentElement = T.nextToken();
1643                    int sepIndex = currentElement.indexOf("|");
1644                    if (sepIndex != -1) {
1645                        // current element == "body*"
1646
currentElement = currentElement.substring(0, sepIndex);
1647                    }
1648                    if (currentElement.endsWith("*")) {
1649                        // current element == "body"
1650
currentElement = currentElement.substring(0, currentElement.length() - 1);
1651                    }
1652                    elementList.add(currentElement);
1653                }
1654            }
1655            if (elementList.contains(m_element)) {
1656                validElement = true;
1657            }
1658        } catch (Exception JavaDoc e) {
1659            e.printStackTrace();
1660        }
1661
1662        return validElement;
1663    }
1664
1665    /**
1666     * Reads the content of an Html file from the real file system and parses it for link
1667     * transformation.<p>
1668     *
1669     * @param file the filein the real file system
1670     * @param properties the file properties
1671     * @return the modified Html code of the file
1672     * @throws CmsException if something goes wrong
1673     */

1674    private String JavaDoc parseHtmlFile(File JavaDoc file, Hashtable JavaDoc properties) throws CmsException {
1675
1676        String JavaDoc parsedHtml = "";
1677        try {
1678
1679            byte[] content = getFileBytes(file);
1680
1681            // use the correct encoding to get the string from the file bytes
1682
String JavaDoc contentString = new String JavaDoc(content, m_inputEncoding);
1683            // escape the string to remove all special chars
1684
contentString = CmsEncoder.escapeNonAscii(contentString);
1685            // we must substitute all occurences of "&#", otherwiese tidy would remove them
1686
contentString = CmsStringUtil.substitute(contentString, "&#", "{subst}");
1687            // parse the content
1688
parsedHtml = m_htmlConverter.convertHTML(
1689                file.getAbsolutePath(),
1690                contentString,
1691                m_startPattern,
1692                m_endPattern,
1693                properties);
1694            // resubstidute the converted HTML code
1695
parsedHtml = CmsStringUtil.substitute(parsedHtml, "{subst}", "&#");
1696        } catch (Exception JavaDoc e) {
1697            CmsMessageContainer message = Messages.get().container(
1698                Messages.ERR_HTMLIMPORT_PARSE_1,
1699                file.getAbsolutePath());
1700            LOG.error(message.key(), e);
1701            throw new CmsImportExportException(message, e);
1702        }
1703        return parsedHtml;
1704    }
1705
1706    /**
1707     * Validates a fielname for OpenCms.<p>
1708     *
1709     * This method checks if there are any illegal characters in the fielname and modifies them
1710     * if nescessary. In addition it ensures that no dublicate filenames are created.
1711     *
1712     * @param filename the filename to validate
1713     * @return a validated and unique filename in OpenCms
1714     */

1715    private String JavaDoc validateFilename(String JavaDoc filename) {
1716
1717        // if its an external filename, use it directley
1718
if (isExternal(filename)) {
1719            return filename;
1720        }
1721
1722        // check if this resource name does already exist
1723
// if so add a postfix to the name
1724

1725        int postfix = 1;
1726        boolean found = true;
1727        String JavaDoc validFilename = filename;
1728
1729        // if we are not in overwrite mode, we must find a valid, non-existing filename
1730
// otherwise we will use the current translated name
1731
if (!m_overwriteMode) {
1732
1733            while (found) {
1734                try {
1735                    // get the translated name, this one only contains valid chars in OpenCms
1736
validFilename = m_cmsObject.getRequestContext().getFileTranslator().translateResource(validFilename);
1737
1738                    // try to read the file.....
1739
found = true;
1740                    // first try to read it form the fileIndex of already processed files
1741
if (!m_fileIndex.containsValue(validFilename.replace('\\', '/'))) {
1742                        found = false;
1743                    }
1744                    if (!found) {
1745                        found = true;
1746                        // there was no entry in the fileIndex, so try to read from the VFS
1747
m_cmsObject.readResource(validFilename, CmsResourceFilter.ALL);
1748                    }
1749                    // ....it's there, so add a postfix and try again
1750
String JavaDoc path = filename.substring(0, filename.lastIndexOf("/") + 1);
1751                    String JavaDoc name = filename.substring(filename.lastIndexOf("/") + 1, filename.length());
1752                    validFilename = path;
1753                    if (name.lastIndexOf(".") > 0) {
1754                        validFilename += name.substring(0, name.lastIndexOf("."));
1755                    } else {
1756                        validFilename += name;
1757                    }
1758                    validFilename += "_" + postfix;
1759                    if (name.lastIndexOf(".") > 0) {
1760                        validFilename += name.substring(name.lastIndexOf("."), name.length());
1761                    }
1762                    postfix++;
1763                } catch (CmsException e) {
1764                    // the file does not exist, so we can use this filename
1765
found = false;
1766                }
1767            }
1768
1769        } else {
1770            validFilename = validFilename.replace('\\', '/');
1771        }
1772
1773        return OpenCms.getResourceManager().getFileTranslator().translateResource(validFilename);
1774    }
1775
1776}
1777
Popular Tags