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.io.BufferedInputStream;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Date;

import com.hfg.datetime.DateUtil;
import com.hfg.util.StringUtil;
import com.hfg.xml.XMLDoc;
import com.hfg.xml.XMLSchema;
import com.hfg.xml.XMLTag;
import com.hfg.xml.msofficexml.OfficeOpenXMLContentType;
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(OfficeOpenXmlDocument inXlsx, BufferedInputStream inStream)
   {
      this(inXlsx);

      XMLDoc doc = new XMLDoc(inStream);
      setRootNode(doc.getRootNode());

      if (! StringUtil.isSet(getTitle()))
      {
         setTitle(""); // Avoid empty XML tags
      }

      if (! StringUtil.isSet(getSubject()))
      {
         setSubject(""); // Avoid empty XML tags
      }
   }


   //---------------------------------------------------------------------------
   @Override
   public OfficeOpenXMLContentType getContentType()
   {
      return OfficeOpenXMLContentType.CORE_PROPERTIES;
   }

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

      return this;
   }

   //---------------------------------------------------------------------------
   public String getTitle()
   {
      String value = null;

      XMLTag tag = getRootNode().getOptionalSubtagByName(OfficeXML.TITLE);
      if (tag != null)
      {
         value = tag.getUnescapedContent();
      }

      return value;
   }

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

      return this;
   }

   //---------------------------------------------------------------------------
   public String getCreator()
   {
      String value = null;

      XMLTag tag = getRootNode().getOptionalSubtagByName(OfficeXML.CREATOR);
      if (tag != null)
      {
         value = tag.getUnescapedContent();
      }

      return value;
   }


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

      return this;
   }

   //---------------------------------------------------------------------------
   public String getDescription()
   {
      String value = null;

      XMLTag tag = getRootNode().getOptionalSubtagByName(OfficeXML.DESCRIPTION);
      if (tag != null)
      {
         value = tag.getUnescapedContent();
      }

      return value;
   }


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

      return this;
   }

   //---------------------------------------------------------------------------
   public String getSubject()
   {
      String value = null;

      XMLTag tag = getRootNode().getOptionalSubtagByName(OfficeXML.SUBJECT);
      if (tag != null)
      {
         value = tag.getUnescapedContent();
      }

      return value;
   }


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

      return this;
   }

   //---------------------------------------------------------------------------
   public String getRevision()
   {
      String value = null;

      XMLTag tag = getRootNode().getOptionalSubtagByName(OfficeXML.REVISION);
      if (tag != null)
      {
         value = tag.getUnescapedContent();
      }

      return value;
   }


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

      return this;
   }

   //---------------------------------------------------------------------------
   public String getKeywords()
   {
      String value = null;

      XMLTag tag = getRootNode().getOptionalSubtagByName(OfficeXML.KEYWORDS);
      if (tag != null)
      {
         value = tag.getUnescapedContent();
      }

      return value;
   }


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

      return this;
   }

   //---------------------------------------------------------------------------
   public String getLastModifiedBy()
   {
      String value = null;

      XMLTag tag = getRootNode().getOptionalSubtagByName(OfficeXML.LAST_MODIFIED_BY);
      if (tag != null)
      {
         value = tag.getUnescapedContent();
      }

      return value;
   }

   
   //---------------------------------------------------------------------------
   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 Date getDateCreated()
   {
      Date value = null;

      XMLTag tag = getRootNode().getOptionalSubtagByName(OfficeXML.CREATED);
      if (tag != null)
      {
         value = new Date(Instant.from(DateUtil.W3CDTF_FORMATTER.parse(tag.getUnescapedContent())).toEpochMilli());
      }

      return value;
   }


   //---------------------------------------------------------------------------
   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;
   }

   //---------------------------------------------------------------------------
   public Date getDateModified()
   {
      Date value = null;

      XMLTag tag = getRootNode().getOptionalSubtagByName(OfficeXML.MODIFIED);
      if (tag != null)
      {
         value = new Date(Instant.from(DateUtil.W3CDTF_FORMATTER.parse(tag.getUnescapedContent())).toEpochMilli());
      }

      return value;
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy