Permalink
Browse files

Refactoring getAssociatedExtension(String).

Introducing the FormatFactory removes the PMS dependency for all
classes using this method, making them easier to test.
  • Loading branch information...
1 parent 45d0570 commit 309414d7d3909e791b235b4aa07d7f559f232dd2 @Raptor399 Raptor399 committed Feb 5, 2012
@@ -65,6 +65,7 @@
import net.pms.formats.DVRMS;
import net.pms.formats.FLAC;
import net.pms.formats.Format;
+import net.pms.formats.FormatFactory;
import net.pms.formats.GIF;
import net.pms.formats.ISO;
import net.pms.formats.JPG;
@@ -193,8 +194,6 @@ public void setRendererfound(RendererConfiguration mediarenderer) {
*/
private String serverName;
- private ArrayList<Format> extensions;
-
/**
* List of registered {@link Player}s.
*/
@@ -472,13 +471,10 @@ public void configurationChanged(ConfigurationEvent event) {
// wrap System.err
System.setErr(new PrintStream(new SystemErrWrapper(), true));
- extensions = new ArrayList<Format>();
players = new ArrayList<Player>();
allPlayers = new ArrayList<Player>();
server = new HTTPServer(configuration.getServerPort());
- registerExtensions();
-
/*
* XXX: keep this here (i.e. after registerExtensions and before registerPlayers) so that plugins
* can register custom players correctly (e.g. in the GUI) and/or add/replace custom formats
@@ -626,27 +622,6 @@ public boolean installWin32Service() {
return pwinstall.isSuccess();
}
- /**Add a known set of extensions to the extensions list.
- * @see PMS#init()
- */
- private void registerExtensions() {
- extensions.add(new WEB());
- extensions.add(new MKV());
- extensions.add(new M4A());
- extensions.add(new MP3());
- extensions.add(new ISO());
- extensions.add(new MPG());
- extensions.add(new WAV());
- extensions.add(new JPG());
- extensions.add(new OGG());
- extensions.add(new PNG());
- extensions.add(new GIF());
- extensions.add(new TIF());
- extensions.add(new FLAC());
- extensions.add(new DVRMS());
- extensions.add(new RAW());
- }
-
/**Register a known set of audio/video transcoders (known as {@link Player}s). Used in PMS#init().
* @see PMS#init()
*/
@@ -945,18 +920,15 @@ private synchronized static void createInstance() {
}
/**
+ * @deprecated Use {@link FormatFactory#getAssociatedExtension(filename)}
+ * instead.
+ *
* @param filename
* @return The format.
*/
+ @Deprecated
public Format getAssociatedExtension(String filename) {
- logger.trace("Search extension for " + filename);
- for (Format ext : extensions) {
- if (ext.match(filename)) {
- logger.trace("Found 1! " + ext.getClass().getName());
- return ext.duplicate();
- }
- }
- return null;
+ return FormatFactory.getAssociatedExtension(filename);
}
public Player getPlayer(Class<? extends Player> profileClass, Format ext) {
@@ -1039,8 +1011,13 @@ public HTTPServer getServer() {
return server;
}
+ /**
+ * @deprecated Use {@link FormatFactory#getExtensions()} instead.
+ *
+ * @return The list of formats.
+ */
public ArrayList<Format> getExtensions() {
- return extensions;
+ return FormatFactory.getExtensions();
}
public ArrayList<Player> getPlayers() {
@@ -25,6 +25,7 @@
import static net.pms.util.StringUtil.endTag;
import static net.pms.util.StringUtil.openTag;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
@@ -56,6 +57,7 @@
import net.pms.external.ExternalListener;
import net.pms.external.StartStopListener;
import net.pms.formats.Format;
+import net.pms.formats.FormatFactory;
import net.pms.io.OutputParams;
import net.pms.io.ProcessWrapper;
import net.pms.io.SizeLimitInputStream;
@@ -853,7 +855,7 @@ public boolean refreshChildren() {
protected void checktype() {
if (getExt() == null) {
- setExt(PMS.get().getAssociatedExtension(getSystemName()));
+ setExt(FormatFactory.getAssociatedExtension(getSystemName()));
}
if (getExt() != null) {
if (getExt().isUnknown()) {
@@ -25,16 +25,15 @@
import java.util.ArrayList;
import java.util.List;
-import org.apache.commons.lang.StringUtils;
-
import net.pms.PMS;
+import net.pms.configuration.RendererConfiguration;
+import net.pms.formats.FormatFactory;
import net.pms.io.OutputParams;
import net.pms.io.ProcessWrapperImpl;
import net.pms.util.FileUtil;
import net.pms.util.ProcessUtil;
-// Ditlew
-import net.pms.configuration.RendererConfiguration;
+import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -233,7 +232,7 @@ public boolean isFolder() {
@Override
public boolean isValid() {
if (getExt() == null) {
- setExt(PMS.get().getAssociatedExtension("dummy.iso"));
+ setExt(FormatFactory.getAssociatedExtension("dummy.iso"));
}
return true;
}
@@ -32,6 +32,7 @@
import net.pms.configuration.MapFileConfiguration;
import net.pms.dlna.virtual.TranscodeVirtualFolder;
import net.pms.dlna.virtual.VirtualFolder;
+import net.pms.formats.FormatFactory;
import net.pms.network.HTTPResource;
import net.pms.util.NaturalComparator;
@@ -92,7 +93,7 @@ private boolean isFolderRelevant(File f) {
File children[] = f.listFiles();
for (File child : children) {
if (child.isFile()) {
- if (PMS.get().getAssociatedExtension(child.getName()) != null || isFileRelevant(child)) {
+ if (FormatFactory.getAssociatedExtension(child.getName()) != null || isFileRelevant(child)) {
excludeNonRelevantFolder = false;
break;
}
@@ -264,7 +265,7 @@ public void doRefreshChildren() {
}
for (File f : files) {
if (!f.isHidden()) {
- if (f.isDirectory() || PMS.get().getAssociatedExtension(f.getName()) != null) {
+ if (f.isDirectory() || FormatFactory.getAssociatedExtension(f.getName()) != null) {
addedFiles.add(f);
}
}
@@ -27,11 +27,11 @@
import net.pms.PMS;
import net.pms.formats.Format;
+import net.pms.formats.FormatFactory;
import net.pms.util.FileUtil;
import net.pms.util.ProcessUtil;
import org.apache.commons.lang.StringUtils;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -134,7 +134,7 @@ public String getName() {
@Override
protected void checktype() {
if (getExt() == null) {
- setExt(PMS.get().getAssociatedExtension(getFile().getAbsolutePath()));
+ setExt(FormatFactory.getAssociatedExtension(getFile().getAbsolutePath()));
}
super.checktype();
@@ -21,8 +21,8 @@
import java.io.IOException;
import java.io.InputStream;
-import net.pms.PMS;
import net.pms.dlna.DLNAResource;
+import net.pms.formats.FormatFactory;
import net.pms.network.HTTPResource;
/**
@@ -158,7 +158,7 @@ public String getThumbnailContentType() {
*/
@Override
public boolean isValid() {
- setExt(PMS.get().getAssociatedExtension("toto.mpg"));
+ setExt(FormatFactory.getAssociatedExtension("toto.mpg"));
return true;
}
}
@@ -228,4 +228,13 @@ public boolean skip(String extensions, String moreExtensions) {
}
return false;
}
+
+ /**
+ * Return the class name for string representation.
+ * @return The name.
+ */
+ public String toString() {
+ return getClass().getSimpleName();
+
+ }
}
@@ -0,0 +1,86 @@
+/*
+ * PS3 Media Server, for streaming any medias to your PS3.
+ * Copyright (C) 2008 A.Brochard
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; version 2
+ * of the License only.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+package net.pms.formats;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This class matches and instantiates formats.
+ */
+public final class FormatFactory {
+ /**
+ * Logger used for all logging.
+ */
+ private static final Logger LOGGER = LoggerFactory.getLogger(FormatFactory.class);
+
+ /**
+ * List of known formats.
+ */
+ private static final Format[] FORMATS = new Format[] { new DVRMS(),
+ new FLAC(), new GIF(), new ISO(), new JPG(), new M4A(), new MKV(),
+ new MP3(), new MPG(), new OGG(), new PNG(), new RAW(), new TIF(),
+ new WAV(), new WEB() };
+
+ /**
+ * This class is not meant to be instantiated.
+ */
+ private FormatFactory() {
+ }
+
+ /**
+ * Match a given filename to all known formats and return a fresh instance
+ * of that format. Matching is done by the extension (e.g. ".gif") or
+ * protocol (e.g. "http://") of the filename. Will return <code>null</code>
+ * if no match can be made.
+ *
+ * @param filename The filename to match.
+ * @return The format.
+ * @see Format#match(String)
+ */
+ public static Format getAssociatedExtension(final String filename) {
+ for (Format ext : FORMATS) {
+ if (ext.match(filename)) {
+ LOGGER.trace("Matched format " + ext + " to \"" + filename + "\"");
+
+ // Return a fresh instance
+ return ext.duplicate();
+ }
+ }
+
+ LOGGER.trace("Could not match any format to \"" + filename + "\"");
+ return null;
+ }
+
+ /**
+ * Returns the list of known formats.
+ *
+ * @return The list of known formats.
+ */
+ public static ArrayList<Format> getExtensions() {
+ ArrayList<Format> formats = new ArrayList<Format>(Arrays.asList(FORMATS));
+
+ return formats;
+ }
+}
+
Oops, something went wrong.

4 comments on commit 309414d

Member

Raptor399 replied Feb 16, 2012

Does this commit break pmsencoder as reported on the forum?

Yes. VirtualVideoAction is broken to....

Member

Raptor399 replied Feb 17, 2012

I've done some research: playing the VirtualVideoAction MPG is not broken by this commit, but by c761b08.

I don't really care which commit broke this or that. It doesn't work will it be fixed????

Please sign in to comment.