001    /****************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one   *
003     * or more contributor license agreements.  See the NOTICE file *
004     * distributed with this work for additional information        *
005     * regarding copyright ownership.  The ASF licenses this file   *
006     * to you under the Apache License, Version 2.0 (the            *
007     * "License"); you may not use this file except in compliance   *
008     * with the License.  You may obtain a copy of the License at   *
009     *                                                              *
010     *   http://www.apache.org/licenses/LICENSE-2.0                 *
011     *                                                              *
012     * Unless required by applicable law or agreed to in writing,   *
013     * software distributed under the License is distributed on an  *
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015     * KIND, either express or implied.  See the License for the    *
016     * specific language governing permissions and limitations      *
017     * under the License.                                           *
018     ****************************************************************/
019    
020    package org.apache.james.mime4j.field;
021    
022    import org.apache.james.mime4j.codec.DecodeMonitor;
023    import org.apache.james.mime4j.dom.FieldParser;
024    import org.apache.james.mime4j.dom.field.ContentLocationField;
025    import org.apache.james.mime4j.stream.Field;
026    import org.apache.james.mime4j.stream.ParserCursor;
027    import org.apache.james.mime4j.stream.RawField;
028    import org.apache.james.mime4j.stream.RawFieldParser;
029    import org.apache.james.mime4j.util.ByteSequence;
030    import org.apache.james.mime4j.util.CharsetUtil;
031    import org.apache.james.mime4j.util.ContentUtil;
032    
033    /**
034     * Represents a <code>Content-Location</code> field.
035     */
036    public class ContentLocationFieldLenientImpl extends AbstractField implements ContentLocationField {
037    
038        private boolean parsed = false;
039        private String location;
040    
041        ContentLocationFieldLenientImpl(Field rawField, DecodeMonitor monitor) {
042            super(rawField, monitor);
043        }
044    
045        private void parse() {
046            parsed = true;
047            location = null;
048            RawField f = getRawField();
049            ByteSequence buf = f.getRaw();
050            int pos = f.getDelimiterIdx() + 1;
051            if (buf == null) {
052                String body = f.getBody();
053                if (body == null) {
054                    return;
055                }
056                buf = ContentUtil.encode(body);
057                pos = 0;
058            }
059            RawFieldParser parser = RawFieldParser.DEFAULT;
060            ParserCursor cursor = new ParserCursor(pos, buf.length());
061            String token = parser.parseValue(buf, cursor, null);
062            StringBuilder sb = new StringBuilder(token.length());
063            for (int i = 0; i < token.length(); i++) {
064                char ch = token.charAt(i);
065                if (!CharsetUtil.isWhitespace(ch)) {
066                    sb.append(ch);
067                }
068            }
069            this.location = sb.toString();
070        }
071    
072        public String getLocation() {
073            if (!parsed) {
074                parse();
075            }
076            return location;
077        }
078    
079        public static final FieldParser<ContentLocationField> PARSER = new FieldParser<ContentLocationField>() {
080    
081            public ContentLocationField parse(final Field rawField, final DecodeMonitor monitor) {
082                return new ContentLocationFieldLenientImpl(rawField, monitor);
083            }
084    
085        };
086    
087    }
088