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