All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.xml.msofficexml.docx.wordprocessingml.WmlHyperlink Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.xml.msofficexml.docx.wordprocessingml;

import java.net.URL;

import com.hfg.xml.XMLizable;
import com.hfg.xml.msofficexml.docx.Docx;
import com.hfg.xml.msofficexml.docx.DocxException;
import com.hfg.xml.msofficexml.docx.RelationshipXML;
import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlCharacterStyle;

//------------------------------------------------------------------------------
/**
 Represents an Office Open XML hyperlink (<w:hyperlink>) tag.

 @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class WmlHyperlink extends WmlXMLTag
{
   private URL mURL;

   //---------------------------------------------------------------------------
   public WmlHyperlink(URL inURL, Docx inDocx)
   {
      super(WmlXML.HYPERLINK, inDocx);

      if (null == inDocx)
      {
         throw new DocxException("No reference to the parent doc available!?");
      }

      mURL = inURL;

      // Ensure that the styles part has been initialized
      inDocx.getStylesPart();
   }

   //---------------------------------------------------------------------------
   // Since we need to know whether this is being added to the body, the header,
   // or the footer we can't establish the relationship in the constructor.
   public void register()
   {
      String id = getParentOfficeXMLPart().getRelationshipPart().addHyperlink(mURL);
      setAttribute(RelationshipXML.ID_ATT, id);
   }

   //---------------------------------------------------------------------------
   public WmlHyperlink setId(String inValue)
   {
      setAttribute(RelationshipXML.ID_ATT, inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public String getId()
   {
      return getAttributeValue(RelationshipXML.ID_ATT.getLocalName());
   }

   //---------------------------------------------------------------------------
   public WmlTextRun addTextRun()
   {
      WmlTextRun run = new WmlTextRun(getParentDoc());
      run.getProperties().setStyle(WmlCharacterStyle.HYPERLINK_STYLE_ID);
      addSubtag(run);

      return run;
   }

   //---------------------------------------------------------------------------
   public WmlTextRun addTextRun(String inContent)
   {
      WmlTextRun run = addTextRun();
      run.addText(inContent);

      return run;
   }

   //---------------------------------------------------------------------------
   @Override
   public void addSubtag(XMLizable inSubtag)
   {
      super.addSubtag(inSubtag);
      // If it's a textRun, make sure that it has the hyperlink style
      if (inSubtag instanceof WmlTextRun)
      {
         ((WmlTextRun)inSubtag).getProperties().setStyle(WmlCharacterStyle.HYPERLINK_STYLE_ID);
      }
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy