001package com.hfg.xml.msofficexml.docx.wordprocessingml; 002 003import java.net.URL; 004 005import com.hfg.xml.XMLizable; 006import com.hfg.xml.msofficexml.docx.Docx; 007import com.hfg.xml.msofficexml.docx.DocxException; 008import com.hfg.xml.msofficexml.docx.RelationshipXML; 009import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlCharacterStyle; 010 011//------------------------------------------------------------------------------ 012/** 013 Represents an Office Open XML hyperlink (<w:hyperlink>) tag. 014 015 @author J. Alex Taylor, hairyfatguy.com 016 */ 017//------------------------------------------------------------------------------ 018// com.hfg XML/HTML Coding Library 019// 020// This library is free software; you can redistribute it and/or 021// modify it under the terms of the GNU Lesser General Public 022// License as published by the Free Software Foundation; either 023// version 2.1 of the License, or (at your option) any later version. 024// 025// This library is distributed in the hope that it will be useful, 026// but WITHOUT ANY WARRANTY; without even the implied warranty of 027// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 028// Lesser General Public License for more details. 029// 030// You should have received a copy of the GNU Lesser General Public 031// License along with this library; if not, write to the Free Software 032// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 033// 034// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 035// jataylor@hairyfatguy.com 036//------------------------------------------------------------------------------ 037 038public class WmlHyperlink extends WmlXMLTag 039{ 040 private URL mURL; 041 042 //--------------------------------------------------------------------------- 043 public WmlHyperlink(URL inURL, Docx inDocx) 044 { 045 super(WmlXML.HYPERLINK, inDocx); 046 047 if (null == inDocx) 048 { 049 throw new DocxException("No reference to the parent doc available!?"); 050 } 051 052 mURL = inURL; 053 054 // Ensure that the styles part has been initialized 055 inDocx.getStylesPart(); 056 } 057 058 //--------------------------------------------------------------------------- 059 // Since we need to know whether this is being added to the body, the header, 060 // or the footer we can't establish the relationship in the constructor. 061 public void register() 062 { 063 String id = getParentOfficeXMLPart().getRelationshipPart().addHyperlink(mURL); 064 setAttribute(RelationshipXML.ID_ATT, id); 065 } 066 067 //--------------------------------------------------------------------------- 068 public WmlHyperlink setId(String inValue) 069 { 070 setAttribute(RelationshipXML.ID_ATT, inValue); 071 return this; 072 } 073 074 //--------------------------------------------------------------------------- 075 public String getId() 076 { 077 return getAttributeValue(RelationshipXML.ID_ATT.getLocalName()); 078 } 079 080 //--------------------------------------------------------------------------- 081 public WmlTextRun addTextRun() 082 { 083 WmlTextRun run = new WmlTextRun(getParentDoc()); 084 run.getProperties().setStyle(WmlCharacterStyle.HYPERLINK_STYLE_ID); 085 addSubtag(run); 086 087 return run; 088 } 089 090 //--------------------------------------------------------------------------- 091 public WmlTextRun addTextRun(String inContent) 092 { 093 WmlTextRun run = addTextRun(); 094 run.addText(inContent); 095 096 return run; 097 } 098 099 //--------------------------------------------------------------------------- 100 @Override 101 public void addSubtag(XMLizable inSubtag) 102 { 103 super.addSubtag(inSubtag); 104 // If it's a textRun, make sure that it has the hyperlink style 105 if (inSubtag instanceof WmlTextRun) 106 { 107 ((WmlTextRun)inSubtag).getProperties().setStyle(WmlCharacterStyle.HYPERLINK_STYLE_ID); 108 } 109 } 110 111}