001    package railo.runtime.net.mail;
002    
003    import java.io.Serializable;
004    
005    import railo.runtime.op.Caster;
006    import railo.runtime.type.Array;
007    import railo.runtime.type.List;
008    
009    
010    /**
011     * 
012     */
013    public final class MailPart implements Serializable {
014        /**
015         * @see java.lang.Object#toString()
016         */
017        public String toString() {
018            return "railo.runtime.mail.MailPart(wraptext:"+wraptext+";type:"+type+";charset:"+charset+";body:"+body+";)";
019        }
020            /** IThe MIME media type of the part */
021        private boolean isHTML;
022            
023            /** Specifies the maximum line length, in characters of the mail text */
024            private int wraptext=-1;
025    
026            /** The character encoding in which the part text is encoded */
027            private String charset;
028    
029        private String body;
030            private String type;
031    
032        /**
033         * 
034         */
035        public void clear() {
036            isHTML=false;
037            type=null;
038            wraptext=-1;
039            charset=null;
040            body="null";
041        }   
042        
043        
044    
045        /**
046         * 
047         */
048        public MailPart() {
049        }
050    
051        /**
052         * @param charset
053         */
054        public MailPart(String charset) {
055            this.charset = charset;
056        }
057        /**
058         * @return Returns the body.
059         */
060        public String getBody() {
061            return wrap(body);
062        }
063        /**
064         * @param body The body to set.
065         */
066        public void setBody(String body) {
067            this.body = body;
068        }
069        /**
070         * @return Returns the charset.
071         */
072        public String getCharset() {
073            return charset;
074        }
075        /**
076         * @param charset The charset to set.
077         */
078        public void setCharset(String charset) {
079            this.charset = charset;
080        }
081        /**
082         * @return Returns the isHTML.
083         */
084        public boolean isHTML() {
085            return isHTML;
086        }
087        /**
088         * @param isHTML The type to set.
089         */
090        public void isHTML(boolean isHTML) {
091            this.isHTML = isHTML;
092        }
093        /**
094         * @return Returns the wraptext.
095         */
096        public int getWraptext() {
097            return wraptext;
098        }
099        
100        
101        /**
102             * @return the type
103             */
104            public String getType() {
105                    return type;
106            }
107    
108    
109    
110            /**
111             * @param type the type to set
112             */
113            public void setType(String type) {
114                    this.type = type;
115            }
116    
117    
118    
119            /**
120         * @param wraptext The wraptext to set.
121         */
122        public void setWraptext(int wraptext) {
123            this.wraptext = wraptext;
124        }
125    
126    
127            /**
128             * wraps a String to specified length
129             * @param str string to erap
130             * @return wraped String
131             */
132            private String wrap(String str) {
133                    if(body==null || wraptext<=0)return str;
134                    
135                    StringBuffer rtn=new StringBuffer();
136                    String ls=System.getProperty("line.separator");
137                    Array arr = List.listToArray(str,ls);
138                    int len=arr.size();
139                    
140                    for(int i=1;i<=len;i++) {
141                            rtn.append(wrapLine(Caster.toString(arr.get(i,""),"")));
142                            if(i+1<len)rtn.append(ls);
143                    }
144                    return rtn.toString();
145            }
146    
147            /**
148             * wrap a single line
149             * @param str
150             * @return wraped Line
151             */
152            private String wrapLine(String str) {
153                    int wtl=wraptext;
154                    
155                    if(str.length()<=wtl) return str;
156                    
157                    String sub=str.substring(0,wtl);
158                    String rest=str.substring(wtl);
159                    char firstR=rest.charAt(0);
160                    String ls=System.getProperty("line.separator");
161                    
162                    if(firstR==' ' || firstR=='\t') return sub+ls+wrapLine(rest.length()>1?rest.substring(1):"");
163                    
164                    
165                    int indexSpace = sub.lastIndexOf(' ');
166                    int indexTab = sub.lastIndexOf('\t');
167                    int index=indexSpace<=indexTab?indexTab:indexSpace;
168                    
169                    if(index==-1) return sub+ls+wrapLine(rest);
170                    return sub.substring(0,index) + ls + wrapLine(sub.substring(index+1)+rest);
171                    
172            }
173    }