org.odftoolkit.odfdom.incubator.doc.office.OdfOfficeStyles Maven / Gradle / Ivy
/************************************************************************ * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * Copyright 2008, 2010 Oracle and/or its affiliates. All rights reserved. * * Use is subject to license terms. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0. You can also * obtain a copy of the License at http://odftoolkit.org/docs/license.txt * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * limitations under the License. * ************************************************************************/ package org.odftoolkit.odfdom.incubator.doc.office; import java.util.ArrayList; import java.util.HashMap; import org.odftoolkit.odfdom.pkg.OdfElement; import org.odftoolkit.odfdom.pkg.OdfFileDom; import org.odftoolkit.odfdom.dom.element.draw.DrawFillImageElement; import org.odftoolkit.odfdom.dom.element.draw.DrawGradientElement; import org.odftoolkit.odfdom.dom.element.draw.DrawHatchElement; import org.odftoolkit.odfdom.dom.element.draw.DrawMarkerElement; import org.odftoolkit.odfdom.dom.element.number.NumberBooleanStyleElement; import org.odftoolkit.odfdom.dom.element.number.NumberTextStyleElement; import org.odftoolkit.odfdom.dom.element.office.OfficeStylesElement; import org.odftoolkit.odfdom.dom.style.OdfStyleFamily; import org.odftoolkit.odfdom.incubator.doc.number.OdfNumberCurrencyStyle; import org.odftoolkit.odfdom.incubator.doc.number.OdfNumberDateStyle; import org.odftoolkit.odfdom.incubator.doc.number.OdfNumberPercentageStyle; import org.odftoolkit.odfdom.incubator.doc.number.OdfNumberStyle; import org.odftoolkit.odfdom.incubator.doc.number.OdfNumberTimeStyle; import org.odftoolkit.odfdom.incubator.doc.style.OdfDefaultStyle; import org.odftoolkit.odfdom.incubator.doc.style.OdfStyle; import org.odftoolkit.odfdom.incubator.doc.text.OdfTextListStyle; import org.odftoolkit.odfdom.incubator.doc.text.OdfTextOutlineStyle; import org.w3c.dom.Node; /** * Convenient functionalty for the parent ODF OpenDocument element * */ public class OdfOfficeStyles extends OfficeStylesElement { private static final long serialVersionUID = 700763983193326060L; // styles that are only in OdfOfficeStyles private HashMap
element */ public OdfDefaultStyle getOrCreateDefaultStyle(OdfStyleFamily family) { OdfDefaultStyle style = getDefaultStyle(family); if (style == null) { style = ((OdfFileDom) this.ownerDocument).newOdfElement(OdfDefaultStyle.class); style.setStyleFamilyAttribute(family.getName()); this.appendChild(style); } return style; } /** * Create ODF TextListStyle * * @param name The style name * @return The code>OdfTextListStyle element */ public OdfTextListStyle newListStyle(String name) { OdfTextListStyle newStyle = ((OdfFileDom) this.ownerDocument).newOdfElement(OdfTextListStyle.class); newStyle.setStyleNameAttribute(name); this.appendChild(newStyle); return newStyle; } /** * Retrieve or create ODF OutlineStyle * * @return The code>OdfTextOutlineStyle element */ public OdfTextOutlineStyle getOrCreateOutlineStyle() { if (mOutlineStyle == null) { this.appendChild(((OdfFileDom) this.ownerDocument).newOdfElement(OdfTextOutlineStyle.class)); } return mOutlineStyle; } /** * Returns themDefaultStyles; private HashMap mMarker; private HashMap mGradients; private HashMap mHatches; private HashMap mFillImages; private OdfTextOutlineStyle mOutlineStyle; // styles that are common for OdfOfficeStyles and OdfOfficeAutomaticStyles private OdfStylesBase mStylesBaseImpl; public OdfOfficeStyles(OdfFileDom ownerDoc) { super(ownerDoc); mStylesBaseImpl = new OdfStylesBase(); } /** * Create an ODF style with style name and family * * @param name The style name * @param family The style family * @return The OdfStyle
element */ public OdfStyle newStyle(String name, OdfStyleFamily family) { OdfStyle newStyle = ((OdfFileDom) this.ownerDocument).newOdfElement(OdfStyle.class); newStyle.setStyleNameAttribute(name); newStyle.setStyleFamilyAttribute(family.getName()); this.appendChild(newStyle); return newStyle; } /** * Retrieve or create ODF default style * * @param family The style family * @return The code>OdfDefaultStyleOdfTextOutlineStyle
element. * * @return a pointer to the outline stye or null if there is no such element */ public OdfTextOutlineStyle getOutlineStyle() { return mOutlineStyle; } /** * Returns theOdfStyleDefaultStyle
element. * * @param familyType is the family for the default style * @return the default style with the given family or null if there is no such default style */ public OdfDefaultStyle getDefaultStyle(OdfStyleFamily familyType) { if (mDefaultStyles != null) { return mDefaultStyles.get(familyType); } else { return null; } } /** * Returns an iterator for allOdfStyleDefaultStyle
elements. * * @return iterator for allOdfStyleDefaultStyle
elements */ public IterablegetDefaultStyles() { if (mDefaultStyles != null) { return mDefaultStyles.values(); } else { return new ArrayList (); } } /** * Returns the DrawMarkerElement
element with the given name. * * @param name is the name of the marker * @return the marker or null if there is no such marker */ public DrawMarkerElement getMarker(String name) { if (mMarker != null) { return mMarker.get(name); } else { return null; } } /** * Returns an iterator for allDrawMarkerElement
elements. * * @return an iterator for allDrawMarkerElement
elements */ public IterablegetMarker() { if (mMarker != null) { return mMarker.values(); } else { return new ArrayList (); } } /** * Returns the DrawGradientElement
element with the given name. * * @param name is the name of the gradient * @return the gradient or null if there is no such gradient */ public DrawGradientElement getGradient(String name) { if (mGradients != null) { return mGradients.get(name); } else { return null; } } /** * Returns an iterator for allDrawGradientElement
elements. * * @return an iterator for allDrawGradientElement
elements */ public IterablegetGradients() { if (mGradients != null) { return mGradients.values(); } else { return new ArrayList (); } } /** * Returns the DrawHatchElement
element with the given name. * * @param name is the name of the hatch * @return the hatch or null if there is no such hatch */ public DrawHatchElement getHatch(String name) { if (mHatches != null) { return mHatches.get(name); } else { return null; } } /** * Returns an iterator for allDrawHatchElement
elements. * * @return an iterator for allDrawHatchElement
elements */ public IterablegetHatches() { if (mHatches != null) { return mHatches.values(); } else { return new ArrayList (); } } /** * Returns the DrawFillImageElement
element with the given name. * * @param name is the name of the fill image * @return the fill image or null if there is no such fill image */ public DrawFillImageElement getFillImage(String name) { if (mFillImages != null) { return mFillImages.get(name); } else { return null; } } /** * Returns an iterator for allDrawFillImageElement
elements. * * @return an iterator for allDrawFillImageElement
elements */ public IterablegetFillImages() { if (mFillImages != null) { return mFillImages.values(); } else { return new ArrayList (); } } /** * Returns the OdfStyle
element with the given name and family. * * @param name is the name of the style * @param familyType is the family of the style * @return the style or null if there is no such style */ public OdfStyle getStyle(String name, OdfStyleFamily familyType) { return mStylesBaseImpl.getStyle(name, familyType); } /** * Returns an iterator for allOdfStyle
elements for the given family. * * @param familyType * @return an iterator for allOdfStyle
elements for the given family */ public IterablegetStylesForFamily(OdfStyleFamily familyType) { return mStylesBaseImpl.getStylesForFamily(familyType); } /** * Returns the OdfTextListStyle
element with the given name. * * @param name is the name of the list style * @return the list style or null if there is no such list style */ public OdfTextListStyle getListStyle(String name) { return mStylesBaseImpl.getListStyle(name); } /** * Returns an iterator for allOdfTextListStyle
elements. * * @return an iterator for allOdfTextListStyle
elements */ public IterablegetListStyles() { return mStylesBaseImpl.getListStyles(); } /** * Returns the OdfNumberNumberStyle
element with the given name. * * @param name is the name of the number style * @return the number style or null if there is no such number style */ public OdfNumberStyle getNumberStyle(String name) { return mStylesBaseImpl.getNumberStyle(name); } /** * Returns an iterator for allOdfNumberNumberStyle
elements. * * @return an iterator for allOdfNumberNumberStyle
elements */ public IterablegetNumberStyles() { return mStylesBaseImpl.getNumberStyles(); } /** * Returns the OdfNumberDateStyle
element with the given name. * * @param name is the name of the date style * @return the date style or null if there is no such date style */ public OdfNumberDateStyle getDateStyle(String name) { return mStylesBaseImpl.getDateStyle(name); } /** * Returns an iterator for allOdfNumberDateStyle
elements. * * @return an iterator for allOdfNumberDateStyle
elements */ public IterablegetDateStyles() { return mStylesBaseImpl.getDateStyles(); } /** * Returns the OdfNumberPercentageStyle
element with the given name. * * @param name is the name of the percentage style * @return the percentage style null if there is no such percentage style */ public OdfNumberPercentageStyle getPercentageStyle(String name) { return mStylesBaseImpl.getPercentageStyle(name); } /** * Returns an iterator for allOdfNumberPercentageStyle
elements. * * @return an iterator for allOdfNumberPercentageStyle
elements */ public IterablegetPercentageStyles() { return mStylesBaseImpl.getPercentageStyles(); } /** * Returns the OdfNumberCurrencyStyle
element with the given name. * * @param name is the name of the currency style * @return the currency style null if there is no such currency style */ public OdfNumberCurrencyStyle getCurrencyStyle(String name) { return mStylesBaseImpl.getCurrencyStyle(name); } /** * Returns an iterator for allOdfNumberCurrencyStyle
elements. * * @return an iterator for allOdfNumberCurrencyStyle
elements */ public IterablegetCurrencyStyles() { return mStylesBaseImpl.getCurrencyStyles(); } /** * Returns the OdfNumberTimeStyle
element with the given name. * * @param name is the name of the time style * @return the time style null if there is no such time style */ public OdfNumberTimeStyle getTimeStyle(String name) { return mStylesBaseImpl.getTimeStyle(name); } /** * Returns an iterator for allOdfNumberTimeStyle
elements. * * @return an iterator for allOdfNumberTimeStyle
elements */ public IterablegetTimeStyles() { return mStylesBaseImpl.getTimeStyles(); } /** * Returns the NumberBooleanStyleElement
element with the given name. * * @param name is the name of the boolean style * @return the boolean style null if there is no such boolean style */ public NumberBooleanStyleElement getBooleanStyle(String name) { return mStylesBaseImpl.getBooleanStyle(name); } /** * Returns an iterator for allNumberBooleanStyleElement
elements. * * @return an iterator for allNumberBooleanStyleElement
elements */ public IterablegetBooleanStyles() { return mStylesBaseImpl.getBooleanStyles(); } /** * Returns the OdfNumberTextStyle
element with the given name. * * @param name is the name of the text style * @return the text style null if there is no such text style */ public NumberTextStyleElement getTextStyle(String name) { return mStylesBaseImpl.getTextStyle(name); } /** * Returns an iterator for allOdfNumberTextStyle
elements. * * @return an iterator for allOdfNumberTextStyle
elements */ public IterablegetTextStyles() { return mStylesBaseImpl.getTextStyles(); } @Override protected void onOdfNodeInserted(OdfElement node, Node refNode) { if (node instanceof OdfDefaultStyle) { OdfDefaultStyle defaultStyle = (OdfDefaultStyle) node; if (mDefaultStyles == null) { mDefaultStyles = new HashMap (); } mDefaultStyles.put(defaultStyle.getFamily(), defaultStyle); } else if (node instanceof DrawMarkerElement) { DrawMarkerElement marker = (DrawMarkerElement) node; if (mMarker == null) { mMarker = new HashMap (); } mMarker.put(marker.getDrawNameAttribute(), marker); } else if (node instanceof DrawGradientElement) { DrawGradientElement gradient = (DrawGradientElement) node; if (mGradients == null) { mGradients = new HashMap (); } mGradients.put(gradient.getDrawNameAttribute(), gradient); } else if (node instanceof DrawHatchElement) { DrawHatchElement hatch = (DrawHatchElement) node; if (mHatches == null) { mHatches = new HashMap (); } mHatches.put(hatch.getDrawNameAttribute(), hatch); } else if (node instanceof DrawFillImageElement) { DrawFillImageElement fillImage = (DrawFillImageElement) node; if (mFillImages == null) { mFillImages = new HashMap (); } mFillImages.put(fillImage.getDrawNameAttribute(), fillImage); } else if (node instanceof OdfTextOutlineStyle) { mOutlineStyle = (OdfTextOutlineStyle) node; } else { mStylesBaseImpl.onOdfNodeInserted(node, refNode); } } @Override protected void onOdfNodeRemoved(OdfElement node) { if (node instanceof OdfDefaultStyle) { if (mDefaultStyles != null) { OdfDefaultStyle defaultStyle = (OdfDefaultStyle) node; mDefaultStyles.remove(defaultStyle.getFamily()); } } else if (node instanceof DrawMarkerElement) { if (mMarker != null) { DrawMarkerElement marker = (DrawMarkerElement) node; mMarker.remove(marker.getDrawNameAttribute()); } } else if (node instanceof DrawGradientElement) { if (mGradients != null) { DrawGradientElement gradient = (DrawGradientElement) node; mGradients.remove(gradient.getDrawNameAttribute()); } } else if (node instanceof DrawHatchElement) { if (mHatches != null) { DrawHatchElement hatch = (DrawHatchElement) node; mHatches.remove(hatch.getDrawNameAttribute()); } } else if (node instanceof DrawFillImageElement) { if (mFillImages != null) { DrawFillImageElement fillImage = (DrawFillImageElement) node; mFillImages.remove(fillImage.getDrawNameAttribute()); } } else if (node instanceof OdfTextOutlineStyle) { if (mOutlineStyle == (OdfTextOutlineStyle) node) { mOutlineStyle = null; } } else { mStylesBaseImpl.onOdfNodeRemoved(node); } } }