001    /*
002     * This file is part of muCommander, http://www.mucommander.com
003     * Copyright (C) 2002-2008 Maxence Bernard
004     *
005     * muCommander is free software; you can redistribute it and/or modify
006     * it under the terms of the GNU General Public License as published by
007     * the Free Software Foundation; either version 3 of the License, or
008     * (at your option) any later version.
009     *
010     * muCommander is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    
019    package com.mucommander.command;
020    
021    import com.mucommander.xml.XmlAttributes;
022    import com.mucommander.xml.XmlWriter;
023    
024    import java.io.IOException;
025    import java.io.OutputStream;
026    
027    /**
028     * Class used to write custom associations XML files.
029     * <p>
030     * <code>AssociationWriter</code> is an {@link AssociationBuilder} that will send
031     * all build messages it receives into an XML stream (as defined in {@link AssociationsXmlConstants}).
032     * </p>
033     * @author Nicolas Rinaudo
034     */
035    public class AssociationWriter implements AssociationsXmlConstants, AssociationBuilder {
036        // - Instance variables --------------------------------------------------
037        // -----------------------------------------------------------------------
038        /** Where to write the custom command associations to. */
039        private XmlWriter out;
040    
041    
042    
043        // - Initialisation ------------------------------------------------------
044        // -----------------------------------------------------------------------
045        /**
046         * Builds a new writer that will send data to the specified output stream.
047         * @param  stream      where to write the XML data.
048         * @throws IOException if an I/O error occurs.
049         */
050        public AssociationWriter(OutputStream stream) throws IOException {out = new XmlWriter(stream);}
051    
052    
053    
054        // - Builder methods ------------------------------------------------------
055        // -----------------------------------------------------------------------
056        /**
057         * Opens the root XML element.
058         */
059        public void startBuilding() throws CommandException {
060            try {
061                out.startElement(ELEMENT_ROOT);
062                out.println();
063            }
064            catch(IOException e) {throw new CommandException(e);}
065        }
066    
067        /**
068         * Closes the root XML element.
069         */
070        public void endBuilding() throws CommandException {
071            try {out.endElement(ELEMENT_ROOT);}
072            catch(IOException e) {throw new CommandException(e);}
073        }
074    
075        public void startAssociation(String command) throws CommandException {
076            XmlAttributes attr;
077    
078            attr = new XmlAttributes();
079            attr.add(ATTRIBUTE_COMMAND, command);
080    
081            try {
082                out.startElement(ELEMENT_ASSOCIATION, attr);
083                out.println();
084            }
085            catch(IOException e) {throw new CommandException(e);}
086        }
087    
088        public void endAssociation() throws CommandException {
089            try {out.endElement(ELEMENT_ASSOCIATION);}
090            catch(IOException e) {throw new CommandException(e);}
091        }
092    
093        public void setMask(String mask, boolean isCaseSensitive) throws CommandException {
094            XmlAttributes attr;
095    
096            attr = new XmlAttributes();
097            attr.add(ATTRIBUTE_VALUE, mask);
098            if(!isCaseSensitive)
099                attr.add(ATTRIBUTE_CASE_SENSITIVE, VALUE_FALSE);
100    
101            try {out.writeStandAloneElement(ELEMENT_MASK, attr);}
102            catch(IOException e) {throw new CommandException(e);}
103        }
104    
105        public void setIsSymlink(boolean isSymlink) throws CommandException {
106            XmlAttributes attr;
107    
108            attr = new XmlAttributes();
109            attr.add(ATTRIBUTE_VALUE, isSymlink ? VALUE_TRUE : VALUE_FALSE);
110    
111            try {out.writeStandAloneElement(ELEMENT_IS_SYMLINK, attr);}
112            catch(IOException e) {throw new CommandException(e);}
113        }
114    
115        public void setIsHidden(boolean isHidden) throws CommandException {
116            XmlAttributes attr;
117    
118            attr = new XmlAttributes();
119            attr.add(ATTRIBUTE_VALUE, isHidden ? VALUE_TRUE : VALUE_FALSE);
120    
121            try {out.writeStandAloneElement(ELEMENT_IS_HIDDEN, attr);}
122            catch(IOException e) {throw new CommandException(e);}
123        }
124    
125        public void setIsReadable(boolean isReadable) throws CommandException {
126            XmlAttributes attr;
127    
128            attr = new XmlAttributes();
129            attr.add(ATTRIBUTE_VALUE, isReadable ? VALUE_TRUE : VALUE_FALSE);
130    
131            try {out.writeStandAloneElement(ELEMENT_IS_READABLE, attr);}
132            catch(IOException e) {throw new CommandException(e);}
133        }
134    
135        public void setIsWritable(boolean isWritable) throws CommandException {
136            XmlAttributes attr;
137    
138            attr = new XmlAttributes();
139            attr.add(ATTRIBUTE_VALUE, isWritable ? VALUE_TRUE : VALUE_FALSE);
140    
141            try {out.writeStandAloneElement(ELEMENT_IS_WRITABLE, attr);}
142            catch(IOException e) {throw new CommandException(e);}
143        }
144    
145        public void setIsExecutable(boolean isExecutable) throws CommandException {
146            XmlAttributes attr;
147    
148            attr = new XmlAttributes();
149            attr.add(ATTRIBUTE_VALUE, isExecutable ? VALUE_TRUE : VALUE_FALSE);
150    
151            try {out.writeStandAloneElement(ELEMENT_IS_EXECUTABLE, attr);}
152            catch(IOException e) {throw new CommandException(e);}
153        }
154    }