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

com.hfg.xml.msofficexml.part.DocumentPropertiesPart Maven / Gradle / Ivy

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

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Date;

import com.hfg.datetime.DateUtil;
import com.hfg.xml.XMLSchema;
import com.hfg.xml.XMLTag;
import com.hfg.xml.msofficexml.OfficeOpenXmlDocument;
import com.hfg.xml.msofficexml.OfficeXML;

//------------------------------------------------------------------------------
/**
 Represents an Office Open XML document properties part.

 @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]
//------------------------------------------------------------------------------
/*
Ex from microsoft.com/en-us/library/office/aa338205(v=office.12).aspx#office2007aboutnewfileformat_structureoftheofficexmlformats



   Word Document Sample
   Microsoft Office Word 2007
   2007 Microsoft Office System User
   
   2007 Microsoft Office system .docx file
   2007 Microsoft Office System User
   2
   2005-05-05T20:01:00Z
   2005-05-05T20:02:00Z

 */

public class DocumentPropertiesPart extends OfficeXMLPart
{

   //---------------------------------------------------------------------------
   public DocumentPropertiesPart(OfficeOpenXmlDocument inOfficeDoc)
   {
      super(inOfficeDoc);

      setFile(OfficeXML.CORE_PROPERTIES_FILE);

      XMLTag rootTag = new XMLTag(OfficeXML.CORE_PROPERTIES);
      rootTag.addXMLNamespaceDeclaration(OfficeXML.DUBLIN_CORE_NAMESPACE);
      rootTag.addXMLNamespaceDeclaration(OfficeXML.DUBLIN_CORE_TERMS_NAMESPACE);
      rootTag.addXMLNamespaceDeclaration(OfficeXML.DUBLIN_CORE_TYPES_NAMESPACE);
      rootTag.addXMLNamespaceDeclaration(XMLSchema.XML_SCHEMA_INSTANCE_NAMESPACE);

      setRootNode(rootTag);

      // Set some initial values:
      setTitle("");
      setSubject("");
      setCreator(System.getProperty("user.name"));
//      setKeywords("");
      setLastModifiedBy(System.getProperty("user.name"));
      setRevision("1");

      ZonedDateTime now = ZonedDateTime.now();
      setDateCreated(now);
      setDateModified(now);

   }

   //---------------------------------------------------------------------------
   public DocumentPropertiesPart setTitle(String inValue)
   {
      getOrCreateSubtag(OfficeXML.TITLE).setContent(inValue);

      return this;
   }

   //---------------------------------------------------------------------------
   public DocumentPropertiesPart setCreator(String inValue)
   {
      getOrCreateSubtag(OfficeXML.CREATOR).setContent(inValue);

      return this;
   }

   //---------------------------------------------------------------------------
   public DocumentPropertiesPart setDescription(String inValue)
   {
      getOrCreateSubtag(OfficeXML.DESCRIPTION).setContent(inValue);

      return this;
   }

   //---------------------------------------------------------------------------
   public DocumentPropertiesPart setSubject(String inValue)
   {
      getOrCreateSubtag(OfficeXML.SUBJECT).setContent(inValue);

      return this;
   }

   //---------------------------------------------------------------------------
   public DocumentPropertiesPart setRevision(String inValue)
   {
      getOrCreateSubtag(OfficeXML.REVISION).setContent(inValue);

      return this;
   }

   //---------------------------------------------------------------------------
   public DocumentPropertiesPart setKeywords(String inValue)
   {
      getOrCreateSubtag(OfficeXML.KEYWORDS).setContent(inValue);

      return this;
   }

   //---------------------------------------------------------------------------
   public DocumentPropertiesPart setLastModifiedBy(String inValue)
   {
      getOrCreateSubtag(OfficeXML.LAST_MODIFIED_BY).setContent(inValue);

      return this;
   }

   
   //---------------------------------------------------------------------------
   public DocumentPropertiesPart setDateCreated(Date inValue)
   {
      return setDateCreated(DateUtil.convertToZonedDateTime(inValue));
   }

   //---------------------------------------------------------------------------
   public DocumentPropertiesPart setDateCreated(ZonedDateTime inValue)
   {
      // Convert time to UTC
      ZonedDateTime utc = ZonedDateTime.ofInstant(inValue.toInstant(), ZoneOffset.UTC.normalized());

      // This was sometimes creating datetimes with too many decimals (9) that caused Excel to throw errors.
      // Ex: 2021-04-13T23:09:15.754474687Z
//      getOrCreateSubtag(OfficeXML.CREATED).setContent(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(utc))
      getOrCreateSubtag(OfficeXML.CREATED).setContent(DateUtil.W3CDTF_FORMATTER.format(utc))
                                          .setAttribute(XMLSchema.TYPE_ATT, "dcterms:W3CDTF");
      return this;
   }


   //---------------------------------------------------------------------------
   public DocumentPropertiesPart setDateModified(Date inValue)
   {
      return setDateModified(DateUtil.convertToZonedDateTime(inValue));
   }
   
   //---------------------------------------------------------------------------
   public DocumentPropertiesPart setDateModified(ZonedDateTime inValue)
   {
      // Convert time to UTC
      ZonedDateTime utc = ZonedDateTime.ofInstant(inValue.toInstant(), ZoneOffset.UTC.normalized());

      // This was sometimes creating datetimes with too many decimals (9) that caused Excel to throw errors.
      // Ex: 2021-04-13T23:09:15.754474687Z
//      getOrCreateSubtag(OfficeXML.MODIFIED).setContent(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(utc))
      getOrCreateSubtag(OfficeXML.MODIFIED).setContent(DateUtil.W3CDTF_FORMATTER.format(utc))
                                           .setAttribute(XMLSchema.TYPE_ATT, "dcterms:W3CDTF");
      return this;
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy